Created
August 7, 2013 03:44
-
-
Save vmassuchetto/6171017 to your computer and use it in GitHub Desktop.
Import Flickr images from one account to another based on text files links.
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
#!/usr/bin/python | |
FILES_READ_PATH = '/path/to/txt/files' | |
FILES_WRITE_PATH = '/where/to/save/images' | |
FROM_USER = 'you_username' | |
FROM_FLICKR_API_KEY = 'some_key' | |
FROM_FLICKR_API_SECRET = 'some_secret' | |
TO_USER = 'your_username' | |
TO_FLICKR_API_KEY = 'some_key' | |
TO_FLICKR_API_SECRET = 'some_secret' | |
import sys | |
import os | |
import re | |
import flickrapi | |
import urllib2 | |
def get_files(initial): | |
paths = (os.path.join(root, f) for root, dirs, files in os.walk(initial) | |
for f in files) | |
return (path for path in paths) | |
# Objects | |
from_flickr = flickrapi.FlickrAPI(FROM_FLICKR_API_KEY, FROM_FLICKR_API_SECRET) | |
to_flickr = flickrapi.FlickrAPI(TO_FLICKR_API_KEY, TO_FLICKR_API_SECRET) | |
# Tokens | |
(to_token, to_frob) = to_flickr.get_token_part_one(perms='write') | |
if not to_token: raw_input("Press ENTER after you authorize") | |
to_flickr.get_token_part_two((to_token, to_frob)) | |
for f_path in get_files(FILES_READ_PATH): | |
print 'Scanning ', f_path | |
f_handler = open(f_path, 'r') | |
f_content = f_handler.read().decode('utf-8') | |
f_regex = '(http[s]?://(www\.)?flickr\.com/photos/' + FROM_USER + '/([0-9]+))' | |
f_urls = re.findall(f_regex, f_content) | |
flickr_urls = [] | |
for f_url in f_urls: | |
url = f_url[2] | |
# File info | |
fileinfo = False | |
while not fileinfo: | |
try: | |
f = from_flickr.photos_getInfo(photo_id=url).find('photo') | |
except: | |
# Invalid photo | |
continue | |
fileinfo = True | |
t = f.find('title').text | |
d = f.find('description').text | |
d = d if d else '' | |
p = f.attrib | |
# File names | |
p_filename = '%s_%s_o.%s' % (p['id'], p['originalsecret'], p['originalformat']) | |
p_base = 'http://farm%s.staticflickr.com/%s/%s' | |
p_url = p_base % (p['farm'], p['server'], p_filename) | |
p_filepath = FILES_WRITE_PATH + p_filename | |
# Download | |
print ' Downloading ', p_url | |
if not os.path.exists(p_filepath): | |
download = False | |
while not download: | |
try: | |
p_bin = urllib2.urlopen(p_url) | |
except: | |
continue | |
download = True | |
output = open(p_filepath, 'wb') | |
output.write(p_bin.read()) | |
output.close() | |
# Upload | |
print ' Uploading ', p_filepath | |
upload = False | |
while not upload: | |
try: | |
u = to_flickr.upload( | |
filename = p_filepath, | |
title = t, | |
description = d, | |
format = 'etree') | |
except: | |
continue | |
upload = True | |
u_id = u.find('photoid').text | |
# Uploaded file info | |
fileinfo = False | |
while not fileinfo: | |
try: | |
f = from_flickr.photos_getInfo(photo_id=u_id).find('photo') | |
except: | |
continue | |
fileinfo = True | |
u_t = f.find('title').text | |
u_d = f.find('description').text | |
u_d = u_d if u_d else u_t | |
u = f.attrib | |
u_url = 'http://farm%s.staticflickr.com/%s/%s_%s.jpg' % \ | |
(u['farm'], u['server'], u['id'], u['secret']) | |
u_img = '<a title="%s" href="http://flickr.com/%s/%s/"><img alt="%s" src="%s" /></a>' % \ | |
(u_d, TO_USER, u_id, u_d, u_url) | |
match = re.compile(ur'<.*%s.*>' % url, re.UNICODE) | |
new_content = match.sub(u_img, f_content) | |
f = open(f_path, 'wb') | |
f.write(new_content.encode('utf-8')) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment