Last active
August 29, 2015 14:23
-
-
Save muratgu/b910a6222fc5f0ade26a to your computer and use it in GitHub Desktop.
Upload image to Flickr with tags and title
This file contains 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
import glob, os, sys | |
import pyexiv2 | |
import flickrapi | |
import subprocess | |
import flickrapi | |
import webbrowser | |
class MyFlickr: | |
def __init__(self, api_key, api_secret, flickr_userid): | |
self.flickr = self.connect(api_key, api_secret) | |
self.flickr_userid = flickr_userid | |
def connect(self, api_key, api_secret): | |
flickr = flickrapi.FlickrAPI(api_key, api_secret) | |
print('flickr: authenticating...') | |
if not flickr.token_valid(perms=unicode('write')): | |
flickr.get_request_token(oauth_callback='oob') | |
authorize_url = flickr.auth_url(perms=unicode('write')) | |
webbrowser.open_new_tab(authorize_url) | |
verifier = unicode(raw_input('Verifier code: ')) | |
flickr.get_access_token(verifier) | |
print('flickr: authenticating...done') | |
return flickr | |
def check_file_in_flickr(self, fn): | |
file, ext = os.path.splitext(os.path.basename(fn)) | |
tag_fn = 'file:name=%s' % file | |
for p in self.flickr.walk(user_id=self.flickr_userid, tags=tag_fn): | |
print ' flickr id=', p.get('id') | |
print ' flickr title=', p.get('title') | |
print ' flickr desc=', p.get('description') | |
pi = self.flickr.photos_getInfo(photo_id=p.get('id')) | |
print ' flickr tags=', [x.text for x in pi[0].findall('tags/tag')] | |
print ' flickr posted=', pi[0].findall('dates')[0].get('posted') | |
print ' flickr taken=', pi[0].findall('dates')[0].get('taken') | |
return 1 | |
return 0 | |
def progress_upload(self, progress, done): | |
if done: | |
print "\n Upload Complete" | |
else: | |
sys.stdout.write(" %3d%%\b\b\b\b\b\b\b\b\b\b\b\b\b" % progress) | |
def upload_file_to_flickr(self, fn, title, desc, keywords): | |
file, ext = os.path.splitext(os.path.basename(fn.lower())) | |
tag_fn = 'file:name=%s' % file | |
if not title: | |
title = file | |
tags = '' | |
if keywords: | |
for k in keywords: | |
if len(k.split())>1: tags += ' "'+k+'"' | |
else: tags += ' '+k | |
tags += ' ' + tag_fn | |
tags = tags.strip() | |
print tags | |
self.flickr.upload(filename=fn, | |
title=title, description=desc, tags=tags, is_public=0, | |
callback=self.progress_upload) | |
def process_photo(self, fn): | |
print 'file>', fn | |
metadata = pyexiv2.ImageMetadata(fn) | |
metadata.read() | |
copyright = metadata['Exif.Image.Copyright'].value if 'Exif.Image.Copyright' in metadata else None | |
print ' copyright=', copyright | |
title = metadata['Iptc.Application2.Headline'].value if 'Iptc.Application2.Headline' in metadata else None | |
print ' title=', title | |
desc = metadata['Iptc.Application2.Caption'].value if 'Iptc.Application2.Caption' in metadata else None | |
print ' desc=', desc | |
keywords = metadata['Iptc.Application2.Keywords'].value if 'Iptc.Application2.Keywords' in metadata else None | |
print ' keywords=', keywords | |
ret = self.check_file_in_flickr(fn) | |
if ret == 0: | |
self.upload_file_to_flickr(fn, title, desc, keywords) | |
def process_video(self, fn): | |
print 'file>', fn | |
copyright = None | |
title = None | |
keywords = None | |
print ' title=', title | |
print ' keywords=', keywords | |
ret = self.check_file_in_flickr(fn) | |
if ret == 0: | |
self.upload_file_to_flickr(fn, title, keywords) | |
def print_usage_and_exit(): | |
print 'Set environment vars FLICKR_API_KEY, FLICKR_API_SECRET, FLICKR_USERID' | |
print 'Then run: imgsync.py <filename>|<folder>' | |
sys.exit(-1) | |
def main(argv): | |
if len(argv) < 2: | |
print_usage_and_exit() | |
api_key = unicode(os.environ.get('FLICKR_API_KEY')) | |
api_secret = unicode(os.environ.get('FLICKR_API_SECRET')) | |
flickr_userid = unicode(os.environ.get('FLICKR_USERID')) | |
assert len(api_key or '') > 0, 'set FLICKR_API_KEY var' | |
assert len(api_secret or '') > 0, 'set FLICKR_API_SECRET var' | |
assert len(flickr_userid or '') > 0, 'set FLICKR_USERID var' | |
F = MyFlickr(api_key, api_secret, flickr_userid) | |
fp = argv[1].lower() | |
fp = fp if fp.endswith('.jpg') else fp + '/*.jpg' | |
for infile in glob.glob(fp): | |
F.process_photo(infile) | |
fp = argv[1].lower() | |
print fp | |
fp = fp if fp.endswith('.mov') else fp + '/*.mov' | |
for infile in glob.glob(fp): | |
F.process_video(infile) | |
if __name__ == '__main__': | |
try: | |
main(sys.argv) | |
except Exception, e: | |
print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment