Import Image from Website

Sorry begginer here. I’m trying to pull in an image from any website you choose. I’m not having the problem grabbing the image off a website using

Import["www.redditcom/r/itookapicture","Images"][[2]]

The problem I’m having is creating an API that will allow you to insert any website you would like. The code I’m trying to make work is as follows

APIFunction[{"Website" ->;<|"URL" ->"String"|>},Import[#Website,"Images"][[2]]]

It is erroring out with

?FormFunction[{Website ?;?URL ?String|>},Import[Website,Images]]" is incomplete; more input is needed

Any help would be much appreciated.

The second argument to APIFunction should be a function. The most common way to give the function is as a pure/anonymous function. To make a pure/anonymous function, you are missing the apersand:

f = APIFunction[{"Website" -> <|"URL" -> "String"|>}, Import[#Website, "Images"] &]

f[<|"Website" -> "http://www.google.com"|>]

First, thank you for taking the time to reply. Second,
You are hardcoding the website in the code. How do you pass any website into the API?

I don’t believe I am hardcording the website into the code. Let me rewrite and explain the code a bit.

This line defines an APIFunction expression called “f”. The APIFunction “f” takes an argument called Website which it will interpret as a “SemanticURL”. This means it’ll convert input like “Facebook” automatically to “https://www.facebook.com/”. It tries to convert anything reasonable into a website url. The second argument is a function that takes that URL and does something with it. The third argument specified how the function should be returned. In this case, it’ll be returned as a “JPEG”:

f = APIFunction[{"Website" -> "SemanticURL"}, First@Import[#Website, "Images"] &, "JPEG"]

Here is an example how we would call f in Mathematica (running locally on your computer). In this case we call f with google:

f[<|"Website" -> "http://www.google.com"|>]

or

 f[<|"Website" -> "google"|>]

We can deploy f to the wolfram cloud with CloudDeploy. This will allow us to run the APIFunction in the cloud as an API:

myDeployment = CouldDeploy[f]

This will return a CloudObject with a url. If you go to the URL by clicking it, you’ll see some instructions on how to use it. Let’s say that the URL was:

https://www.wolframcloud.com/objects/SomeString

We could call the API with this URL. It says to call the function with the value “Website” → “Google”:

https://www.wolframcloud.com/objects/SomeString?Website=Google


The bad news is that the Import function doesn’t seem to work right now on the cloud side. So this example won’t work until that’s fixed - I’m not aware of a workaround. I’ve forwarded the issue to the relevant people to make sure they’re aware of it. I included the code above to hopefully give a better example of how APIFunction works.

Ah ok. I already tried both
CloudDeploy[f] and the ?Website=
and I could not get it to work. I’m glad that you told me that it was broken. Again, thanks for taking your time to explain it to a newbi like me.

This still seems to be an issue, see http://mathematica.stackexchange.com/q/97751/1635 Any ideas for a workaround when you want a program to run in the cloud, gather data from a website x that is a parameter to the program?

Import HTML images

Deploy and test Sean Clarke’s API:

With[{cloudobject = FileNameJoin[{$CloudRootDirectory, "ImportHTMLImageAPI"}]},
   CloudDeploy[
      APIFunction[{"Website" -> "SemanticURL"}, First@Import[#Website, "Images"] &, "JPEG"],
      cloudobject, Permissions -> "Public"];
   URLExecute[URLBuild[cloudobject, {"Website" -> "google"}]]]

For the above query “google” the API returns:

ImportHTMLImageAPI.jpg

Eventually, a better solution might be to import HTML images from a private cloud.

Update 12/18/15

Previous update on 12/14/15 confirmed Sean Clarke’s API was working, but the CloudObject had default “Private” permissions, which works fine from a browser signed into the Wolfram Development Platform, but gives an signin error from URLExecute.

Import of website images now works from a cloud deployed APIFunction, FormPage, etc. Last week my reply to Sean Clarke’s post missed this fact, but I’ve updated it to show that his API function is now working.