如何使用 LinqToTwitter 获取推文的 HTML?

发布于 2024-12-11 19:31:05 字数 774 浏览 0 评论 0原文

我最近从 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);

I recently switched from TweetSharp to LinqToTwitter and the one thing I'm missing is a way to retrieve a tweet as HTML.

TweetSharp had a method called .TextAsHtml() which automatically linked mentions, hash tags, and hyperlinks.

Does anyone know if such a feature exist in LinqtoTwitter? Any insight into how TweetSharp was able to accomplish this would be much appricated.

UPDATE:

It looks as though TweetSharp used Regular Expressions to match URLs, mentions, and hash tags. Here is a sample:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2024-12-18 19:31:05

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

Here is my final solution which uses some logic from TweetSharp's library. It's working out nicely:

/// <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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文