Skip to content

Instantly share code, notes, and snippets.

@smdooley
Created September 30, 2014 18:36
Show Gist options
  • Save smdooley/45595bfc53b68f06e901 to your computer and use it in GitHub Desktop.
Save smdooley/45595bfc53b68f06e901 to your computer and use it in GitHub Desktop.
Twitter Feed with caching
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinqToTwitter;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.Caching;
using umbraco.DataLayer;
public partial class usercontrols_Feed : System.Web.UI.UserControl
{
public int Count { get; set; }
public string ProfileName { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
MemoryCache cache = MemoryCache.Default;
List<Tweet> tweets = (List<Tweet>)cache["TwitterFeed"];
if (tweets == null)
{
try
{
tweets = new List<Tweet>();
var auth = new SingleUserAuthorizer
{
Credentials = new InMemoryCredentials
{
ConsumerKey = "",
ConsumerSecret = "",
OAuthToken = "",
AccessToken = ""
}
};
using (var twitterContext = new TwitterContext(auth))
{
var statusTweets =
from tweet in twitterContext.Status
where tweet.Type == StatusType.User
&& tweet.Count == 25
&& tweet.ScreenName == ProfileName
select tweet;
foreach (var status in statusTweets)
{
tweets.Add(new Tweet
{
//Id = Convert.ToInt32(status.Page),
Text = status.Text.Replace("\n", "").Replace("\r", ""),
CreateDate = status.CreatedAt,
Timestamp = DateTime.Now,
TweetId = status.StatusID
});
}
}
//-- Add to cache - for 1 hour
cache.Add("TwitterFeed", tweets, new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddHours(1) });
}
catch (Exception ex)
{
// todo: log error
}
}
rptTweets.DataSource = tweets.Take(Count).ToList();
rptTweets.DataBind();
}
public string ParseTweet(string rawTweet)
{
Regex link = new Regex(@"http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&amp;\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?");
Regex screenName = new Regex(@"@\w+");
Regex hashTag = new Regex(@"#\w+");
string formattedTweet = link.Replace(rawTweet, delegate(Match m)
{
string val = m.Value;
return "<a href='" + val + "' target='_blank'>" + val + "</a>";
});
formattedTweet = screenName.Replace(formattedTweet, delegate(Match m)
{
string val = m.Value.Trim('@');
return string.Format("@<a href='https://twitter.com/{0}' target='_blank'>{1}</a>", val, val);
});
formattedTweet = hashTag.Replace(formattedTweet, delegate(Match m)
{
string val = m.Value;
//return string.Format("<a href='http://twitter.com/#search?q=%23{0}&src=hash'>{1}</a>", val, val);
return string.Format("<a href='https://twitter.com/search?q=%23{0}&src=hash' target='_blank'>{1}</a>", val.Replace("#", ""), val);
});
return formattedTweet;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Tweet
/// </summary>
public class Tweet
{
public int Id { get; set; }
public string TweetId { get; set; }
public string Text { get; set; }
public DateTime CreateDate { get; set; }
public DateTime Timestamp { get; set; }
public Tweet()
{
//
// TODO: Add constructor logic here
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment