2
$\begingroup$

The new function ServiceExecute seems to suggest the ability to download images containing a specific hashtags. Can anyone get this to work?

$\endgroup$
4
  • $\begingroup$ Specifically what part of the documentation seems to suggest this ability? $\endgroup$
    – Mr.Wizard
    Commented Jan 22, 2015 at 18:52
  • $\begingroup$ Very closely related: 71603 $\endgroup$
    – C. E.
    Commented Jan 22, 2015 at 19:45
  • $\begingroup$ @Pickett how is this solution applicable to TweetIds? $\endgroup$
    – M.R.
    Commented Jan 24, 2015 at 22:59
  • $\begingroup$ @M.R. It isn't, because it doesn't answer your question about Twitter. But if it would answer the question for Twitter as well, then it would almost certainly answer your other question as a side-effect. $\endgroup$
    – C. E.
    Commented Jan 24, 2015 at 23:18

1 Answer 1

3
$\begingroup$

For Instagram you first have to initiate the service:

instagram = ServiceConnect["Instagram"]

This will open a dialog window where you have to click Sign in to Instagram. That will take you to Instagram API authentication website where you have to follow the steps to get an access key. Paste this access key in the Mathematica dialog.

Once you are authenticated, to get images by hashtag as you said, you can do:

instagram["TaggedMedia", {"Tag"->"cat"}]

Or

ServiceExecute["Instagram", "TaggedMedia", {"Tag" -> "cat"}]

The same idea should work for Twitter, however, I don't see a request to get images by hashtag there.

Look for ListingOfSupportedExternalServices in Mathematica help to get more details.

In response to your last question

Thanks, last question, how can I get more results, I'm trying to make a list of 2k images with apple logos in them....

According to Instagram API documentation, there's a COUNT, MIN_TAG_ID and MAX_TAG_ID parameters to deal with pagination. However, it seems Mathematica "TaggedMedia" request doesn't support that and it's limited to 20 images by default.

A solution I find is using what "TaggedMedia" uses to get the images. When you do

instagram["TaggedMedia", {"Tag" -> "macbook"}]

It seems to be doing something like

Import[("url"/.("thumbnail"/.("images"/.#)))]&/@("data"/.instagram["RawRecentTaggedMedia", {"Tag" -> "macbook"}])

So, let's do

data = instagram["RawRecentTaggedMedia", {"Tag" -> "macbook"}];

To get the URLs of the images you can do

"url" /. ("thumbnail" /. ("images" /. #)) & /@ ("data" /. data)

And you can actually play with resolutions using the "standard_resolution" or the "low_resolution" JSON elements.

"url" /. ("standard_resolution" /. ("images" /. #)) & /@ ("data" /. data)

"url" /. ("low_resolution" /. ("images" /. #)) & /@ ("data" /. data)

Now, if you see the "pagination" JSON element, you may find something like this

In[6]:= "pagination"/.data

Out[6]= {next_max_tag_id->1421984288411354,next_url->https://api.instagram.com/v1/tags/macbook/media/recent?access_token=xxxxxxxxx.xxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&max_tag_id=1421984288411354,deprecation_warning->next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead,min_tag_id->1421984818860187,next_max_id->1421984288411354,next_min_id->1421984818860187}

From here what you want is the "next_url" element

"next_url" /. ("pagination" /. data)

This will give you the pagination URL, i.e. the URL to call next page of images. So you can URLFetch it

ImportString[URLFetch["next_url" /. ("pagination" /. data)], "JSON"]

And with this last line you'll get the exact same structure you got in you data variable when you did instagram["RawRecentTaggedMedia", {"Tag" -> "macbook"}], but with the new set of 20 images. Now you can apply the same stuff as above to get the URLs to your desired resolution and a next_url element.

If you want to experiment a little more with Instagram API you can add the COUNT parameter mentioned in the official docs, to the next_url element to get more results. So something like

StringJoin["next_url" /. ("pagination" /. data), "&count=100"]

I don't know what's the maximum count value. I didn't find that info in the documentation. However, take note of the rate limits of Instagram Platform.

I hope this helps a bit or report back if you are still having trouble.

$\endgroup$
12
  • $\begingroup$ When I run the second command I get this error: LinkOpen::linke: Specified file is not a MathLink executable.. >> StringCases::strse: String or list of strings expected at position 1 in StringCases[Null,"data":[~~Longest[InstagramOAuthPrivatex__]~~]:>InstagramOAuthPrivatex]. >>` $\endgroup$
    – M.R.
    Commented Jan 22, 2015 at 20:38
  • $\begingroup$ What's your Mathematica version? $\endgroup$
    – xtian777x
    Commented Jan 22, 2015 at 20:39
  • $\begingroup$ My version is 10.0.1.0. is this a known issue or something? $\endgroup$
    – M.R.
    Commented Jan 22, 2015 at 20:40
  • $\begingroup$ Did you go all the way using the Sign in to Instagram button, getting an access key and pasting into the WolframConnector dialog? Also, did you try both methods I wrote? One last thing, what's the output of instagram["Requests"]? $\endgroup$
    – xtian777x
    Commented Jan 22, 2015 at 20:45
  • $\begingroup$ yes, and I clicked save. $\endgroup$
    – M.R.
    Commented Jan 22, 2015 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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