[twitter-dev] problem getting acurate remaining-hits with rate_limit_status

2009-11-09 Thread ch...@stuffworldwide.com

My program sends is using rate_limit_status.xml to get the number of
remaining hits until the account limit is exceeded. My problem is that
it always seems to return the same value: 150. Even once I have
already hit the limit the service continues to show 150 hits left. I
am thinking that it is returning the hits remaining for the IP address
and not the user account I need.

Any ideas? I've been trying to figure this out all night but I haven't
been able to find to many examples of the rate_limit_status.xml
service. Here is my code:


string logon=this.UserName + : + this.Password;
logon=System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes
(logon));

//request
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(url);
req.Timeout=System.Convert.ToInt32(gen.Get(twitterTimeout));
req.Headers.Add(Authorization,Basic  + logon);
req.ServicePoint.Expect100Continue=false;

HttpWebResponse resp=(HttpWebResponse)req.GetResponse();
StreamReader sr=new StreamReader(resp.GetResponseStream
(),Encoding.GetEncoding(1252));

//get results
result=sr.ReadToEnd();
resp.Close();



The code XML response from twitter is:

?xml version=1.0 encoding=UTF-8?
hash
  remaining-hits type=integer150/remaining-hits
  hourly-limit type=integer150/hourly-limit
  reset-time-in-seconds type=integer1257786363/reset-time-in-
seconds
  reset-time type=datetime2009-11-09T17:06:03+00:00/reset-time
/hash


[twitter-dev] .NET Class for handling Twitter Updates and Rate Checks

2009-11-10 Thread ch...@stuffworldwide.com

Not many .NET examples out there... here it is... have fun...


using System;

using System.Text;

using System.Net;

using System.IO;

using System.Xml;



namespace Tweeter

{

  public class TwitterTools

  {

#region Members

#endregion

public TwitterTools()

{

  this.Initialize();

}

public TwitterTools(string userName,string password)

{

  this.UserName=userName;

  this.Password=password;



  this.Initialize();

}

private void Initialize()

{

}

public void Dispose()

{

}

#region Properties

public string UserName=null;

public string Password=null;

#endregion

#region Methods

public int Update(string message)

{

  int retval=0;

  string code=null;

  string url=http://twitter.com/account/
rate_limit_status.xml;  //gen.Get(twitterRateService);



  string result=null;

  try

  {

result=this.Request(url + ?

  ,null

  ,GET

  );

//  gen.Test(result);

  }

  catch

  {

result=null;

  }



  if(result==null)

retval=2;

  else

  {

//parse results

try

{

  XmlDocument doc=new XmlDocument();

  doc.LoadXml(result);



  XmlNodeList nodes=doc.SelectNodes(/hash/
remaining-hits);

  int remaining=System.Convert.ToInt32
(nodes[0].InnerText);

  if(remaining=0)

retval=2;



  nodes=null;

  doc=null;

}

catch

{

  retval=2;

}



if(retval!=2)

{

  StringBuilder txt=new StringBuilder();

  txt.Append(status=);

  txt.Append(message);

  code=txt.ToString();



  try

  {

string ret=this.Request(http://
twitter.com/statuses/update.xml//gen.Get(twitterUpdateService)

  ,code);



if(ret!=null)

  retval=1;

else

  retval=0;

  }

  catch

  {

retval=0;

  }

}

  }



  return retval;

}

private string Request(string url,string code)

{

  return this.Request(url,code,POST);

}

private string Request(string url,string code,string
method)

{

  byte[] bytes=null;

  if(code!=null)

bytes=System.Text.Encoding.ASCII.GetBytes
(code);



  string logon=null;

  if(code==null)

logon=Basic  + this.UserName + : +
this.Password;

  else

logon=this.UserName + : + this.Password;

  logon=System.Convert.ToBase64String
(System.Text.Encoding.UTF8.GetBytes(logon));



  //request

  HttpWebRequest req=(HttpWebRequest)WebRequest.Create
(url);

  req.Timeout=1;

  //req.Timeout=System.Convert.ToInt32(gen.Get
(twitterTimeout));

  if(code==null)

req.Headers.Add(Authorization,logon);

  else

req.Headers.Add(Authorization,Basic  +
logon);

  req.ServicePoint.Expect100Continue=false;

  if(code!=null)

  {

req.ContentType=application/x-www-form-
urlencoded;

req.Method=method;

req.ContentLength=code==null ? 0 :
bytes.Length;



Stream trans=req.GetRequestStream();


[twitter-dev] Re: .NET Class for handling Twitter Updates and Rate Checks

2009-11-12 Thread ch...@stuffworldwide.com
I sent it to the twitter people to post on their site but they asked
me to post here as well... I was like... okay

On Nov 10, 5:53 pm, Andrew Badera and...@badera.us wrote:
 I for one tend to prefer Google Code or Code Plex for posting lengthy
 chunks of code intended for resharing ...

 Also, LinqToTwitter is a pretty solid reference implementation ...
 FWIW. (Not affiliated, just a user.)

 ∞ Andy Badera
 ∞ +1 518-641-1280 Google Voice
 ∞ This email is: [ ] bloggable [x] ask first [ ] private
 ∞ Google me:http://www.google.com/search?q=andrew%20badera

 On Tue, Nov 10, 2009 at 5:49 PM, ch...@stuffworldwide.com



 ch...@stuffworldwide.com wrote:

  Not many .NETexamples out there... here it is... have fun...

  using System;

  using System.Text;

  using System.Net;

  using System.IO;

  using System.Xml;

  namespace Tweeter

  {

       public class TwitterTools

       {

             #region Members

             #endregion

             public TwitterTools()

             {

                   this.Initialize();

             }

             public TwitterTools(string userName,string password)

             {

                   this.UserName=userName;

                   this.Password=password;

                   this.Initialize();

             }

             private void Initialize()

             {

             }

             public void Dispose()

             {

             }

             #region Properties

             public string UserName=null;

             public string Password=null;

             #endregion

             #region Methods

             public int Update(string message)

             {

                   int retval=0;

                   string code=null;

                   string url=http://twitter.com/account/
  rate_limit_status.xml;      //gen.Get(twitterRateService);

                   string result=null;

                   try

                   {

                         result=this.Request(url + ?

                               ,null

                               ,GET

                               );

  //                      gen.Test(result);

                   }

                   catch

                   {

                         result=null;

                   }

                   if(result==null)

                         retval=2;

                   else

                   {

                         //parse results

                         try

                         {

                               XmlDocument doc=new XmlDocument();

                               doc.LoadXml(result);

                               XmlNodeList nodes=doc.SelectNodes(/hash/
  remaining-hits);

                               int remaining=System.Convert.ToInt32
  (nodes[0].InnerText);

                               if(remaining=0)

                                     retval=2;

                               nodes=null;

                               doc=null;

                         }

                         catch

                         {

                               retval=2;

                         }

                         if(retval!=2)

                         {

                               StringBuilder txt=new StringBuilder();

                               txt.Append(status=);

                               txt.Append(message);

                               code=txt.ToString();

                               try

                               {

                                     string ret=this.Request(http://
  twitter.com/statuses/update.xml//gen.Get(twitterUpdateService)

                                           ,code);

                                     if(ret!=null)

                                           retval=1;

                                     else

                                           retval=0;

                               }

                               catch

                               {

                                     retval=0;

                               }

                         }

                   }

                   return retval;

             }

             private string Request(string url,string code)

             {

                   return this.Request(url,code,POST);

             }

             private string Request(string url,string code,string
  method)

             {

                   byte[] bytes=null;

                   if(code!=null)

                         bytes=System.Text.Encoding.ASCII.GetBytes
  (code);

                   string logon=null;

                   if(code==null)

                         logon=Basic  + this.UserName + : +
  this.Password;

                   else

                         logon=this.UserName + : + this.Password;

                   logon=System.Convert.ToBase64String
  (System.Text.Encoding.UTF8.GetBytes(logon));

                   //request

                   HttpWebRequest req