How to scrape a photo from a Twitter Photo URL with C#

Twitter uses its URL-shortener to place photo’s in Tweets. I’ve tried processing these URLs in the browser, but cross site scripting measures prevented me from processing the result with jQuery.

I decided to resolve it in C# (as I’m using ASP.Net). The following script leverages a WebClient and a regular expression to extract the photo URL from the shortened URL. This code could be added to a handler or a web service that can be called from script.

string url = "";
string photoUrl = null;

//regex to get the real image
Regex regex = new Regex(
	RegexOptions.Compiled | RegexOptions.IgnoreCase);

//download the url
WebClient client = new WebClient();
var html = client.DownloadString(url);

//check for match - extract the PhotoUrl
var match = regex.Match(html);
if (match.Success)
	photoUrl = match.Groups["PhotoUrl"].Value;

The photoUrl contains the URL to the picture; in this case: