Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get tweet's HTML with LinqToTwitter?

I recently switched from TweetSharp to LinqToTwitter and the one thing I'm missing is a way to retrieve a tweet as HTML.

TweetSharp had a method called .TextAsHtml() which automatically linked mentions, hash tags, and hyperlinks.

Does anyone know if such a feature exist in LinqtoTwitter? Any insight into how TweetSharp was able to accomplish this would be much appricated.

UPDATE:

It looks as though TweetSharp used Regular Expressions to match URLs, mentions, and hash tags. Here is a sample:

private static readonly Regex _parseUrls = new Regex("\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^\\p{P}\\s]|/)))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex _parseMentions = new Regex("(^|\\W)@([A-Za-z0-9_]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex _parseHashtags = new Regex("[#]+[A-Za-z0-9-_]+", RegexOptions.IgnoreCase | RegexOptions.Compiled);
like image 419
Derek Hunziker Avatar asked Oct 21 '11 21:10

Derek Hunziker


1 Answers

Here is my final solution which uses some logic from TweetSharp's library. It's working out nicely:

/// <summary>
/// Extends the LinqToTwitter Library
/// </summary>
public static class TwitterExtensions
{
    private static readonly Regex _parseUrls = new Regex("\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^\\p{P}\\s]|/)))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    private static readonly Regex _parseMentions = new Regex("(^|\\W)@([A-Za-z0-9_]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    private static readonly Regex _parseHashtags = new Regex("[#]+[A-Za-z0-9-_]+", RegexOptions.IgnoreCase | RegexOptions.Compiled);

    /// <summary>
    /// Parse Status Text to HTML equivalent
    /// </summary>
    /// <param name="status">The LinqToTwitter <see cref="Status"/></param>
    /// <returns>Formatted HTML string</returns>
    public static string TextAsHtml(this Status status)
    {
        string tweetText = status.Text;

        if (!String.IsNullOrEmpty(tweetText))
        {
            // Replace URLs
            foreach (var urlMatch in _parseUrls.Matches(tweetText))
            {
                Match match = (Match)urlMatch;
                tweetText = tweetText.Replace(match.Value, String.Format("<a href=\"{0}\" target=\"_blank\">{0}</a>", match.Value));
            }

            // Replace Mentions
            foreach (var mentionMatch in _parseMentions.Matches(tweetText))
            {
                Match match = (Match)mentionMatch;
                if (match.Groups.Count == 3)
                {
                    string value = match.Groups[2].Value;
                    string text = "@" + value;
                    tweetText = tweetText.Replace(text, String.Format("<a href=\"http://twitter.com/{0}\" target=\"_blank\">{1}</a>", value, text));
                }
            }

            // Replace Hash Tags
            foreach (var hashMatch in _parseHashtags.Matches(tweetText))
            {
                Match match = (Match)hashMatch;
                string query = Uri.EscapeDataString(match.Value);
                tweetText = tweetText.Replace(match.Value, String.Format("<a href=\"http://search.twitter.com/search?q={0}\" target=\"_blank\">{1}</a>", query, match.Value));
            }
        }

        return tweetText;
    }
}
like image 150
Derek Hunziker Avatar answered Nov 14 '22 23:11

Derek Hunziker