Created
May 10, 2012 00:05
-
-
Save afternoon/2649929 to your computer and use it in GitHub Desktop.
Very simple Flickr uploader
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
#!/usr/bin/env python | |
from argparse import ArgumentParser | |
from ConfigParser import ConfigParser, NoSectionError, NoOptionError | |
from datetime import datetime | |
from flickrapi import FlickrAPI | |
from os import environ | |
from os.path import abspath, basename, expanduser | |
from sys import argv | |
FLICKR_CONFIG_FILE = '~/.flickr/config' | |
def parse_args(): | |
parser = ArgumentParser(description='upload photos to Flickr') | |
parser.add_argument('--config', default=FLICKR_CONFIG_FILE, metavar='file', | |
help='configuration file (default %s)' % FLICKR_CONFIG_FILE) | |
parser.add_argument('--public', '-p', action='store_true', | |
help='make photos public') | |
parser.add_argument('--family', '-f', action='store_true', | |
help='make photos available to family') | |
parser.add_argument('--friends', '-r', action='store_true', | |
help='make photos available to friends') | |
parser.add_argument('--fandf', '-F', action='store_true', | |
help='make photos available to family and friends') | |
parser.add_argument('--private', '-P', action='store_true', | |
help='make photos private') | |
parser.add_argument('files', metavar='file', nargs='+', | |
help='JPEG files to upload') | |
return parser.parse_args() | |
def read_conf_file(args): | |
fileconf = ConfigParser() | |
fileconf.read(abspath(expanduser(args.config))) | |
return fileconf | |
def determine_visibility(args, fileconf): | |
if args.private: | |
is_public, is_family, is_friend = 0, 0, 0 | |
elif args.friends: | |
is_public, is_family, is_friend = 0, 0, 1 | |
elif args.family: | |
is_public, is_family, is_friend = 0, 1, 0 | |
elif args.fandf: | |
is_public, is_family, is_friend = 0, 1, 1 | |
elif args.public: | |
is_public, is_family, is_friend = 1, 1, 1 | |
else: | |
try: | |
is_public = fileconf.getint('upload', 'is_public') | |
is_family = fileconf.getint('upload', 'is_family') | |
is_friend = fileconf.getint('upload', 'is_friend') | |
except NoSectionError, NoOptionError: | |
is_public, is_family, is_friend = 0, 0, 0 | |
return is_public, is_family, is_friend | |
def merge_conf(args, fileconf): | |
'''Merge file config and command line args, the former taking precedence''' | |
is_public, is_family, is_friend = determine_visibility(args, fileconf) | |
return { | |
'api_key': fileconf.get('credentials', 'api_key'), | |
'secret': fileconf.get('credentials', 'secret'), | |
'is_public': is_public, | |
'is_family': is_family, | |
'is_friend': is_friend, | |
'files': args.files | |
} | |
def get_conf(): | |
args = parse_args() | |
fileconf = read_conf_file(args) | |
return merge_conf(args, fileconf) | |
def auth_session(conf): | |
api_key = conf['api_key'] | |
secret = conf['secret'] | |
flickr = FlickrAPI(api_key, secret) | |
(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)) | |
return flickr | |
def upload_files(flickr, conf): | |
for filename in conf['files']: | |
print datetime.now(), filename | |
flickr.upload(filename=filename, callback=show_progress, | |
title=basename(filename), | |
is_public=conf['is_public'], | |
is_family=conf['is_family'], | |
is_friend=conf['is_friend']) | |
def show_progress(percentage, done): | |
print 'progress, percentage %s, done %s' % (percentage, done) | |
if __name__ == '__main__': | |
conf = get_conf() | |
flickr = auth_session(conf) | |
upload_files(flickr, conf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment