Skip to content

Instantly share code, notes, and snippets.

@blech
Created June 29, 2011 05:09
Show Gist options
  • Save blech/1053190 to your computer and use it in GitHub Desktop.
Save blech/1053190 to your computer and use it in GitHub Desktop.
Create a Flickr "popular" set
#!/usr/bin/python
import flickrapi
import simplejson
# here culty culty cargo cargo culty
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
# Flickr API keys. Get your own: http://www.flickr.com/services/apps/create/apply/
keys = {
'flickr_key': "",
'flickr_sec': "",
}
# some constants. you'll want to change these, because these are mine.
# you know, if I were helpful I'd use flickr.photosets.getList, but...
# I'm lazy and I just copied and pasted from a URL
faveset_id = "72157594229306612";
faveset_primary = "262062926"; # because it's a star
# set up Flickr API
flickr = flickrapi.FlickrAPI(keys['flickr_key'], keys['flickr_sec'], cache=True)
flickr.cache = flickrapi.SimpleCache(timeout=300, max_entries=200)
(token, frob) = flickr.get_token_part_one(perms='write')
if not token: raw_input("Press ENTER after you authorized this program")
flickr.get_token_part_two((token, frob))
# get most favourited photos
rsp = flickr.stats_getPopularPhotos( format='json',
nojsoncallback='1',
sort='favorites',
per_page='81',
)
info = simplejson.loads(rsp);
# debugging info
# for photo in info['photos']['photo']:
# print "%s: %s %s %s" % (photo['id'], photo['title'], photo['stats']['favorites'], photo['ispublic']);
# build photo ID list
photo_id_list = [photo['id'] for photo in info['photos']['photo']]
photo_ids = ",".join(photo_id_list);
# print photo_ids;
# reorder set
rsp = flickr.photosets_editPhotos( format='json',
nojsoncallback='1',
photoset_id=faveset_id,
primary_photo_id=faveset_primary,
photo_ids=photo_ids,
)
info = simplejson.loads(rsp);
if info['stat'] == "ok":
print "Done!"
else:
print "Error: ".rsp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment