2011-10-21 11 views
5

मैंने हाल ही में TweetSharp से LinqToTwitter तक स्विच किया है और एक चीज जिसे मैं याद कर रहा हूं वह HTML के रूप में एक ट्वीट को पुनर्प्राप्त करने का एक तरीका है।LinqToTwitter के साथ ट्वीट के HTML कैसे प्राप्त करें?

TweetSharp में .TextAsHtml() नामक एक विधि थी जो स्वचालित रूप से उल्लेख, हैश टैग और हाइपरलिंक्स से जुड़ा हुआ था।

क्या किसी को पता है कि ऐसी सुविधा LinqtoTwitter में मौजूद है या नहीं? TweetSharp यह कैसे पूरा करने में सक्षम था इस बारे में कोई अंतर्दृष्टि बहुत सराहना की जाएगी।

अद्यतन:

ऐसा लगता है जैसे कि 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 प्रतिस्थापन विधि का उपयोग नहीं कर सकते? –

उत्तर

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

संबंधित मुद्दे