def authenticate(self): try: # Only do this if we don't have a valid token already if not self.api.token_valid(perms=Flickr.FLICKR_PERMS): # Get a request token self.api.get_request_token(oauth_callback='oob') authorize_url = self.api.auth_url(perms=Flickr.FLICKR_PERMS) print('Open a browser at the authentication URL:') print(authorize_url) # Get the verifier code from the user. Do this however you # want, as long as the user gives the application the code. verifier = str(input('Enter the Flickr Verifier code: ')) # Trade the request token for an access token self.api.get_access_token(verifier) resp = self.api.test.login() user0 = resp.find('user') print('authenticated id<%s>' % user0.attrib['id']) print('authenticated id<%s>' % user0.find('username').text) except flickrapi.exceptions.FlickrError as e: raise Error(e)
def get_photos(self, minuploaddate): assert minuploaddate >= 0, "minuploaddate not supplied<%s>" % minuploaddate logger.debug("minuploaddate<%s>" % minuploaddate) photos = [] try: for photo in self.api.walk_user( per_page=Flickr.PER_PAGE, min_upload_date=minuploaddate, extras= "date_upload,date_taken,original_format,tags,machine_tags,url_o" ): try: a_photo = { 'id': photo.get('id'), 'title': photo.get('title'), 'originalformat': photo.get('originalformat'), 'dateupload': photo.get('dateupload'), 'datetaken': photo.get('datetaken'), 'datetakenunknown': photo.get('datetakenunknown'), 'datetakengranularity': photo.get('datetakengranularity'), 'url_o': photo.get('url_o'), 'originalsecret': photo.get('originalsecret'), 'farm': photo.get('farm'), 'server': photo.get('server'), 'tags': photo.get('tags'), 'machine_tags': photo.get('machine_tags'), 'signature': self.__get_signature(photo.get('machine_tags')), 'shortname': self.__get_short_name(photo), 'dateflat': general.get_flat_date(photo.get('datetaken')) } photos.append(a_photo) logger.debug('[%d] %s' % (len(photos), a_photo)) except UnicodeError as e: logger.error('Unicode Error: photo.id<%s>, error<%s>' % (photo.get('id'), e)) except flickrapi.exceptions.FlickrError as e: raise Error(e) return photos
def __init__(self, api_key, api_secret, username): assert api_key, "api_key not supplied<%s>" % api_key assert api_secret, "api_secret not supplied<%s>" % api_secret assert username, "username not supplied<%s>" % username try: self.api = flickrapi.FlickrAPI(api_key, api_secret, username=username) except flickrapi.exceptions.FlickrError as e: raise Error(e) logger.debug('%s' % self.api)
def __init__(self, database): logger.debug('database<%s>' % database) assert database, 'Database is <%s>' % database try: self.con = sqlite3.connect(database, check_same_thread=False) except Exception: raise Error("Unable to connect to database <%s>" % database) self.con.row_factory = sqlite3.Row assert self.con, "Database connection not created" self.__create_tables_if_not_exist()
def get_photosets(self): photosets = [] try: for photoset in self.api.walk_photosets(): a_set = { 'id': photoset.get('id'), 'datecreate': photoset.get('date_create'), 'dateupdate': photoset.get('date_update'), 'title': photoset.find('title').text, 'description': photoset.find('description').text } photosets.append(a_set) logger.debug('[%d] %s' % (len(photosets), a_set)) except flickrapi.exceptions.FlickrError as e: raise Error(e) return photosets
def __init__(self, args): assert args, "Args not supplied<%s>" % args logger.debug('args<%s>' % args) configfile = os.path.expanduser('~/.flickrsync/config.ini') self.configname = os.path.abspath( args.config if args.config else configfile) sectionname = args.profile if args.profile else Settings.CONFIG_DEFAULT logger.debug('configname<%s>, sectionname<%s>' % (self.configname, sectionname)) assert os.path.isfile( self.configname), "Config file not found <%s>" % self.configname config = configparser.ConfigParser() config.read(self.configname) try: assert config.get(sectionname, 'api_key'), "api_key not set in config file" assert config.get( sectionname, 'api_secret'), "api_secret not set in config file" assert config.get( sectionname, 'database') or args.database, "database not set in config file" assert config.get( sectionname, 'directory' ) or args.directory, "directory not set in config file" self.api_key = config.get(sectionname, 'api_key') self.api_secret = config.get(sectionname, 'api_secret') self.username = args.username if args.username else config.get( sectionname, 'username') self.directory = os.path.abspath( os.path.expanduser(args.directory if args.directory else config .get(sectionname, 'directory'))) self.database = os.path.abspath( os.path.expanduser(args.database if args.database else config. get(sectionname, 'database'))) except configparser.NoOptionError as e: raise Error('NoOptionError: configname<%s>, sectionname<%s>, %s' % (self.configname, sectionname, e)) except configparser.NoSectionError as e: raise Error('NoSectionError: configname<%s>, sectionname<%s>, %s' % (self.configname, sectionname, e)) logger.debug('api_key <%s>' % self.api_key) logger.debug('api_secret<%s>' % self.api_secret) logger.debug('username <%s>' % self.username) logger.debug('database <%s>' % self.database) logger.debug('directory <%s>' % self.directory) if not os.path.exists(self.directory): logger.info("Config file location: <%s>" % self.configname) raise Error("picture directory does not exist <%s>" % self.directory) if not os.path.exists(os.path.dirname(self.database)): os.makedirs(os.path.dirname(self.database), exist_ok=True) logger.info("Created database directory <%s>" % os.path.dirname(self.database)) if not os.path.exists(self.database): logger.info("Config file location <%s>" % self.configname) logger.info('Database file Does not exist <%s>' % self.database)