Workflow (iOS): A Workflow to Download Images (Jay Mantri)24 Jan 2015
I’ve been experimenting a lot recently with the iOS Workflow application and whilst it is an initial release it has a lot of power and a vast amount of potential. Whilst there are a number of features I’m hoping will be included in upcoming releases I have a few workflows that are already saving me some time and one of those relates to a site called jaymantri.com. The site offers up a set of photographs for download each week and when the e-mail comes to let me know they’re available I dutifully went to the site to download each of them from. But now with Workflow I can grab them from across the site with the push of a button.
Downloading a set of images from a page on a web site is a pretty standard sort of process for Workflow, but in this instance it wasn’t going to quite work for me. Each week jaymantri.com puts seven images online which are free to download and use for pretty much any purpose you choose - and I find it useful to have a stash of high quality stock images available. Whilst the images are all put on the home page of the site, the images are not actually the highest resolution images available. Each image has a download link beneath it which is the link to that version. So rather than downloading all of the images from the page (which would also get me any other site images as well) what I wanted to do was download each of the images from the download link.
Below you’ll be able to download the workflow and see the steps laid out, but I thought I’d also explain some of how I’ve configured it as there are some useful principles.
The first action I start off with is to specify a variable NumImagesToFetch to be “7”. This means should the web site ever change the number of images they make available (or I miss downloading the set one week), this is really easy to change as it is right at the top of the workflow.
The next step simply initialises a variable called NumberImagesFetched to “0” as I’m going to use this as the counter for getting the images I actually want.
With the variables set, the next action sets the URL for the Jay Mantri web site. At the moment the URL for the site isn’t going to change, but if later down the line I want to merge a few retrieval workflows for various sites (I’ve been considering Unsplash.com for example), I might move this up higher and set a variable based upon a user selection.
The URL is passed into an action to an action to get the contents of the URL (note this is a URL not a web page being passed in). Another action then collects together all of the hyperlinks within the page and puts them into a list.
Now I don’t want to download every linked file from the page necessarily. Just the latest seven images. But the initial process is to loop through and process every link in the list just generated. On the site, every image download rather than being a direct path is instead a URL shortened by the bit.ly link shortener service. Knowing this I can search within each URL for the string of characters “bit.ly”. However just in case this has the potential to show up elsewhere in the string I search for it pre-fixed with “://” and suffixed with “/” (i.e. “://bit.ly/”) as this will ensure that it is specifically a bit.ly shortened link. When a match is found, the UrlCurrentImage variable is set (to the bit.ly link).
The next step checks if we’ve reached our requisite number of image downloads. If we have no further action will be taken and the URL will be ignored (as will all subsequent URLs the workflow encounters). If we haven’t reached that point (i.e. if NumImagesFetched does not contain NumImagesToFetch | is in effect equal to).
When the two are not equal, the current image URL just saved in a variable is retrieved and the workflow then fetches the contents of that URL. This should be a high resolution image which is then stored in the ImgCurrent variable. I’m storing the image at this point because I want to name he image very specifically.
With the image still as the input for processing in the workflow, the Get Name action is then used to get the file name. The files usually have relatively meaningful names so it is useful to find out what that is. Because the files I save will ultimately get sorted out into a stock image collection I also like to prefix the names with an identifier to indicate what web site they came from. In this particular case, the file name is prefixed with “mantri-“ and saved to the FilenameCurrentImage variable. The image is then retrieved and the name set to the file name just created.
I use a number of cloud storage systems, but Workflow currently supports only iCloud and Dropbox. Since Dropbox is cross platform and I often do my stock image sorting on a Windows laptop, the sensible place to export them is to a folder within Dropbox (in this case a folder called “Mantri” and located in the root of my Dropbox folder).
At this point the image has been safely retrieved and so the number of images fetched is incremented by one and saved back to the variable.
Because the images are relatively high resolution, they can be several megabytes. This can take a little while to download. Because of this and because I like to get some sort of sense of what is being downloaded I added a couple of actions here to describe to me what has just happened in the process. Workflow’s speak action let’s me know what number image has just been saved and also what the file name is (as it might give me a hint as to what the image is of).
Once all of the images have been downloaded, the repeat action loop will complete and the next action is a Wait action. This action pauses the workflow for three seconds. This is roughly long enough to allow the speak action on the final image file to complete and the ignoring of any remaining URLs to take place. The reasons this is important is that the last thing the workflow does is announce it is complete and without that pause the completion statement is lost to the previously running speech related action.
So that’s how the workflow should work and if you would like to read through it, you can see a screen shot of all of the actions below.
If you are on an iOS device with the Workflow app installed, you can tap here to install the Jay Mantri Workflow.