Created
April 24, 2014 22:14
-
-
Save ridomin/11271315 to your computer and use it in GitHub Desktop.
Windows App Studio. Build Demo. Adding a Twitter Data Source
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.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using System.Text.RegularExpressions; | |
using LinqToTwitter; | |
namespace AppStudio.Data | |
{ | |
public class TwitterDataProvider | |
{ | |
TwitterContext twitterCtx; | |
public TwitterDataProvider() | |
{ | |
var auth = new SingleUserAuthorizer() | |
{ | |
CredentialStore = new SingleUserInMemoryCredentialStore | |
{ | |
ConsumerKey = ConsumerKey, | |
ConsumerSecret = ConsumerSecret, | |
AccessToken = AccessToken, | |
AccessTokenSecret = AccessTokenSecret | |
} | |
}; | |
twitterCtx = new TwitterContext(auth); | |
} | |
public async Task<IEnumerable<DefaultSchema>> Load() | |
{ | |
var statusTweets = await | |
(from tweet in twitterCtx.Status | |
where tweet.Type == StatusType.Home && | |
tweet.Count == 40 | |
select new DefaultSchema() | |
{ | |
Title = tweet.User.Name, | |
ImageUrl = tweet.User.ProfileImageUrl, | |
Id = tweet.ID.ToString(), | |
Subtitle = ElapsedSeconds(tweet.CreatedAt), | |
Description = ParseURL(tweet.Text) | |
}). | |
ToListAsync(); | |
return statusTweets; | |
} | |
private string ElapsedSeconds(DateTime input) | |
{ | |
int mins = Convert.ToInt32(DateTime.UtcNow.Subtract(input).TotalSeconds / 60); | |
return mins + " minutes ago"; | |
} | |
private string ParseURL(string s) | |
{ | |
return Regex.Replace(s, @"(http(s)?://)?([\w-]+\.)+[\w-]+(/\S\w[\w- ;,./?%&=]\S*)?", | |
new MatchEvaluator((m) => | |
{ | |
string x = m.ToString(); | |
return string.Format("<a href=\"{0}\" target=\"_blank\">{0}</a>", x); | |
})); | |
} | |
#region tkeys | |
const string ConsumerKey = ""; | |
const string ConsumerSecret = ""; | |
const string AccessToken = ""; | |
const string AccessTokenSecret = ""; | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment