How to get tweet's HTML with LinqToTwitter?
Asked Answered
S

1

5

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);
Serenity answered 21/10, 2011 at 21:15 Comment(1)
cannot you use the regex replace method on _parseHashtags?Showmanship
S
18

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;
    }
}
Serenity answered 21/10, 2011 at 22:10 Comment(1)
A combination of LinqToTwitter and your extensions makes a great solution for managing tweets.Ulyssesumayyad

© 2022 - 2024 — McMap. All rights reserved.