6
$\begingroup$

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"?

$\endgroup$
  • $\begingroup$ I don't see any possible options in ServiceExecute that could work. Have you tried making a custom function to download straight from the internet page of the tweet? $\endgroup$ – Emy Mar 11 '15 at 14:49
7
+250
$\begingroup$

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]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.