Skip to content

Instantly share code, notes, and snippets.

@yaulaannl
Created April 1, 2017 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaulaannl/277a27e29b31bdb64a68ca0b529be521 to your computer and use it in GitHub Desktop.
Save yaulaannl/277a27e29b31bdb64a68ca0b529be521 to your computer and use it in GitHub Desktop.
Python script to get wordpress embed codes for a Flickr album
# Getting Flickr's photos in an album
# Input Parameters:
# api_key
# user_id
# photoset_id
#
# Output:
# A text file with many wordpress embed codes
#
# steps:
# 1. Build the Flickr endpoint string
from xml.dom import minidom
import urllib2, json
album_id = "your-album-id"
api_key = "your-api-key"
user_id = "your-user-id"
f_method = "flickr.photosets.getPhotos"
outfName = "flickr_url.txt"
# Build url string
url = "https://api.flickr.com/services/rest/?"
url = url + "method=" + f_method
url = url + "&photoset_id=" + album_id
url = url + "&api_key=" + api_key
url = url + "&user_id=" + user_id
url = url + "&extras=date_taken" #add date taken data
print "Flickr url: " + url
# get method to get xml response
res = urllib2.urlopen(url)
rawdata = res.read()
res.close()
#parse xml
dom = minidom.parseString(rawdata)
#get photos xml
photos = dom.getElementsByTagName('photo')
print "Number of photos: " , len(photos)
#print photos[0].toxml()
#print photos[0].attributes["id"].value
# sort returned data
# strategy: try dictionary
myDict = {}
for photo in photos:
date = photo.attributes["datetaken"].value
id1 = photo.attributes["id"].value
myDict[date] = id1
#print out photo urls
baseUrl = "https://www.flickr.com/photos/" + user_id + "/"
f = open(outfName, 'w');
#sort dictionary
for key in sorted(myDict):
outS = "[embed]" + baseUrl + myDict[key] + "[/embed]\n"
f.write(outS)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment