Skip to main content

serviceexecute - How can I download a tweeted image given the tweet id?


ServiceExecute[] supports sending an image via "ImageTweet" i.e. posting an image in a tweet. But how can I achieve the reverse of this and download the image given the "TweetID"?




Answer



I'm sure the internet used to be simpler than this! Anyway, this worked for the two examples I tried:


tweetid = "493792023691149312";

s = ServiceConnect["Twitter"];

tweet = ServiceExecute[s, "GetTweet", "TweetID" -> tweetid];

link = Last @ Flatten @ StringCases[StringSplit[tweet], "http" ~~ __];


manylinks = Import[link, "Hyperlinks"];

anotherlink = First @ Flatten @ StringCases[manylinks, __ ~~ "photo" ~~ __];

xmldata = Import[anotherlink, "XMLObject"];

finallink = First @ Cases[xmldata, XMLElement[
"meta", {"property" -> "og:image", "content" -> address_}, {}] :> address, -1];

Import[finallink]

Comments