Skip to content

Instantly share code, notes, and snippets.

@jrsconfitto
Created June 7, 2012 20:13
Show Gist options
  • Save jrsconfitto/2891285 to your computer and use it in GitHub Desktop.
Save jrsconfitto/2891285 to your computer and use it in GitHub Desktop.
Grab a Twitter search result using RestSharp and some json classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RestSharpLearning
{
public class Metadata
{
public string result_type { get; set; }
}
public class Tweet
{
public string created_at { get; set; }
public string from_user { get; set; }
public int from_user_id { get; set; }
public string from_user_id_str { get; set; }
public string from_user_name { get; set; }
public object geo { get; set; }
public string id { get; set; }
public string id_str { get; set; }
public string iso_language_code { get; set; }
public Metadata metadata { get; set; }
public string profile_image_url { get; set; }
public string profile_image_url_https { get; set; }
public string source { get; set; }
public string text { get; set; }
public string to_user { get; set; }
public int to_user_id { get; set; }
public string to_user_id_str { get; set; }
public string to_user_name { get; set; }
public long? in_reply_to_status_id { get; set; }
public string in_reply_to_status_id_str { get; set; }
}
public class TwitterSearchResponse
{
public double completed_in { get; set; }
public long max_id { get; set; }
public string max_id_str { get; set; }
public string next_page { get; set; }
public int page { get; set; }
public string query { get; set; }
public string refresh_url { get; set; }
public List<Tweet> results { get; set; }
public int results_per_page { get; set; }
public int since_id { get; set; }
public string since_id_str { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestSharp;
using System.IO;
namespace RestSharpLearning
{
class TwitterSearch
{
static void Main(string[] args)
{
Console.WriteLine("We're going to try to use Twitter's search api to get some tweets back!");
var base_url = "http://search.twitter.com/";
var client = new RestClient(base_url);
Console.WriteLine("Please enter a query that you may want to use:");
var user_query = Console.ReadLine();
user_query = user_query.Trim();
// Spacer
Console.WriteLine();
// Setup us the request
IRestRequest query_request = new RestRequest("search.json", Method.GET);
query_request.AddParameter("q", user_query);
// Get the response
client.ExecuteAsync<TwitterSearchResponse>(query_request, query_response =>
{
Console.WriteLine("Query complete, let's see what you've won:");
Console.WriteLine("Status Code: " + query_response.StatusCode);
//Console.WriteLine("Response: " + query_response.Content);
TwitterSearchResponse resulting_tweets = query_response.Data;
foreach (Tweet tweet in resulting_tweets.results)
{
Console.WriteLine(String.Format("Tweet from {0}: {1} - {2}", tweet.from_user, tweet.created_at, tweet.text));
}
});
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment