Advertisement
Guest User

Netduino - Twitter WebRequest and Parsing

a guest
May 24th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1.         private ArrayList ParseResponse(string html)
  2.         {
  3.             // Get the index of the first result
  4.             int resultIndex = html.IndexOf("\"results\":[{\"created_at\":");
  5.  
  6.             // Check if there is no result (no result(s) shows "results:[]" and thus index == -1)
  7.             if (resultIndex == -1) return null;
  8.  
  9.             // Take apart the results from the full json
  10.             html = html.Substring(resultIndex + 11);
  11.  
  12.             // ArrayList to store individual results
  13.             ArrayList results = new ArrayList();
  14.             // Bool to indicate wheter there are more results left in the json
  15.             bool moreResults = true;
  16.  
  17.             // Split the individual results
  18.             do
  19.             {
  20.                 resultIndex = html.IndexOf("},{\"created_at\":");
  21.                 if (resultIndex == -1)
  22.                 {
  23.                     moreResults = false;
  24.                     results.Add(html);
  25.                 }
  26.                 else
  27.                 {
  28.                     results.Add(html.Substring(0, resultIndex));
  29.                     html = html.Substring(resultIndex + 2);
  30.                 }
  31.             } while (moreResults);
  32.  
  33.  
  34.  
  35.             // Save the tweets in an ArrayList
  36.             ArrayList tweets = new ArrayList();
  37.  
  38.             // Make new tweet object of the results and put them in the ArrayList
  39.             foreach (string result in results)
  40.             {
  41.  
  42.                 // User
  43.                 int userIndex = result.IndexOf("\"from_user\":\"") + 13;
  44.                 int userEndIndex = result.IndexOf(",\"from_user_id\":");
  45.                 string user = result.Substring(userIndex, userEndIndex - userIndex - 1);
  46.  
  47.                 // Text
  48.                 int textIndex = result.IndexOf("\"text\":\"") + 8;
  49.                 int textEndIndex = result.IndexOf(",\"to_user\":");
  50.                 string text = result.Substring(textIndex, textEndIndex - textIndex - 1);
  51.  
  52.                 // @todo ID
  53.  
  54.                 tweets.Add(new Tweet()
  55.                 {
  56.                     Text = text,
  57.                     User = user
  58.                 });
  59.  
  60.             }
  61.  
  62.             // Return results
  63.             return tweets;
  64.         }
  65.  
  66.         /// <summary>
  67.         /// Performs a HTTP GET WebRequest.
  68.         /// </summary>
  69.         /// <param name="url">The URL which needs to be requested.</param>
  70.         /// <returns>The WebResponse of the request.</returns>
  71.         private string PerformGetRequest(string url)
  72.         {
  73.             HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
  74.             request.KeepAlive = false;
  75.             WebResponse response = request.GetResponse();
  76.             StreamReader reader = new StreamReader(response.GetResponseStream());
  77.             string responseString = reader.ReadToEnd();
  78.             reader.Close();
  79.             return responseString;
  80.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement