Gallery restoration status at 70 percent
I spent a good chunk of today trying to figure out if I could automate some of the work of rebuilding my galleries. Answer: yes, though it took some research to learn how to do!
Because here’s the thing y’all: I had several elements in place that would allow for at least some of the restoration work to be done in a scripted fashion rather than manually.
Namely:
- A known directory structure
- One or more screenshot files in each directory in the structure
- A known file called metadata.json in each directory, which contained the metadata for all of the screenshots in the same directory, and which contained a known structure that I should be able to parse
And here’s the process I’ve been following to rebuild the galleries:
- Work my way forward in chronological order through the broken galleries
- For each one, find the corresponding directory on the server
- Manually import each screenshot in that directory into the WordPress media library
- Review contents of metadata.json, and copy and paste all of the relevant data out of that file onto the files added to the media library
- Using the FileBird plugin, create a new folder in the media library to contain the added files
- Using the FooGallery plugin, create a new gallery using the files in the new FileBird folder
- Add the newly created gallery to the relevant album of galleries, and remove the broken one
- Update the gallery shortcode in the relevant playthrough post from the broken gallery to the new replacement
So, a lot of steps here, right?
The problem I wanted to solve was automating steps 3 and 4, because the most time-consuming part of this process has been adding hundreds and hundreds of files into my media library, and copy and pasting the metadata in for them. (And as a reminder: these files weren’t in the media library already, because the broken galleries had been previously reading all the relevant files and data right off the server file structure. Since I can’t do that now, I have to pull all of this stuff directly into the WordPress media library.)
And solving this problem required doing these major things:
- Installing wp-cli, the command line tool for managing WordPress installations
- Learning how to use the wp media import command, including how to make it work with a multi-site installation, which is what I have; Anna Plays Skyrim is part of a multi-site WordPress network
- Learning how to use jq, which is a command line JSON parser
I threw some threads out on Mastodon today on the WordPress hashtag, asking for recommendations on how to accomplish what I wanted to do. So now I have a two-command solution to do steps 3 and 4 above, like so.
For step 3, I’m running this scary-looking thing which basically chugs through metadata.json in the current working directory, and spits out a full “wp media import” command for every file covered in the JSON file. Then it saves that list into temp.sh.
jq -r '.items[] | @sh "wp media import \(.file) --caption=\(.caption) --title=\(.caption) --desc=\(.description) --alt=\(.alt) --url=https://skyrim.annathepiper.org/"' metadata.json > temp.sh
Then I need to run those commands:
sh temp.sh
Lastly, I pop into the media library and confirm that:
- All the files in the working directory now appear in the media library
- All the metadata on the files is correct (and I’m correcting typos where I spot them)
Then I can proceed to making the FileBird directories and the new galleries.
So this is still taking some effort; it’s not just a matter of doing a mass import of the entire directory structure. Which I’m actually okay with, on the grounds that having to go through each directory is letting me doublecheck that the files and their metadata are getting imported okay. And look for any typos I want to fix as well.
As of this writing, I’m now at about 70 percent complete on the restoration effort. The Faanshi Playthrough and Shenner Playthrough galleries are now done.
Galleries now in active progress of being fixed are the ones for Gyllerah, Tembriel, Harrowhark, and Kendis.
More on this tomorrow. And thanks to everybody on Mastodon who offered advice!