- Manual Flickr Photo Insertion Problem
- Automatic Flickr Photo Insertion Solution Introduction
- API Key Setup
- Get user_id and album_id
- Using the Python Script
- Code Explanation
- Line 14-22: basic library import, id setup and file name setup.
- Line 24-30: Flickr REST api construction
- Line 34-37: Executing API and read the return value
- Line 40-42: XML parsing
- Line 50-54: Building dictionary for sorting
- Line 57-64: Sort dictionary by date taken and output embed codes
- Also Recommended:
- Like My Post? Subscribe Below and Get Latest Post Email Updates!
Manual Flickr Photo Insertion Problem
Many photo-heavy posts contain lots of photos (150+) stored on image servers like Flickr. It is time consuming to copy-and-paste the photo’s sharing urls to the post.
I often have lots of photos needed to insert into wordpress!
Furthermore, Flickr albums do not by default order photos by date-taken. Thus I also wasted some time manually sorting out the photos, sometimes causing few photos not being included in the post.
Automatic Flickr Photo Insertion Solution Introduction
Here we introduce an automatic way to speed up Flickr photo insertion process. Although here we aim at Flickr, the same principle can be applied to other major online image storage services, as long as they provide similar APIs.
The solution summary is as follows:
- Apply for an API key on Flickr.
- Find user id and the album id from your Flickr account.
- Use 1. and 2. to construct Flickr API that grab information of photos from a particular album, in xml format.
- Use python to process raw data into wordpress photo embed codes, photos ordered by date taken.
- Copy these codes into wordpress’ post editor.
Doing so automates the Flickr photo insertion process, so we can focus on adding words/headings to explain these photos.
Steps 2.-4. are done by a python script which you can find it here on gist.
API Key Setup
Flickr app creation page.
Go through the instructions and agreements you should get your api key right away. If you forgot your api key, again search “Flickr App Garden” and at “Your Apps” on right, click “Apps By You”. The key should be visible right under your app:
This is where to find your api key.
Get user_id and album_id
These two ids can be found in the urls of your Flickr account. Click on
You -> Photostream, and the url has the form:
Similarly, click on any of your albums, the url has the form:
You now have all the information for running the above python script!
Using the Python Script
You first must edit the file to plugin your three ids on line 17,18,19. Then you can run the script (on Linux it is simply the command:
python get-flickr-album-photos.py ).
The script outputs a file called
flickr_url.txt with text like this:
Generated file content.
Copy these into the post editor and you’ll get all the photo images rendered immediately! No longer need to copy and paste all the time!
Here is a brief explanation of the code in case you are curious about how it works.
Line 14-22: basic library import, id setup and file name setup.
Line 24-30: Flickr REST api construction
The main method use is
flickr.photosets.getPhotos (line 21,26). It returns photo information from a photo set, which is what an album is. The code
extras=date_taken asks Flickr to return also the date taken of these photos so we can order them by date.
Line 34-37: Executing API and read the return value
Here we execute the api and get returned raw XML data.
Line 40-42: XML parsing
Here we use the library minidom to parse the XML file and get a list of photo data.
Line 50-54: Building dictionary for sorting
We use python’s dictionary (line 50. A set of key-value pairs) to build
photo_id pairs. Here the values are extracted by minidom’s method (line 52,53).
Building this dictionary makes it easy to sort the photo data by date taken. We have to do the sort ourselves as Flickr api cannot sort the data in advance.
Line 57-64: Sort dictionary by date taken and output embed codes
We first build the base url for each photo (line 57) and open the output file (file 58). Then we sort the dictionary by key (line 60
sorted(myDict) ) and access each key-value pair (line 60).
At each pair we use the
photo_id and base url to build a wordpress embed code set, and write it to the output file. After we have gone through every pair we close the file (line 64).