def set_photo_date(self, **kwargs): if not 'exif' in kwargs or (not 'sk' in kwargs and not 'fp' in kwargs): kwargs = self.extend_kwargs(**kwargs) if not 'exif' in kwargs or (not 'sk' in kwargs and not 'fp' in kwargs): return -1 try: if 'fp' in kwargs: fp = kwargs['fp'] else: fp = flickr_api.Photo(id=kwargs['sk']) dttm = datetime.strptime(kwargs['exif'][self.cfg['date_posted']], '%Y:%m:%d %H:%M:%S') udttm = int((dttm - datetime(1970, 1, 1)).total_seconds()) if udttm < self.cfg['min_date_posted']: udttm = self.cfg['min_date_posted'] + (udttm / 35000) fp.setDates(date_posted=str(udttm)) self.log.info(self.current_file + ": Setting Date to " + str(udttm)) except Exception, e: self.log.exception(self.current_file + ': ' + str(e)) return 0
def poll_upload_tickets(self): """ Checks the upload status of all uploading tickets. Once complete, adds the photo to it's repsective photoset """ while self._syncing: logging.debug("[---] Checking [%s] upload tickets" % len(self._upload_tickets)) if len(self._upload_tickets) > 0: logging.info("Checking [%s] upload tickets" % len(self._upload_tickets)) tickets = flickr.Photo.checkUploadTickets( self._upload_tickets.keys()) for ticket in tickets: logging.debug("[---->]checking upload ticket [%s]" % (ticket)) if ticket["complete"] == 1: photo = flickr.Photo(id=ticket["photoid"]) logging.debug("\tcompleted uploading photo [%s]" % (ticket["photoid"])) self.add_to_photoset( photo, self._upload_tickets[ticket["id"]]) del self._upload_tickets[ticket["id"]] else: self._syncing = False time.sleep(1)
def set_photo_tags(self, **kwargs): if not 'exif' in kwargs or (not 'sk' in kwargs and not 'fp' in kwargs): kwargs = self.extend_kwargs(**kwargs) if not 'exif' in kwargs or (not 'sk' in kwargs and not 'fp' in kwargs): return -1 tag_set = [] for tag_source in self.cfg['tags'].split(','): if tag_source in kwargs['exif']: tag_value = kwargs['exif'][tag_source] if isinstance(tag_value, basestring): tag_set.append(tag_value) else: tag_set.extend(tag_value) tags = "" for tag in set(tag_set): if tags == '': tags = '"' + tag + '"' else: tags = tags + ', "' + tag + '"' self.log.info(self.current_file + ': Setting Tags To ' + tags) try: if 'fp' in kwargs: fp = kwargs['fp'] else: fp = flickr_api.Photo(id=kwargs['sk']) fp.setTags(tags) except Exception, e: self.log.exception(self.current_file + ': ' + str(e)) return 0
def download_photo(photo_id, get_filename, size_label): """ Download one photo @param photo_id: str, id of the photo @param get_filename: Function, function that creates a filename for the photo @param size_label: str|None, size to download (or None for largest available) """ photo = Flickr.Photo(id=photo_id) suffix = " ({})".format(size_label) if size_label else "" do_download_photo(".", None, photo, size_label, suffix, get_filename)
def download_photo(photo_id, get_filename, size_label, skip_download=False, save_json=False): """ Download one photo @param photo_id: str, id of the photo @param get_filename: Function, function that creates a filename for the photo @param size_label: str|None, size to download (or None for largest available) @param skip_download: bool, do not actually download the photo @param save_json: bool, save photo info as .json file """ photo = Flickr.Photo(id=photo_id) suffix = " ({})".format(size_label) if size_label else "" do_download_photo(".", None, photo, size_label, suffix, get_filename, skip_download, save_json)
def set_photo_safety(self, **kwargs): if not 'sk' in kwargs and not 'fp' in kwargs: kwargs = self.extend_kwargs(**kwargs) if not 'sk' in kwargs and not 'fp' in kwargs: return -1 try: if 'fp' in kwargs: fp = kwargs['fp'] else: fp = flickr_api.Photo(id=kwargs['sk']) fp.setSafetyLevel(hidden=self.cfg['hidden'], safety_level=self.cfg['safety_level']) except Exception, e: self.log.exception(self.current_file + ': ' + str(e)) return 0
def delete_duplicates(): logging.debug('Find duplicates by title and taken timestamp.') sql = """SELECT p.id FROM f_photo p INNER JOIN (SELECT MIN( id ) AS id, title, taken FROM f_photo GROUP BY taken, title HAVING COUNT( * ) > 1) d ON p.title = d.title AND p.taken = d.taken WHERE p.id <> d.id;""" duplicateids = get_int_list_from_database(sql) logging.debug('Found %s duplicates.' % len(duplicateids)) for id in duplicateids: logging.debug('Deleting photo id %s.' % id) photo = flickr_api.Photo(id = id) photo.delete()
def getVotes(photo): flickr_api.set_keys(api_key=__flickr_api_key, api_secret=__flickr_api_secret) flickr_api.set_auth_handler('oauth_verifier.txt') favorites = flickr_api.Photo(id=photo.flickr_photo_id).getFavorites() graph = facebook.GraphAPI(access_token=__facebook_page_token, version='2.5') response = graph.get_object(id=photo.facebook_post_id, fields='likes.summary(true)') likes = response['likes']['summary']['total_count'] photo.favorites = len(favorites) photo.likes = likes photo.votes = photo.likes + photo.favorites photo.last_modified_time = timezone.now() photo.save( update_fields=['favorites', 'likes', 'votes', 'last_modified_time']) return photo.favorites + photo.likes
def do_download_photo(dirname, photo_id): photo = flickr.Photo(id=photo_id) fname = photo_id + '.jpg' if not os.path.exists(dirname): os.makedirs(dirname) if os.path.exists(fname): # TODO: Ideally we should check for file size / md5 here # to handle failed downloads. print('Skipping {0}, as it exists already'.format(fname)) return print('Saving: {} ({})'.format(fname, photo.getPageUrl())) try: photo.save(dirname + "/" + fname, None) except IOError, ex: logging.warning('IO error saving photo: {}'.format(ex.strerror)) return
def upload_photo(self, photo_file, photo_title, file_extension, photoset_title, make_public, is_hidden): """ Uploads a given photo to a given photoset. Photo is set to private for all users """ if photo_title == ".DS_Store" or file_extension.lower( ) not in self.valid_extensions: logging.debug("Invalid file type, cannot upload...") return logging.info( "Attempting to upload photo [%s] then add to photoset [%s]." % (photo_title, photoset_title)) upload_result = flickr.upload( **{ "photo_file": photo_file, "is_public": make_public, "is_friend": "0", "is_family": "0", "hidden": is_hidden, "async": 1 }) if isinstance(upload_result, flickr.Photo): logging.info("\tphoto uploaded immediately! Photo ID: [%s]" % upload_result.id) photo = flickr.Photo(id=upload_result.id) self.add_to_photoset(photo, photoset_title) return "DONE" elif isinstance(upload_result, UploadTicket): logging.info( "\tadded to Flickr upload queue, will check on shortly. UploadTicket ID: [%s]" % upload_result.id) self._upload_tickets[upload_result.id] = photoset_title self._syncing = True _thread.start_new_thread(self.poll_upload_tickets, ()) return "QUEUED" else: logging.info("Unknown response received: %s" % upload_result) return "FAIL"
def poll_upload_tickets(self): """ Checks the upload status of all uploading tickets. Once complete, adds the photo to it's repsective photoset """ while self._syncing: tickets = flickr.Photo.checkUploadTickets( self._upload_tickets.keys()) for ticket in tickets: if ticket["complete"] == 1: photo = flickr.Photo(id=ticket["photoid"]) self.add_to_photoset(photo, self._upload_tickets[ticket["id"]]) del self._upload_tickets[ticket["id"]] time.sleep(1)
def set_photo_perms(self, **kwargs): if not 'sk' in kwargs and not 'fp' in kwargs: kwargs = self.extend_kwargs(**kwargs) if not 'sk' in kwargs and not 'fp' in kwargs: return -1 try: if 'fp' in kwargs: fp = kwargs['fp'] else: fp = flickr_api.Photo(id=kwargs['sk']) fp.setPerms(is_public=self.cfg['is_public'], is_friend=self.cfg['is_friend'], is_family=self.cfg['is_family'], perm_comment=self.cfg['perm_comment'], perm_addmeta=self.cfg['perm_addmeta']) except Exception, e: self.log.exception(self.current_file + ': ' + str(e)) return 0
def DownloadPhotos(): ff = file(downloadlist) lines = ff.readlines() ff.close() order = 0 for line in lines: order += 1 data = line.split("\t")#split the line with "tab" p = flickr_api.Photo(id = data[0]) if(not os.path.exists(pdir)): os.mkdir(pdir) print pdir # downloading the photo file() filename = pdir + str(order) + "." + data[0] + ".jpg" if(not os.path.exists(filename)): print filename try: p.save(filename, size_label = 'Medium') except Exception,e: print Exception, ":", e
def set_photo_metadata(self, **kwargs): if not 'exif' in kwargs or (not 'sk' in kwargs and not 'fp' in kwargs): kwargs = self.extend_kwargs(**kwargs) if not 'exif' in kwargs or (not 'sk' in kwargs and not 'fp' in kwargs): return -1 arg_list = {} arg_list['description'] = self.exif_match(self.cfg['description'], kwargs['exif']) arg_list['title'] = self.exif_match(self.cfg['title'], kwargs['exif']) try: if 'fp' in kwargs: fp = kwargs['fp'] else: fp = flickr_api.Photo(id=kwargs['sk']) fp.setMeta(**arg_list) except Exception, e: self.log.exception(self.current_file + ': ' + str(e)) return 0
def getPhotoDetails(photo, user_access_token): ''' 取得某張照片的facebook讚數、評論跟內容,和flickr的收藏數 如果使用者有登入的話,確認使用者是否有按過讚了 ''' __facebook_query_field = 'likes.summary(true), comments{from{name, picture{url}}, message}' graph = facebook.GraphAPI(access_token=__facebook_page_token, version='2.5') response = graph.get_object(id=photo.facebook_post_id, fields=__facebook_query_field) comment_list = [] if 'comments' in response: for item in response['comments']['data']: comment_list.append( Comment( user_name=item['from']['name'], user_photo_url=item['from']['picture']['data']['url'], comment_text=item['message'], comment_facebook_id=item['id'], )) facebook_likes = response['likes']['summary']['total_count'] flickr_api.set_keys(api_key=__flickr_api_key, api_secret=__flickr_api_secret) flickr_api.set_auth_handler('oauth_verifier.txt') favorites = flickr_api.Photo(id=photo.flickr_photo_id).getFavorites() return { 'facebook_likes': facebook_likes, 'facebook_post_id': photo.facebook_post_id, 'comment_list': [x.toDict() for x in comment_list], 'flickr_favorites': len(favorites), 'photo_url': photo.flickr_photo_url, 'photo_content': getFacebookPostContent(photo), 'user_has_like': getHasLiked(photo.facebook_post_id, user_access_token), }
def edit_photo_title(self, old_photo_title, old_photoset_title, new_photo_title, new_photoset_title): """ Deletes a given photo from a given photoset """ photoset = self._photosets[old_photoset_title] photo = next(p for p in photoset["photos"] if p.title == old_photo_title) photoset["photos"].remove(photo) photo = flickr.Photo(id=photo.id, title=new_photo_title) photo.setMeta(title=new_photo_title) if old_photoset_title != new_photoset_title: photoset.removePhoto(photo) if new_photo_title not in self._photosets: photoset = self.add_photoset(new_photoset_title, photo) photoset["photoset"].addPhoto(photo) photoset["photos"].append(photo) logging.info("Edited photo name")
def delete_photo(self, **kwargs): if not 'sk' in kwargs and not 'fp' in kwargs: kwargs = extend_kwargs(**kwargs) if not 'sk' in kwargs and not 'fp' in kwargs: return -1 try: if 'fp' in kwargs: fp = kwargs['fp'] else: fp = flickr_api.Photo(id=kwargs['sk']) self.log.info(self.current_file + ": Deleting From Flickr") fp.delete() except Exception, e: if type( e ) == flickr_api.flickrerrors.FlickrAPIError and e.code == 1: self.log.info(self.current_file + ": File Already Deleted on Flickr. Continuing") else: self.log.info(self.current_file + ": " + str(e)) return 0
for tag in photoset_tags: if not tag in tags_to_ignore and tag in kwargs['exif']: tag_value = kwargs['exif'][tag] if isinstance(tag_value, basestring): desired_ps.append(tag_value) else: desired_ps.extend(tag_value) current_ps = [] try: if 'fp' in kwargs: fp = kwargs['fp'] else: fp = flickr_api.Photo(id=kwargs['sk']) for ppps in fp.getAllContexts()[0]: if type(ppps) == flickr_api.objects.Photoset: if ppps.title in desired_ps: current_ps.append(ppps.title) else: self.log.info(self.current_file + ': Removing Photo From Photoset ' + ppps.title + ' [' + ppps.id + ']') try: ppps.removePhoto(fp) except Exception, e: self.log.exception(self.current_file + ': ' + str(e)) return 0
def fetch_by_id(id, save_path): photo = f.Photo(id=id) return fetch(photo, save_path)
def flickr_photo(self): if not self.photo.flickr_id: raise ValueError('Photo is missing flickr_id.') return flickr_api.Photo(id=self.photo.flickr_id)
def sync(self): if self.halt == 1: return PBT = "PB" + str(self.pub) c = self.conn.cursor() c2 = self.conn.cursor() epicfail = self.cfg['epic_fail'] for photo in c.execute('SELECT ' + PBT + '.PK,' + PBT + '.SK,PHOTOS.FILE FROM ' + PBT + ', PHOTOS WHERE PHOTOS.PK = ' + PBT + '.PK AND PHOTOS.STATUS="RM"'): if self.halt != 1: pk, sk, self.current_file = photo self.log.info(self.current_file + ": Deleting From Flickr") if self.delete_photo(sk=sk) == 1: c2.execute('DELETE FROM ' + PBT + ' WHERE pk=?', [pk]) self.log.info(self.current_file + ": Deleting From DB Table " + PBT) self.conn.commit() for photo in c.execute( 'SELECT PK,PATH,FILE FROM PHOTOS WHERE STATUS="OK" AND PK NOT IN (SELECT PK FROM ' + PBT + ') AND PATH LIKE ?', [self.cfg['PATH'] + "%"]): pk, path, self.current_file = photo self.log.info(self.current_file + ": Inserting as NW into " + PBT) c2.execute('INSERT INTO ' + PBT + ' (PK,SK,STATUS) VALUES (?,?,?)', [photo[0], "TEMPSK_" + photo[0], "NW"]) self.conn.commit() for tc in c.execute( 'SELECT PK FROM ' + PBT + ' WHERE STATUS!="OK" AND STATUS!=?', [epicfail]): if self.halt == 1: return c2.execute( 'SELECT ' + PBT + '.PK,' + PBT + '.SK, ' + PBT + '.STATUS, ' + PBT + '.UDTTM, PHOTOS.PATH, PHOTOS.FILE ,PHOTOS.EXIF,PHOTOS.MDTTM,' + PBT + '.SYNCED FROM ' + PBT + ', PHOTOS WHERE PHOTOS.PK=' + PBT + '.PK AND PHOTOS.PK=?', [tc[0]]) pk, sk, status, udttm, path, file, raw_exif, mdttm, synced_raw = c2.fetchone( ) exif = json.loads(raw_exif) self.current_file = str(file) try: status = int(status) except Exception: status = 0 fp = None if udttm < mdttm or synced_raw == None or synced_raw == '' or synced_raw == '{"D": 1, "F": 1, "I": 1, "M": 1, "P": 1, "S": 1, "T": 1}': synced = { 'I': 0, 'S': 0, 'F': 0, 'P': 0, 'T': 0, 'M': 0, 'D': 0 } else: synced = json.loads(synced_raw) self.log.info(self.current_file + ": Sync Starting With Status " + str(synced)) if synced['I'] == 1 or udttm != None: try: fp = flickr_api.Photo(id=sk) except Exception, e: if type( e ) == flickr_api.flickrerrors.FlickrAPIError and e.code == 1: fp = None synced['I'] = 0 else: self.log.info(self.current_file + ": " + str(e)) try: udttm = os.path.getmtime(os.path.join(path, file)) except Exception, e: pass