Created
July 29, 2013 23:00
-
-
Save ryanlewis/6108654 to your computer and use it in GitHub Desktop.
LinqToTwitter example to fetch tweet data for a particular tweet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using LinqToTwitter; | |
namespace TweetTest | |
{ | |
internal class Program | |
{ | |
private static readonly Regex TweetStatusRegex = new Regex(@"^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)$"); | |
private static void Main(string[] args) | |
{ | |
string tweetUrl = "https://twitter.com/wpyz/status/360832527872688129"; | |
var tweet = GetTweet(tweetUrl); | |
if (tweet == null) | |
{ | |
Console.WriteLine("Could not find that status"); | |
Console.WriteLine("\nPress any key to do one"); | |
Console.ReadKey(); | |
return; | |
} | |
Console.WriteLine("Twitter Account: {0}", tweet.User.ScreenName); | |
Console.WriteLine("Name: {0}", tweet.User.Name); | |
Console.WriteLine("Text: {0}", tweet.Text); | |
Console.WriteLine("\nPress any key to do one"); | |
Console.ReadKey(); | |
} | |
private static Status GetTweet(string tweetUrl) | |
{ | |
Match tweetUrlMatch = TweetStatusRegex.Match(tweetUrl); | |
Status status = null; | |
if (tweetUrlMatch.Success) | |
{ | |
using (var ctx = GetTwitterContext()) | |
{ | |
var tweetId = tweetUrlMatch.Groups[3].Value; | |
status = ctx.Status.FirstOrDefault(tweet => tweet.Type == StatusType.Show && tweet.ID == tweetId); | |
} | |
} | |
return status; | |
} | |
private static TwitterContext GetTwitterContext() | |
{ | |
/* Get your keys at http://dev.twitter.com */ | |
var auth = new SingleUserAuthorizer | |
{ | |
Credentials = new SingleUserInMemoryCredentials | |
{ | |
ConsumerKey = "<SECRET>", | |
ConsumerSecret = "<SECRET>", | |
TwitterAccessToken = "<SECRET>", | |
TwitterAccessTokenSecret = "<SECRET>" | |
} | |
}; | |
var ctx = new TwitterContext(auth); | |
return ctx; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment