【问题标题】:How to get tweet's HTML with LinqToTwitter?如何使用 LinqToTwitter 获取推文的 HTML?
【发布时间】:2011-10-21 21:15:32
【问题描述】:

我最近从 TweetSharp 切换到了 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);

【问题讨论】:

  • 你不能在 _parseHashtags 上使用正则表达式替换方法吗?

标签: c# asp.net twitter tweetsharp


【解决方案1】:

这是我的最终解决方案,它使用了 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;
    }
}

【讨论】:

  • LinqToTwitter 和您的扩展程序的组合是管理推文的绝佳解决方案。
猜你喜欢
  • 2016-04-28
  • 2017-07-21
  • 2018-11-05
  • 1970-01-01
  • 2020-01-16
  • 2018-02-10
  • 1970-01-01
  • 2014-03-05
  • 1970-01-01
相关资源
最近更新 更多