2011-10-21 13 views
5

Niedawno przełączyłem się z TweetSharp na LinqToTwitter, a jedyną rzeczą, której mi brakuje, jest sposób na pobranie tweeta jako HTML.Jak uzyskać kod HTML tweeta za pomocą LinqToTwitter?

TweetSharp miał metodę o nazwie .TextAsHtml(), która automatycznie łączyła wzmianki, skróty i hiperlinki.

Czy ktoś wie, czy taka funkcja istnieje w LinqtoTwitter? Wszelkie wgląd w to, w jaki sposób TweetSharp był w stanie to osiągnąć, byłby bardzo przydatny.

UPDATE:

Wygląda jakby TweetSharp stosować wyrażenia regularne pasujące adresy URL, wspomina, tagi i cebulą. Oto przykład:

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

Nie możesz użyć metody "regex replace" na _parseHashtags? –

Odpowiedz

17

Oto moje ostateczne rozwiązanie, które wykorzystuje pewną logikę z biblioteki TweetSharp użytkownika. Ładnie działa:

/// <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

dzięki, to mi pomogło! – Roger

+0

Zdecydowanie działa dla mnie. Dziękuję bardzo! – Nickmaovich

+0

Połączenie LinqToTwitter z Twoimi rozszerzeniami stanowi świetne rozwiązanie do zarządzania tweetami. – avantprime

Powiązane problemy