2011-10-21 4 views
5

私は最近TweetSharpからLinqToTwitterに切り替えました。欠けているものは、ツイートをHTMLとして取得する方法です。LinqToTwitterでツイートのHTMLを取得するには?

TweetSharpには、言及、ハッシュタグ、ハイパーリンクを自動的にリンクする.TextAsHtml()というメソッドがありました。

このような機能がLinqtoTwitterに存在するかどうかは誰にも知られていますか? TweetSharpがこれをどのように達成できたかについての洞察は、非常に喜ばれるだろう。

UPDATE:

それはTweetSharp URLを一致させるために正規表現を使用しているかのように、見え言及し、およびハッシュタグ。ここにサンプルがあります。

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); 
+0

_parseHashtagsでregex replaceメソッドを使用することはできませんか? –

答えて

17

ここでは、TweetSharpのライブラリのロジックを使用した最終的な解決方法です。それはうまくいきます:

/// <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; 
    } 
} 
+0

ありがとう、これは私を助けた! – Roger

+0

私にとって間違いなく機能します。大いに感謝する! – Nickmaovich

+0

LinqToTwitterとあなたの拡張機能を組み合わせると、つぶやきを管理するのに最適なソリューションになります。 – avantprime

関連する問題