TweetSharp Entities

In the latest preview of TweetSharp, the guys have introduced support for a Twitter feature known as "entities". The idea is that when you ask Twitter for a tweet (or a list of them) it doesn't just return the text of the tweet, it also returns a list of special items that occur inside the text: specifically hashtags, mentions and links.

You access the entities inside a tweet using the TwitterStatus.Entities property, which is of type TwitterEntities. It looks something like this:

public class TwitterEntities
{
    public virtual IEnumerable<TwitterMention> Mentions { get; set; }
    public virtual IEnumerable<TwitterHashTag> HashTags { get; set; }
    public virtual IEnumerable<TwitterUrl> Urls { get; set; }

    public IEnumerable<TwitterEntity> Coalesce() { /* ... */ }
}

So for any given status you can retrieve a list of mentions, hashtags and URLs. You can also call Coalesce to return an ordered list of all the entities within a tweet, although that's not necessary because the class itself implements IEnumerable<TwitterEntity>.

This makes it insanely easy to turn a TwitterStatus object into, say, a WPF TextBlock. Here's the code I'm using right now. You can use this in a ViewModel or even an IValueConverter for binding:

    public static TextBlock GetContentTextBlock(this ITweetable item)
    {
        var i = 0;
        var text = item.Text;

        var textBlock = new TextBlock
        {
            TextWrapping = System.Windows.TextWrapping.Wrap,
        };

        foreach (var e in item.Entities)
        {
            if (e.StartIndex > i)
            {
                textBlock.Inlines.Add(new Run(WebUtility.HtmlDecode(text.Substring(i, e.StartIndex - i))));
            }
            i = e.EndIndex;

            switch (e.Type)
            {
                case TwitterEntityType.HashTag:
                    textBlock.Inlines.Add(CreateSearchLink(((TwitterHashTag)e).Text));
                    break;

                case TwitterEntityType.Mention:
                    textBlock.Inlines.Add(CreateUserLink((TwitterMention)e));
                    break;

                case TwitterEntityType.Url:
                    textBlock.Inlines.Add(CreateUriLink((TwitterUrl)e));
                    break;
            }
        }

        if (i < item.Text.Length)
        {
            textBlock.Inlines.Add(new Run(WebUtility.HtmlDecode(text.Substring(i))));
        }
        return textBlock;
    }

So the idea is that we iterate through the known entities in a tweet, and add them to the TextBlock as Hyperlinks (via helper methods that I won't list here). We also insert any text that lives before, between or after entities.

If there are no entities at all in the text, the foreach block does nothing and the entirety of the text is simply added to the TextBlock.

The best part of TweetSharp's support for entities is that they've added the Entities property to their ITweetable interface, which means you can get to it for statuses, direct messages and search results. Twitter doesn't actually supply the information for the last two - TweetSharp is doing the hard work for us!

The addition of the Entities property has, as you could imagine, vastly simplified my code for turning a tweet into a TextBlock. Removing code is my favourite kind of refactoring! Thanks TweetSharp!

tweetsharp twitter c# .net wpf
Posted by: Matt Hamilton
Last revised: 25 Nov, 2017 05:33 AM History