5

I'm trying to implement a feature in a .Net WPF application to automatically display photos from twitpic when users post them to my hashtag.

I'm using the Twitterizer2 API library, all the tweets coming from my search have their links as t.co links (I think this is from Twitter). I don't know how to parse these links to get the images to send them to the users.

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
KamalSalem
  • 495
  • 2
  • 8
  • 21

4 Answers4

2

You can rely on the media entities data that is returned by Twitter.

You can see a sample and explaination that I posted a while back here: http://www.twitterizer.net/528/finding-urls-hashtags-and-mentions-using-entities/

The sample doesn't include it, but there is a newer TwitterMediaEntity class that gives information on images and videos.

Ricky Smith
  • 2,379
  • 1
  • 13
  • 29
0

I used something like this (with the Twitterizer nuget package)

li.Text = tweet.text;
foreach(Twitterizer.Entities.TwitterEntity te in tweet.Entities)
{
    if(te.GetType() == (typeof(Twitterizer.Entities.TwitterMediaEntity)))
    {
        var b = (Twitterizer.Entities.TwitterMediaEntity) te;
        li.Text += "<img src=\"" + b.MediaUrl + "\" />";
    }
}
JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
0

all attach media and audio URL in any tweet will be in entities\media and media will be array of items that attach with the post if you want to get path of first attached item as suppose you have your tweets saved in enumerableTwitts your could will be

foreach (dynamic t in enumerableTwitts)
            {
 dynamic media = t["entities"]["media"];
                        string media_url = media[0]["media_url"].ToString();
}

you should use try-catch because some of tweets don't have attach files

Hussein Alrubaye
  • 915
  • 8
  • 14
0

I don't know how Twitterizer2 API works but I think you could solve your problem in two ways:

  1. Search in Official Twitter API something that helps you to parse such links (look at Tweet Entities);
  2. Download the content of that URL yourself and parse it;

For the second approach I would suggest something like the function below (this parsing is based on the first picture I have found on my followed people tweets so it works only for TwitPic images):

private static Uri GetPicture(string twitterUri)
{
    using (var webClient = new WebClient())
    {
        string html = webClient.DownloadString(twitterUri);
        int imgIndex = html.IndexOf("<img class=\"photo\" id=\"photo-display\"");
        int srcStartIndex = html.IndexOf("src", imgIndex) + 5;
        int srcEndIndex = html.IndexOf("\"", srcStartIndex);
        string imgSrc = html.Substring(srcStartIndex, srcEndIndex - srcStartIndex);
        return new Uri(imgSrc);
    }
}

As you could imagine the usage is:

Uri imgUri = GetPicture("http://t.co/RQu9hZn8"); // this is a real image
as-cii
  • 12,819
  • 4
  • 41
  • 43
  • This will work (for twitpic), but be aware that you will be making web calls for each image and that will take time and bandwidth to sort out. – Ricky Smith Oct 08 '11 at 17:50
  • How would you download an image without making a webcall? I have just proposed a method to extract them from an *encoded* link. How he handles *images showing* is up to him :) – as-cii Oct 08 '11 at 18:00
  • If you're counting the web call to download the image data, then your code would perform 2 webcalls for each image. What you are doing wouldn't extract the image data, but the url to the image, which the client would have to make another call to that url to download. That is what I'm talking about, you're making 1 extra web call. I posted a link to sample code that will get the url directly from twitter. The data is already included in the results. – Ricky Smith Oct 11 '11 at 02:13