Downloading Media from WordPress using AppleScript

I got a request from a teacher who wanted to download a years worth of images from a Glow Blog (for end of year slideshow).
Although there are plugins that can do this these are not available on Glow Blogs. I was stumped apart from going through the site and downloading them 1 by 1. But after a wee bit of thinking I though I’d try using the REST API via AppleScript.
The REST API will list in JSON format the media:
http://johnjohnston.info/blog/wp-json/wp/v2/media/
Look at that in FireFox for a pretty view.
JSON Helper is
an agent (or scriptable background application) which allows you to do useful things with JSON directly from AppleScript.
So I can grab the list of media from a site in JSON format use appleScript to download all the files.
The script I wrote is not great, you can’t download from a particular year, but a quick look at the JSON will help in working out how many files to download.
I am sure there are more efficient ways to do this and I’ve only tested on a couple of site, but it seems to do the trick and might be useful again sometime.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

–needs json helper for apple script Free from mac app store

–https://itunes.apple.com/gb/app/json-helper-for-applescript/id453114608?mt=12

set downloadfolder to POSIX path of (choose folder with prompt "Please select an output folder:")

set pagecountstart to 1

set pagecountmax to 1

set perpage to 10 –MAX =100

set blogurl to "FILL-IN-THE-URL"

if blogurl = "FILL-IN-THE-URL" then

display dialog "You need to edit the script to fill in a url" buttons "OK" default button "OK"

return false

end if

–We could also limit media to a particular type

–http://v2.wp-api.org/reference/media/

–Or filter in various ways

repeat with pagecount from pagecountstart to pagecountmax

tell application "JSON Helper"

set bURL to blogurl & "/wp-json/wp/v2/media/?per_page=" & perpage & "&page=" & pagecount

set thejson to fetch JSON from bURL

set theimgs to {}

set imageCount to count of thejson

repeat with n from 1 to imageCount

set end of theimgs to source_url of item n of thejson

end repeat

end tell

repeat with img in theimgs

set cmd to "curl -L " & img & " > " & downloadfolder & filenamefromurl(img)

do shell script cmd

end repeat

end repeat

on filenamefromurl(theurl)

set s to "url=\"" & theurl & "\"; echo \"${url##*/}\""

return do shell script s

end filenamefromurl

view raw

downloadWordPressMedia.scpt

hosted with ❤ by GitHub