2011-10-21 3 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