Created
November 11, 2010 02:00
-
-
Save bcse/671861 to your computer and use it in GitHub Desktop.
FlickrBackup
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
from __future__ import with_statement | |
import glob | |
import os.path | |
import re | |
import urllib | |
import flickrapi | |
api_key = '________________________________' | |
api_secret = '________________' | |
user_id = '____________' | |
flickr = flickrapi.FlickrAPI(api_key, api_secret) | |
(token, frob) = flickr.get_token_part_one(perms='read') | |
if not token: raw_input("Press ENTER after you authorized this program") | |
flickr.get_token_part_two((token, frob)) | |
photosets = flickr.photosets_getList(user_id=user_id) \ | |
.find('photosets').findall('photoset') | |
for photoset in photosets: | |
photoset_id = photoset.attrib['id'] | |
photoset_count = photoset.attrib['photos'] | |
photoset_title = re.sub(r'[\\/:*?"<>|]', '_', photoset.find('title').text.strip()) | |
photodir = os.path.join('D:\\', 'Downloads', photoset_title) | |
if not os.path.exists(photodir): | |
os.mkdir(photodir) | |
# check if this dir is already walked | |
current_count = len([f for f in os.listdir(photodir) \ | |
if re.match(r'.+?\.(jpg|png|gif)', f, re.IGNORECASE)]) | |
if not int(photoset_count) > int(current_count): | |
continue | |
photos = flickr.walk_set(photoset_id=photoset_id, per_page=50, extras='url_o') | |
for photo in photos: | |
filename = os.path.join(photodir , re.sub(r'[\\/:*?"<>|]', '_', photo.attrib['title'].strip()) + os.path.splitext(photo.attrib['url_o'])[1]) | |
if not os.path.exists(filename): | |
with open(filename, 'wb') as f: | |
# TODO: First, open a temp file. The rename it to the filename after downloaded. | |
img = urllib.urlopen(photo.attrib['url_o']) | |
f.write(img.read()) | |
img.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment