<s> from __future__ import absolute_import <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import argparse <EOL> import codecs <EOL> import errno <EOL> import logging <EOL> import os <EOL> import sys <EOL> import time <EOL> import flickr_api as Flickr <EOL> from flickr_api . flickrerrors import FlickrAPIError <EOL> from dateutil import parser <EOL> import yaml <EOL> from flickr_download . filename_handlers import get_filename_handler <EOL> from flickr_download . filename_handlers import get_filename_handler_help <EOL> from flickr_download . utils import get_full_path <EOL> CONFIG_FILE = "<STR_LIT>" <EOL> OAUTH_TOKEN_FILE = "<STR_LIT>" <EOL> def _init ( key , secret , oauth ) : <EOL> """<STR_LIT>""" <EOL> Flickr . set_keys ( key , secret ) <EOL> if not oauth : <EOL> return True <EOL> if os . path . exists ( os . path . expanduser ( OAUTH_TOKEN_FILE ) ) : <EOL> Flickr . set_auth_handler ( os . path . expanduser ( OAUTH_TOKEN_FILE ) ) <EOL> return True <EOL> auth = Flickr . auth . AuthHandler ( ) <EOL> perms = "<STR_LIT>" <EOL> url = auth . get_authorization_url ( perms ) <EOL> print <EOL> print ( "<STR_LIT>" ) <EOL> print ( url ) <EOL> print ( "<STR_LIT>" ) <EOL> Flickr . set_auth_handler ( auth ) <EOL> token = raw_input ( ) <EOL> auth . set_verifier ( token ) <EOL> auth . save ( os . path . expanduser ( OAUTH_TOKEN_FILE ) ) <EOL> print ( "<STR_LIT>" ) <EOL> return False <EOL> def _load_defaults ( ) : <EOL> """<STR_LIT>""" <EOL> filename = os . path . expanduser ( CONFIG_FILE ) <EOL> logging . debug ( '<STR_LIT>' . format ( filename ) ) <EOL> try : <EOL> with open ( filename , '<STR_LIT:r>' ) as cfile : <EOL> vals = yaml . load ( cfile . read ( ) ) <EOL> return vals <EOL> except yaml . YAMLError as ex : <EOL> logging . warning ( '<STR_LIT>' . format ( ex ) ) <EOL> except IOError as ex : <EOL> if ex . errno != errno . ENOENT : <EOL> logging . warning ( '<STR_LIT>' . format ( ex ) ) <EOL> else : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return { } <EOL> def download_set ( set_id , get_filename , size_label = None ) : <EOL> """<STR_LIT>""" <EOL> suffix = "<STR_LIT>" . format ( size_label ) if size_label else "<STR_LIT>" <EOL> pset = Flickr . Photoset ( id = set_id ) <EOL> photos = pset . getPhotos ( ) <EOL> pagenum = <NUM_LIT:2> <EOL> while True : <EOL> try : <EOL> page = pset . getPhotos ( page = pagenum ) <EOL> photos . extend ( page ) <EOL> pagenum += <NUM_LIT:1> <EOL> except FlickrAPIError as ex : <EOL> if ex . code == <NUM_LIT:1> : <EOL> break <EOL> raise <EOL> dirname = pset . title . replace ( os . sep , "<STR_LIT:_>" ) <EOL> if not os . path . exists ( dirname ) : <EOL> os . mkdir ( dirname ) <EOL> for photo in photos : <EOL> fname = get_full_path ( dirname , get_filename ( pset , photo , suffix ) ) <EOL> if os . path . exists ( fname ) : <EOL> print ( '<STR_LIT>' . format ( fname ) ) <EOL> continue <EOL> print ( '<STR_LIT>' . format ( fname ) ) <EOL> photo . save ( fname , size_label ) <EOL> info = photo . getInfo ( ) <EOL> taken = parser . parse ( info [ '<STR_LIT>' ] ) <EOL> taken_unix = time . mktime ( taken . timetuple ( ) ) <EOL> os . utime ( fname , ( taken_unix , taken_unix ) ) <EOL> def download_user ( username , get_filename , size_label ) : <EOL> """<STR_LIT>""" <EOL> user = Flickr . Person . findByUserName ( username ) <EOL> photosets = user . getPhotosets ( ) <EOL> for photoset in photosets : <EOL> download_set ( photoset . id , get_filename , size_label ) <EOL> def print_sets ( username ) : <EOL> """<STR_LIT>""" <EOL> user = Flickr . Person . findByUserName ( username ) <EOL> photosets = user . getPhotosets ( ) <EOL> for photo in photosets : <EOL> print ( '<STR_LIT>' . format ( photo . id , photo . title ) ) <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( <EOL> formatter_class = argparse . RawTextHelpFormatter , <EOL> description = '<STR_LIT>' <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> epilog = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( app = sys . argv [ <NUM_LIT:0> ] ) <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , metavar = '<STR_LIT>' , <EOL> default = None , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . set_defaults ( ** _load_defaults ( ) ) <EOL> args = parser . parse_args ( ) <EOL> if args . list_naming : <EOL> print ( get_filename_handler_help ( ) ) <EOL> return <NUM_LIT:1> <EOL> if not args . api_key or not args . api_secret : <EOL> print ( '<STR_LIT>' , file = sys . stderr ) <EOL> return <NUM_LIT:1> <EOL> ret = _init ( args . api_key , args . api_secret , args . user_auth ) <EOL> if not ret : <EOL> return <NUM_LIT:1> <EOL> if sys . stdout . encoding . lower ( ) != '<STR_LIT:utf-8>' : <EOL> sys . stdout = codecs . getwriter ( sys . stdout . encoding ) ( sys . stdout , '<STR_LIT:replace>' ) <EOL> if args . list : <EOL> print_sets ( args . list ) <EOL> return <NUM_LIT:0> <EOL> if args . download or args . download_user : <EOL> try : <EOL> get_filename = get_filename_handler ( args . naming ) <EOL> if args . download : <EOL> download_set ( args . download , get_filename , args . quality ) <EOL> else : <EOL> download_user ( args . download_user , get_filename , args . quality ) <EOL> except KeyboardInterrupt : <EOL> print ( '<STR_LIT>' , file = sys . stderr ) <EOL> return <NUM_LIT:0> <EOL> print ( '<STR_LIT>' , file = sys . stderr ) <EOL> parser . print_help ( ) <EOL> return <NUM_LIT:1> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
