def authPart1(self): f = FlickrAPI(api_key=self.api_key, api_secret=self.api_secret, callback_url= web.ctx.homedomain + '/auth/flickrAuth?') auth_props = f.get_authentication_tokens(perms='write') auth_url = auth_props['auth_url'] #Store this token in a session or something for later use in the next step. self.oauth_token = auth_props['oauth_token'] self.oauth_token_secret = auth_props['oauth_token_secret'] return auth_url
class flickr_location(location): def __init__( self, flickr_key, flickr_secret, flickr_oauth_token, flickr_oauth_token_secret, flickr_nsid ): self.api = FlickrAPI( api_key=flickr_key, api_secret=flickr_secret, oauth_token=flickr_oauth_token, oauth_token_secret=flickr_oauth_token_secret ) self.flickr_nsid = flickr_nsid def get_location(self): recent_activity = self.api.get( 'flickr.people.getPhotos', params={"user_id": self.flickr_nsid, "per_page": 20} ) place_holder = 0 last_flickr_photo = None last_photo_location = None if recent_activity: while not last_photo_location: try: last_flickr_photo = self.api.get( 'flickr.photos.getInfo', params={ "photo_id": recent_activity['photos']['photo'][place_holder]['id'], "secret": recent_activity['photos']['photo'][place_holder]['secret'] } ) last_photo_location = last_flickr_photo['photo']['location'] except: place_holder = place_holder + 1 flickr_location = {} flickr_location['source'] = 'flickr' flickr_location['id'] = last_flickr_photo['photo']['id'] flickr_location['date'] = str(datetime.datetime.fromtimestamp(int(last_flickr_photo['photo']['dateuploaded']))) flickr_location['latitude'] = last_photo_location['latitude'] flickr_location['longitude'] = last_photo_location['longitude'] return flickr_location
def post(self): p_id = self.request.get('id') p_title = self.request.get('title') album_id = self.request.get('album_id') album_yaf = self.request.get('album_yaf').split(':')[-1] ph = Photo.get_by_id(p_id) if ph is None: f = FlickrAPI(api_key = F_API_KEY, api_secret= F_API_SECRET, oauth_token= F_TOKEN, oauth_token_secret= F_TOKEN_SECRET) sizes = f.get('flickr.photos.getSizes', params={'photo_id': p_id}) if sizes['stat'] == 'ok': url_photo = sizes['sizes']['size'][-1]['source'] logging.info(url_photo) ph = Photo( id = p_id, url = url_photo, title = p_title, flikr_id = p_id, flikr_album_id = album_id, yaf_id = '', yaf_album_id = album_yaf, ) ph.put() if not ph.sync: # taskqueue.add(url='/psync/',queue_name='psync', params = {'id': p.id,}) form_fields = { "id": ph.flikr_id, "url": ph.url, "title": ph.title, "album_id": ph.yaf_album_id, } form_data = urllib.urlencode(form_fields) slaves = Slave.query().order(Slave.order) slave_url = 'http://fyslave.appspot.com/sync/' for s in slaves: logging.info(s) if s.status: slave_url = s.url break logging.info(slave_url) result = urlfetch.fetch(url= slave_url, payload=form_data, method=urlfetch.POST, headers={'Content-Type': 'application/x-www-form-urlencoded'})
def get(self): oauth_verifier = self.request.get('oauth_verifier') oauth_token = self.session.get('oauth_token') oauth_token_secret = self.session.get('oauth_token_secret') f = FlickrAPI(api_key=F_API_KEY, api_secret=F_API_SECRET, oauth_token=oauth_token, oauth_token_secret=oauth_token_secret) authorized_tokens = f.get_auth_tokens(oauth_verifier) Settings.set('F_TOKEN', authorized_tokens['oauth_token']) Settings.set('F_TOKEN_SECRET', authorized_tokens['oauth_token_secret']) self.redirect('/')
def authPart2(self, oauth_token, oauth_verifier): f = FlickrAPI(api_key=self.api_key, api_secret=self.api_secret, oauth_token=self.oauth_token, oauth_token_secret=self.oauth_token_secret) authorised_tokens = f.get_auth_tokens(oauth_verifier) self.final_oauth_token = authorised_tokens['oauth_token'] self.final_oauth_token_secret = authorised_tokens['oauth_token_secret'] authDict = authPickler.getAuthDict(self.apiName) authDict["final_oauth_token"] = self.final_oauth_token authDict["final_oauth_token_secret"] = self.final_oauth_token_secret authPickler.setAuthDict(self.apiName, authDict) self.authorised = True self.setupAPI() return redirectToUrlText(web.ctx.homedomain)
def dispatch(self, request, *args, **kwargs): tokens = request.user.social_auth.get(provider='flickr').tokens tokens = dict(urlparse.parse_qsl(tokens.get('access_token'))) self.flickr_api = FlickrAPI( api_key=settings.FLICKR_APP_ID, api_secret=settings.FLICKR_API_SECRET, oauth_token=tokens.get('oauth_token'), oauth_token_secret=tokens.get('oauth_token_secret')) return (super(FlickrRequiredMixin, self).dispatch(request, *args, **kwargs))
def __init__(self, api_key='', api_secret=''): self.base = 'https://farm{farmid}.staticflickr.com/{serverid}/{id}_{secret}_o.{minetype}' try: with open('flickr.token', 'r') as f: token = pickle.load(f) self.flickr = FlickrAPI( api_key, api_secret, oauth_token=token['oauth_token'], oauth_token_secret=token['oauth_token_secret']) except: print 'first run!' f = FlickrAPI(api_key=api_key, api_secret=api_secret, callback_url='oob') auth_props = f.get_authentication_tokens(perms=u'write') auth_url = auth_props['auth_url'] oauth_token = auth_props['oauth_token'] oauth_token_secret = auth_props['oauth_token_secret'] print('open the url in browser and input the code:\n' + auth_url) oauth_verifier = self.toUnicodeOrBust(raw_input('verifier code:')) f2 = FlickrAPI(api_key=api_key, api_secret=api_secret, oauth_token=oauth_token, oauth_token_secret=oauth_token_secret) authorized_tokens = f2.get_auth_tokens(oauth_verifier) final_oauth_token = authorized_tokens['oauth_token'] final_oauth_token_secret = authorized_tokens['oauth_token_secret'] token = { 'oauth_token': final_oauth_token, 'oauth_token_secret': final_oauth_token_secret } with open('flickr.token', 'w') as f: pickle.dump(token, f) self.flickr = FlickrAPI( api_key=api_key, api_secret=api_secret, oauth_token=final_oauth_token, oauth_token_secret=final_oauth_token_secret)
def __init__( self, flickr_key, flickr_secret, flickr_oauth_token, flickr_oauth_token_secret, flickr_nsid ): self.api = FlickrAPI( api_key=flickr_key, api_secret=flickr_secret, oauth_token=flickr_oauth_token, oauth_token_secret=flickr_oauth_token_secret ) self.flickr_nsid = flickr_nsid
def __init__(self, database, root): self.session = OAuth1Session( self.consumer_key, self.consumer_secret, self.session_access_token, self.session_access_token_secret ) self.flickr = FlickrAPI( api_key=self.consumer_key, api_secret=self.consumer_secret, oauth_token=self.session_access_token, oauth_token_secret=self.session_access_token_secret ) self.database = database self.root = os.path.realpath(root) self.get_collections_tree()
def setupAPI(self): self.flickrAPI = FlickrAPI(api_key=self.api_key, api_secret=self.api_secret, oauth_token=self.final_oauth_token, oauth_token_secret=self.final_oauth_token_secret)
def maps(loc1,loc2): bing_maps_key = "PUT_KEY_HERE" r = requests.get('http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0='+loc1+'&wp.1='+loc2+'&key='+bing_maps_key) r_dict = r.json() route_obj = r_dict["resourceSets"][0]["resources"][0] route_dir = route_obj["routeLegs"][0]["itineraryItems"] directions_list = [] i = -1 for r_instr in route_dir: i += 1 dir_obj = {} r_text = r_instr["instruction"]["text"] dir_obj["coords"] = "("+str(r_instr["maneuverPoint"]["coordinates"][0]) + ", "+str(r_instr["maneuverPoint"]["coordinates"][1])+")" # get hints try: dir_obj["hint"] = r_instr["hints"][1]["text"]+"." except: dir_obj["hint"] = "" # FLICKR api_key = "PUT_FLICKR_KEY_HERE" api_secret = "PUT_FLICKR_SECRET_HERE" flickr = FlickrAPI(api_key, api_secret, '/') auth_props = flickr.get_authentication_tokens() auth_url = auth_props['auth_url'] oauth_token = auth_props['oauth_token'] oauth_token_secret = auth_props['oauth_token_secret'] photo_json = flickr.get('flickr.photos.search', params={'api_key':api_key, 'lat': r_instr["maneuverPoint"]["coordinates"][0], 'lon': r_instr["maneuverPoint"]["coordinates"][1], 'radius': '0.01'}) photos = photo_json['photos']['photo'] photo = random.choice(photos) flickr_image_url = 'https://farm' + str(photo['farm']) + '.staticflickr.com/' + str(photo['server']) + '/' + str(photo['id']) + '_' + photo['secret'] + '.jpg' # PROJECT OXFORD oxford_key = "PUT_KEY_HERE" headers = { # Request headers 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': oxford_key, } params = urllib.urlencode({ # Request parameters 'visualFeatures': 'All', }) try: conn = httplib.HTTPSConnection('api.projectoxford.ai') conn.request("POST", "/vision/v1/analyses?%s" % params, '{ "Url":"'+flickr_image_url+'"}',headers) ox_response = conn.getresponse() ox_data = ox_response.read() ox_json = json.loads(ox_data) #print(ox_data) conn.close() except Exception as e: print("[Errno {0}] {1}".format(e.errno, e.strerror)) # combine directions + descriptions try: if ox_json["categories"][0]["name"] != "others_": r_text = r_text + " near this " + ox_json["categories"][0]["name"] #print flickr_image_url else: pass except: #print "program failed because of oxford. IMAGE URL \n" #print flickr_image_url #print "OXFORD RESPONSE: \n" #print ox_data pass r_text = r_text +". " r_text = r_text.replace("_"," ") r_text = r_text.replace("abstract","landmark") r_text = r_text.replace("outdoor","outdoor area") r_text = r_text.replace(" .",".") print r_text dir_obj["r_text"] = r_text dir_obj["url"] = flickr_image_url directions_list.append(dir_obj) #return json.dumps(directions_list) return render_template("maps.html",my_list = directions_list)
# Cache the folder and file info pic_folders_array = {} for root, dirs, files in os.walk('/home/dave/Media/Pictures'): pic_folders_array[root[16:]] = files # Get Flickr Oauth login data flickr_session_data = pickle.load(open("flickr_session_data.p", "rb")) my_api_key, my_api_secret, final_oauth_token, final_oauth_token_secret = flickr_session_data # Set up error handling try: # Create Flickr session instance flickr_handle = FlickrAPI(api_key=my_api_key, api_secret=my_api_secret, oauth_token=final_oauth_token, oauth_token_secret=final_oauth_token_secret ) # Get the list of collections from Flickr collections_dict = flickr_handle.get('flickr.collections.getTree') collections_dict = collections_dict['collections'] # Put collections info into useful data structures collections_info_dict = {} def recursive_json_dict_walker(json_dict, so_far=""): # Need to handle the top-level case where there is no title field if ('title' in json_dict): so_far = so_far + "/" + json_dict['title'] collections_info_dict[so_far] = (json_dict['id'])
class Flickr(): #Flickr api_key和api_secret,申请地址:https://www.flickr.com/services/apps/create/apply/ def __init__(self, api_key='', api_secret=''): self.base = 'https://farm{farmid}.staticflickr.com/{serverid}/{id}_{secret}_o.{minetype}' try: with open('flickr.token', 'r') as f: token = pickle.load(f) self.flickr = FlickrAPI( api_key, api_secret, oauth_token=token['oauth_token'], oauth_token_secret=token['oauth_token_secret']) except: print 'first run!' f = FlickrAPI(api_key=api_key, api_secret=api_secret, callback_url='oob') auth_props = f.get_authentication_tokens(perms=u'write') auth_url = auth_props['auth_url'] oauth_token = auth_props['oauth_token'] oauth_token_secret = auth_props['oauth_token_secret'] print('open the url in browser and input the code:\n' + auth_url) oauth_verifier = self.toUnicodeOrBust(raw_input('verifier code:')) f2 = FlickrAPI(api_key=api_key, api_secret=api_secret, oauth_token=oauth_token, oauth_token_secret=oauth_token_secret) authorized_tokens = f2.get_auth_tokens(oauth_verifier) final_oauth_token = authorized_tokens['oauth_token'] final_oauth_token_secret = authorized_tokens['oauth_token_secret'] token = { 'oauth_token': final_oauth_token, 'oauth_token_secret': final_oauth_token_secret } with open('flickr.token', 'w') as f: pickle.dump(token, f) self.flickr = FlickrAPI( api_key=api_key, api_secret=api_secret, oauth_token=final_oauth_token, oauth_token_secret=final_oauth_token_secret) def toUnicodeOrBust(self, obj, encoding='utf-8'): if isinstance(obj, basestring): if not isinstance(obj, unicode): obj = unicode(obj, encoding) return obj def upload(self, image_path): if image_path.startswith('http'): headers = { 'Referer': 'http://www.mm131.com/', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36' } r = requests.get(image_path, headers=headers) img_cont = r.content filename = base64.b64encode(image_path) + '.' + image_path.split( '/')[-1].split('.')[-1] with open(filename, 'wb') as f: f.write(img_cont) else: filename = image_path try: with open(filename, 'rb') as img: photo_id = self.flickr.post(files=img) os.remove(filename) return photo_id except Exception as e: print e os.remove(filename) return False def get_image(self, image_path): photo_info = self.upload(image_path) if photo_info == False: print 'upload image fail!' return False else: if photo_info['stat'] == 'ok': photo_id = photo_info['photoid'] info = self.flickr.post('flickr.photos.getInfo', {'photo_id': photo_id}) farmid = info['photo']['farm'] serverid = info['photo']['server'] id = info['photo']['id'] secret = info['photo']['originalsecret'] minetype = info['photo']['originalformat'] imgurl = self.base.format(farmid=farmid, serverid=serverid, id=id, secret=secret, minetype=minetype) return imgurl else: print 'upload image fail!' return False
def get(self): url = '' ps = '' albums = {} y_albums = {} auth = False F_TOKEN = Settings.get('F_TOKEN') F_TOKEN_SECRET = Settings.get('F_TOKEN_SECRET') if F_TOKEN and F_TOKEN_SECRET: f = FlickrAPI(api_key = F_API_KEY, api_secret= F_API_SECRET, oauth_token= F_TOKEN, oauth_token_secret= F_TOKEN_SECRET) try: result = f.get('flickr.test.null') if result['stat'] == 'ok': auth = True except: auth = False if not auth: f = FlickrAPI(api_key=F_API_KEY, api_secret=F_API_SECRET, callback_url='http://fytunnel.appspot.com/callback/') #callback_url='http://localhost:10080/callback/') auth_props = f.get_authentication_tokens(perms = 'read') auth_url = auth_props['auth_url'] #Store this token in a session or something for later use in the next step. self.session['oauth_token'] = auth_props['oauth_token'] self.session['oauth_token_secret'] = auth_props['oauth_token_secret'] self.redirect(auth_url) else: f = FlickrAPI(api_key = F_API_KEY, api_secret= F_API_SECRET, oauth_token= F_TOKEN, oauth_token_secret= F_TOKEN_SECRET) user = f.get('flickr.test.login') data = f.get('flickr.photosets.getList', params={'user_id': user['user']['id']}) albums = {} if data['stat'] == 'ok': for item in data['photosets']['photoset']: id = item['id'] albums[id] = {} albums[id]['id'] = id albums[id]['photos'] = item['photos'] albums[id]['title'] = item['title']['_content'] albums[id]['description'] = item['description']['_content'] ''' a = flickr_api.auth.AuthHandler.fromdict(flickr_auth) flickr_api.set_auth_handler(a) u = flickr_api.test.login() ps = u.getPhotosets() ''' for id, item in albums.iteritems(): a = Album.get_by_id(id) if a is None: a = Album(title = item['title'],description = item['description'], flikr_id = item['id'], yaf_id = '', id = id) a.put() if a.yaf_id == '': url = 'http://api-fotki.yandex.ru/api/users/protasov-s/albums/?format=json' data = json.dumps({'title':item['title'], 'summary':item['description'], 'password':item['id']}) req = urllib2.Request(url, data, {'Accept': 'application/json','Content-Type': 'application/json; charset=utf-8; type=entry;', 'Authorization':'OAuth ' + yaf_token}) f = urllib2.urlopen(req) data = json.load(f) a.yaf_id = data['id'] a.put() f.close() if a.title != item['title'] or a.description != item['description']: a.title = item['title'] a.description = item['description'] url = 'http://api-fotki.yandex.ru/api/users/protasov-s/album/%s/?format=json' % a.yaf_id.split(':')[-1] result = urlfetch.fetch(url=url,headers={'Accept': 'application/json', 'Authorization':'OAuth ' + yaf_token}) if result.status_code == 200: yalbum = json.loads(result.content) yalbum['title'] = item['title'] yalbum['summary'] = item['description'] yalbum_data = json.dumps(yalbum) result = urlfetch.fetch(url=url, payload=yalbum_data, method=urlfetch.PUT, headers={'Accept': 'application/json','Content-Type': 'application/json; charset=utf-8; type=entry;', 'Authorization':'OAuth ' + yaf_token}) item['yaf_id'] = a.yaf_id url = 'http://api-fotki.yandex.ru/api/users/protasov-s/albums/published/' result = urlfetch.fetch(url=url, headers={'Accept': 'application/json', 'Authorization':'OAuth ' + yaf_token}) data = json.loads(result.content) for a in data['entries']: y_albums[a['id']] = a template_values = { 'auth':auth, 'menu':MENU, 'active':'index', 'albums':albums, 'url':url, 'y_albums':y_albums } template = JINJA_ENVIRONMENT.get_template('index.tpl') html = template.render(template_values) self.response.write(html)
def maps(loc1, loc2): bing_maps_key = "PUT_KEY_HERE" r = requests.get( 'http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0=' + loc1 + '&wp.1=' + loc2 + '&key=' + bing_maps_key) r_dict = r.json() route_obj = r_dict["resourceSets"][0]["resources"][0] route_dir = route_obj["routeLegs"][0]["itineraryItems"] directions_list = [] i = -1 for r_instr in route_dir: i += 1 dir_obj = {} r_text = r_instr["instruction"]["text"] dir_obj["coords"] = "(" + str( r_instr["maneuverPoint"]["coordinates"][0]) + ", " + str( r_instr["maneuverPoint"]["coordinates"][1]) + ")" # get hints try: dir_obj["hint"] = r_instr["hints"][1]["text"] + "." except: dir_obj["hint"] = "" # FLICKR api_key = "PUT_FLICKR_KEY_HERE" api_secret = "PUT_FLICKR_SECRET_HERE" flickr = FlickrAPI(api_key, api_secret, '/') auth_props = flickr.get_authentication_tokens() auth_url = auth_props['auth_url'] oauth_token = auth_props['oauth_token'] oauth_token_secret = auth_props['oauth_token_secret'] photo_json = flickr.get('flickr.photos.search', params={ 'api_key': api_key, 'lat': r_instr["maneuverPoint"]["coordinates"][0], 'lon': r_instr["maneuverPoint"]["coordinates"][1], 'radius': '0.01' }) photos = photo_json['photos']['photo'] photo = random.choice(photos) flickr_image_url = 'https://farm' + str( photo['farm']) + '.staticflickr.com/' + str( photo['server']) + '/' + str( photo['id']) + '_' + photo['secret'] + '.jpg' # PROJECT OXFORD oxford_key = "PUT_KEY_HERE" headers = { # Request headers 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': oxford_key, } params = urllib.urlencode({ # Request parameters 'visualFeatures': 'All', }) try: conn = httplib.HTTPSConnection('api.projectoxford.ai') conn.request("POST", "/vision/v1/analyses?%s" % params, '{ "Url":"' + flickr_image_url + '"}', headers) ox_response = conn.getresponse() ox_data = ox_response.read() ox_json = json.loads(ox_data) #print(ox_data) conn.close() except Exception as e: print("[Errno {0}] {1}".format(e.errno, e.strerror)) # combine directions + descriptions try: if ox_json["categories"][0]["name"] != "others_": r_text = r_text + " near this " + ox_json["categories"][0][ "name"] #print flickr_image_url else: pass except: #print "program failed because of oxford. IMAGE URL \n" #print flickr_image_url #print "OXFORD RESPONSE: \n" #print ox_data pass r_text = r_text + ". " r_text = r_text.replace("_", " ") r_text = r_text.replace("abstract", "landmark") r_text = r_text.replace("outdoor", "outdoor area") r_text = r_text.replace(" .", ".") print r_text dir_obj["r_text"] = r_text dir_obj["url"] = flickr_image_url directions_list.append(dir_obj) #return json.dumps(directions_list) return render_template("maps.html", my_list=directions_list)
import sys import tqdm from flickr import FlickrAPI auth_file = 'AUTH.key' auth = dict([map(lambda x: x.strip(), l.split('=')) \ for l in open(auth_file).readlines()]) if not 'KEY' in auth: print 'please create an api key in the flickr app-garden and add to AUTH.key' raise if not 'TOKEN' in auth: f = FlickrAPI(api_key=auth['KEY'], api_secret=auth['SECRET'], callback_url='http://www.example.com/callback/') visit = f.get_authentication_tokens()['auth_url'] print 'please visit %s and add TOKEN and VERIFIER to AUTH.key'%visit raise f = FlickrAPI(api_key=auth['KEY'], api_secret=auth['SECRET'], oauth_token=auth['TOKEN'], oauth_token_secret=auth['VERIFIER']) page = 1 photos_per_page = 500 all_photos = [] stop_next = False
class Uploader(object): """ """ consumer_key = "9d0b69163c0ae8ddfe7a3d0582874b57" consumer_secret = "899b3a6340d8cb56" request_token_url = 'https://www.flickr.com/services/oauth/request_token' access_token_url = 'https://www.flickr.com/services/oauth/access_token' authorize_url = 'https://www.flickr.com/services/oauth/authorize' callback = 'http://test.beta.upcnet.es/callback' base_url = 'https://api.flickr.com/services/rest' upload_url = 'https://up.flickr.com/services/upload' session_access_token = '72157637665662093-bebe07e6387e9e87' session_access_token_secret = '6aff330f2cb038fc' def __init__(self, database, root): self.session = OAuth1Session( self.consumer_key, self.consumer_secret, self.session_access_token, self.session_access_token_secret ) self.flickr = FlickrAPI( api_key=self.consumer_key, api_secret=self.consumer_secret, oauth_token=self.session_access_token, oauth_token_secret=self.session_access_token_secret ) self.database = database self.root = os.path.realpath(root) self.get_collections_tree() def extract_parts(self, filename): parts = filename.split(self.root)[1].lstrip('/').split('/') image_title = parts[-1] photoset_title = parts[-2] collection_tree = parts[:-2] return (collection_tree, photoset_title, image_title) def session_post(self, method, params={}): params.update({'method': method, 'format': 'json'}) resp = self.session.post(self.base_url, data={}, params=dict_to_qs(params), verify=False) if resp.status_code != 200: print resp.status_code, resp.content return json.loads(re.sub(r'jsonFlickrApi\((.*)\)', r'\1', resp.content)) def session_get(self, method, params={}): params.update({'method': method, 'format': 'json'}) resp = self.session.get(self.base_url, params=dict_to_qs(params), verify=False) if resp.status_code != 200: print resp.status_code, resp.content return json.loads(re.sub(r'jsonFlickrApi\((.*)\)', r'\1', resp.content)) def get_collections_tree(self): self.collections = {} resp = self.session_get("flickr.collections.getTree") root = resp['collections'] self.recurse_collections(root, self.collections) def recurse_collections(self, node, base_col): for collection in node.get('collection', []): collection_title = unicodedata.normalize('NFD', collection['title']) base_col[collection_title] = dict(collection) base_col[collection_title]['type'] = 'collection' self.recurse_collections(collection, base_col[collection_title]) for album in node.get('set', []): album_title = unicodedata.normalize('NFD', album['title']) base_col[album_title] = dict(album) base_col[album_title]['type'] = 'set' def create_collection(self, name, node, description=''): print 'Creating collection {} in {}'.format(name, node.get('title', 'Flickr collections root')) options = {'title': name} if 'id' in node: options['parent_id'] = node['id'] resp = self.session_post('flickr.collections.create', params=options) new_collection_id = resp['collection']['id'] node[name] = { 'title': name, 'description': description, 'type': 'collection', 'id': new_collection_id } def rename_photoset(self, photoset, new_title): options = dict( photoset_id=photoset['id'], title=new_title ) resp = self.session_post('flickr.photosets.editMeta', params=options) return resp def delete_photoset(self, photoset): if photoset: options = dict( photoset_id=photoset['id'], ) resp = self.session_post('flickr.photosets.delete', params=options) return resp def put_set_in_collection(self, collection, photoset=None): collection_dicts = [ps for ps_title, ps in collection.items() if isinstance(ps, dict)] current_photosets = [ps for ps in collection_dicts if ps.get('type') == 'set'] if photoset: current_photosets.append(photoset) sorted_photosets = sorted(current_photosets, key=lambda x: x['title']) sorted_photoset_ids = [a['id'] for a in sorted_photosets] options = dict( collection_id=collection['id'], photoset_ids=','.join(sorted_photoset_ids), do_remove='0' ) resp = self.session_get('flickr.collections.editSets', params=options) if photoset: collection[photoset['title']] = photoset return resp def put_photo_in_photoset(self, photoset, photo_id, photo_title): try: options = dict( photoset_id=photoset['id'], photo_id=photo_id, ) resp = self.session_post('flickr.photosets.addPhoto', params=options) photoset['photos'][photo_title] = photo_id if resp['stat'] == 'ok': return True else: if 'not found' in resp['message']: return 'deleted' return False except: return False def set_uploaded_date(self, photo_id, filename): """ Set posted date to uploaded date ** except ** for photos taken before user flickr sign up date. This is a flickr restriction. Membership date will be used as posted date with this photos, to avoid appearing on top of photostream. """ try: membership_date = datetime.datetime(2007, 10, 23, 0, 0, 0).strftime('%s') tags = exifread.process_file(open(filename)) tag = tags.get('EXIF DateTimeOriginal') if tag: date_taken_parts = re.search(r'(\d{,4}):(\d{,2})\:(\d{,2}) (\d{,2}):(\d{,2}):(\d{,2})', tag.values).groups() try: taken_date = datetime.datetime(*[int(a) for a in date_taken_parts]).strftime('%s') except: taken_date = membership_date else: taken_date = membership_date options = dict( photo_id=photo_id, date_posted=taken_date if int(taken_date) > int(membership_date) else membership_date ) self.session_post('flickr.photos.setDates', params=options) except: print "X Could not change posted date to taken date" def upload_photo(self, filename): if DO_UPLOAD: files = open(filename, 'rb') options = { 'title': filename.split('/')[-1], 'is_public': '0', 'content_type': '1' } start = time.time() try: added_photo = self.flickr.post(params=options, files=files) end = time.time() elapsed = int(end - start) filesize = os.path.getsize(filename) speed = (filesize / elapsed) / 1000 print ' --> %.2f KB/s' % speed if added_photo.get('stat', False) == 'ok': return added_photo.get('photoid', None) else: return None except: return None def create_photoset(self, node, name, photo_id, photo_title, description=''): options = dict( title=name, primary_photo_id=photo_id ) try: resp = self.session_post('flickr.photosets.create', params=options) new_set_id = resp['photoset']['id'] new_photoset = { 'title': name, 'description': description, 'type': 'set', 'id': new_set_id, 'photos': {photo_title: photo_id} } self.put_set_in_collection(node, new_photoset) # CHEK if failed # message "Invalid primary photo id" means photo is not really uploaded, so # db is inconsistent, maybe the user has deleted the photo on flickr return new_set_id except: return False def get_collection(self, parts, create=True): node = self.collections for pos, part in enumerate(parts): if not node.get(part, False): if create: self.create_collection(part, node) else: return None node = node[part] return node def get_photoset(self, parents, photoset_title, create=True): collection_node = self.get_collection(parents, create=create) if collection_node: photoset_node = collection_node.get(photoset_title, None) return photoset_node else: return {} def load_photoset_photos(self, photoset): if 'photos' not in photoset: options = dict( photoset_id=photoset['id'] ) resp = self.session_get('flickr.photosets.getPhotos', params=options) photoset['photos'] = dict([(a['title'], a['id']) for a in resp['photoset']['photo']]) def upload(self, filename): if filename.startswith(self.root): collection_tree, photoset_title, image_title = self.extract_parts(filename) photoset_node = self.get_photoset(collection_tree, photoset_title) if photoset_node is not None: # We have an existing photoset, so get the photo list # and upload the photo if not already in self.load_photoset_photos(photoset_node) photos_currently_in_photoset = set(photoset_node['photos'].keys()) photos_currently_in_folder = set([a for a in os.listdir(filename[:filename.rfind('/')]) if a.lower().endswith('jpg')]) missing_in_disk = photos_currently_in_photoset - photos_currently_in_folder # UNUSED CODE, to populate all files into db # for photo_title, photo_id in photoset_node['photos'].items(): # print 'updated', photo_title # self.database.update_photo(photo_title, photoset_title, 'uploaded', 1) # self.database.update_photo(photo_title, photoset_title, 'flickr_photo_id', photo_id) # self.database.update_photo(photo_title, photoset_title, 'flickr_photoset_id', photoset_node['id']) # self.database.update_photo(photo_title, photoset_title, 'date_uploaded', '1384815600') # Get the photo from the database, to check if its flickr_id is assigned in the photoset db_photo = self.database.get_photo_by_filename_and_album(image_title, photoset_title) if bool(db_photo['uploaded']): # The photo was uploaded to flickr in the past ... if LOG_EXISTING: print '> {} already uploaded on flickr on {}'.format(image_title, datetime.datetime.utcfromtimestamp(int(db_photo['date_uploaded'])).isoformat().replace('T', ' ')) if db_photo['flickr_photo_id'] not in photoset_node['photos'].values(): # But the photo was not assigned to this photoset, so put it success = self.put_photo_in_photoset(photoset_node, db_photo['flickr_photo_id'], image_title) if success is True: print '> {} Assigning already uploaded photo {} to photoset'.format(image_title, datetime.datetime.utcfromtimestamp(int(db_photo['date_uploaded'])).isoformat().replace('T', ' ')) self.database.update_photo(image_title, photoset_title, 'flickr_photoset_id', photoset_node['id']) self.set_uploaded_date(db_photo['flickr_photo_id'], filename) else: if success == 'deleted': print u'> Photo {} is not in flickr photoset {}, resetting_photo and... try later'.format(image_title, photoset_title) self.database.update_photo(image_title, photoset_title, 'uploaded', 0) self.database.update_photo(image_title, photoset_title, 'flickr_photoset_id', '') self.database.update_photo(image_title, photoset_title, 'flickr_photo_id', '') self.database.update_photo(image_title, photoset_title, 'date_uploaded', '') else: print u'X Error assigning photo {} to photoset {}, try later'.format(image_title, photoset_title) else: # And is assigned to this photoset, do nothing if LOG_EXISTING: print u'> {} already in photoset {}'.format(image_title, photoset_title) else: # This photo is not uploaded to flickr, so upload it sys.stdout.write(u'> Uploading {} to existing photoset {}'.format(image_title, photoset_node['title'])) sys.stdout.flush() new_photo_id = self.upload_photo(filename) if new_photo_id: self.database.update_photo(image_title, photoset_title, 'uploaded', 1) self.database.update_photo(image_title, photoset_title, 'date_uploaded', datetime.datetime.now().strftime('%s')) self.database.update_photo(image_title, photoset_title, 'flickr_photo_id', new_photo_id) self.set_uploaded_date(new_photo_id, filename) success = self.put_photo_in_photoset(photoset_node, new_photo_id, image_title) if success: self.database.update_photo(image_title, photoset_title, 'flickr_photoset_id', photoset_node['id']) else: print u'X Error assigning photo {} to photoset {}, try later'.format(image_title, photoset_title) else: print u'X Error posting {}, try later'.format(image_title) else: # We have a new album, so first we upload the photo # and next create the photoset with that photo as primary # In the case of first/all photos already uploaded but flickr album missing # just put photo in photoset: # Try get the photo from the database, to check if its uploaded collection_node = self.get_collection(collection_tree) db_photo = self.database.get_photo_by_filename_and_album(image_title, photoset_title) if bool(db_photo['uploaded']): print u'> Assigning already uploaded photo {} to new photoset {}'.format(image_title, photoset_title) new_photoset_id = self.create_photoset(collection_node, photoset_title, db_photo['flickr_photo_id'], image_title) if new_photoset_id: self.database.update_photo(image_title, photoset_title, 'flickr_photoset_id', new_photoset_id) self.set_uploaded_date(db_photo['flickr_photo_id'], filename) else: print u'X Error creating photoset {}, try later'.format(photoset_title) else: sys.stdout.write(u'> Uploading {} to new photoset {}'.format(image_title, photoset_title)) sys.stdout.flush() new_photo_id = self.upload_photo(filename) if new_photo_id: self.database.update_photo(image_title, photoset_title, 'uploaded', 1) self.database.update_photo(image_title, photoset_title, 'date_uploaded', datetime.datetime.now().strftime('%s')) self.database.update_photo(image_title, photoset_title, 'flickr_photo_id', new_photo_id) self.set_uploaded_date(new_photo_id, filename) new_photoset_id = self.create_photoset(collection_node, photoset_title, new_photo_id, image_title) if new_photoset_id: self.database.update_photo(image_title, photoset_title, 'flickr_photoset_id', new_photoset_id) else: print u'X Error creating photoset {}, try later'.format(photoset_title) else: print u'x Error posting {}, try later'.format(image_title)
import cPickle as pickle from flickr import FlickrAPI from pprint import pprint # my_api_key='33a85d0de9040f3d12ed5d17bad02210' # my_api_secret='0a255e264884b059' my_api_key = 'af997fbb975e9e112d3d96b64f6d3ab5' my_api_secret = '384e545fe39b978e' # Part 1 - get online authorisation first_api_handle = FlickrAPI(api_key=my_api_key, api_secret=my_api_secret, callback_url='http://bowsy.me.uk/test/test.php') auth_props = first_api_handle.get_authentication_tokens(perms='delete') auth_url = auth_props['auth_url'] oauth_token = auth_props['oauth_token'] oauth_token_secret = auth_props['oauth_token_secret'] print 'Connect with Flickr via: %s' % auth_url # Get final tokens oauth_token_returned=raw_input('oauth_token_returned=') oauth_verifier_returned=raw_input('oauth_verifier_returned=') second_api_handle = FlickrAPI(api_key=my_api_key, api_secret=my_api_secret, oauth_token=oauth_token, oauth_token_secret=oauth_token_secret )
class FlickrObject(OAuthObjectBase): def __init__(self): OAuthObjectBase.__init__(self) self.apiName = "flickr" def authPart1(self): f = FlickrAPI(api_key=self.api_key, api_secret=self.api_secret, callback_url= web.ctx.homedomain + '/auth/flickrAuth?') auth_props = f.get_authentication_tokens(perms='write') auth_url = auth_props['auth_url'] #Store this token in a session or something for later use in the next step. self.oauth_token = auth_props['oauth_token'] self.oauth_token_secret = auth_props['oauth_token_secret'] return auth_url def authPart2(self, oauth_token, oauth_verifier): f = FlickrAPI(api_key=self.api_key, api_secret=self.api_secret, oauth_token=self.oauth_token, oauth_token_secret=self.oauth_token_secret) authorised_tokens = f.get_auth_tokens(oauth_verifier) self.final_oauth_token = authorised_tokens['oauth_token'] self.final_oauth_token_secret = authorised_tokens['oauth_token_secret'] authDict = authPickler.getAuthDict(self.apiName) authDict["final_oauth_token"] = self.final_oauth_token authDict["final_oauth_token_secret"] = self.final_oauth_token_secret authPickler.setAuthDict(self.apiName, authDict) self.authorised = True self.setupAPI() return redirectToUrlText(web.ctx.homedomain) def setupAPI(self): self.flickrAPI = FlickrAPI(api_key=self.api_key, api_secret=self.api_secret, oauth_token=self.final_oauth_token, oauth_token_secret=self.final_oauth_token_secret) def getGroups(self, params=None): try: firstQuery = self.flickrAPI.get("flickr.groups.pools.getGroups", params) nPages = firstQuery["groups"]["pages"] groupList = [] if nPages > 0: groupList += firstQuery["groups"]["group"] if nPages > 1: paramPerPage = params for pageNum in range(2, nPages+1): paramPerPage["page"] = pageNum pageQuery = self.flickrAPI.get("flickr.groups.pools.getGroups", params) groupList += pageQuery["groups"]["group"] retDict = {} for group in groupList: retDict[group["nsid"]] = group ["name"] imagedb.populateFlickrGroups(retDict) except: pass def getSets(self, params=None): try: firstQuery = self.flickrAPI.get("flickr.photosets.getList", params) nPages = firstQuery["photosets"]["pages"] setList = [] if nPages > 0: setList += firstQuery["photosets"]["photoset"] if nPages > 1: paramPerPage = params for pageNum in range(2, nPages+1): paramPerPage["page"] = pageNum pageQuery = self.flickrAPI.get("flickr.photosets.getList", params) setList += pageQuery["photosets"]["photoset"] retDict = {} for pset in setList: retDict[pset["id"]] = pset ["title"]["_content"] imagedb.populateFlickrSets(retDict) except: pass def postImage(self, jobDict): imageLoc = jobDict["fileurl"] paramsDict = {} if jobDict["title"] != None: paramsDict["title"] = jobDict["title"] if jobDict["description"] != None: paramsDict["description"] = jobDict["description"] if jobDict["tags"] != []: paramsDict["tags"] = ' '.join(map(lambda y: '"' + y + '"', jobDict["tags"])) print "jobDict", jobDict print "paramsDict", paramsDict files = open(imageLoc, 'rb') add_photo = self.flickrAPI.post(params=paramsDict, files=files) #print "PHOTO_ID", add_photo["photoid"] if add_photo["stat"] != "ok": raise Exception("Photo failed to upload, flickr did not return ok stat") return add_photo["photoid"] def setLocation(self, photoId, latitude, longitude): params = {"photo_id":photoId, "lat":latitude, "lon":longitude} result = self.flickrAPI.get("flickr.photos.geo.setLocation", params) def sendToGroups(self, photoId, groupList): if groupList == None: return for group in groupList: params = {"photo_id":photoId, "group_id":group} result = self.flickrAPI.get("flickr.groups.pools.add", params) if result["stat"] != "ok": print "error adding photo " + photoId + " to group " + group def sendToSets(self, photoId, setList): if setList == None: return for flickrSet in setList: params = {"photo_id":photoId, "photoset_id":flickrSet} result = self.flickrAPI.get("flickr.photosets.addPhoto", params) if result["stat"] != "ok": print "error adding photo " + photoId + " to set " + flickrSet def getPhotoInfo(self, photoId): params = {"photo_id":photoId} result = self.flickrAPI.get("flickr.photos.getInfo", params) if result["stat"] != "ok": print "cannot get photo info for photo " + photoId return result["photo"] def getShortURL(self, photoId): num=int(photoId) a='123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' bc=len(a) enc='' while num>=bc: div,mod=divmod(num,bc) enc = a[mod]+enc num = int(div) enc = a[num]+enc return "http://flic.kr/p/" + enc def getViewedPhotosToday(self, params={}): try: if "date" not in params.keys(): datestamp = getStartOfDay() params["date"] = datestamp else: datestamp = params["date"] params["per_page"] = 100 firstQuery = self.flickrAPI.get("flickr.stats.getPopularPhotos", params) nPages = firstQuery["photos"]["pages"] photoList = [] if nPages > 0: photoList += firstQuery["photos"]["photo"] if nPages > 1: paramPerPage = params for pageNum in range(2, nPages+1): paramPerPage["page"] = pageNum pageQuery = self.flickrAPI.get("flickr.stats.getPopularPhotos", params) photoList += pageQuery["photos"]["photo"] #only want the views & photo id & date returnList = [] for photo in photoList: returnList.append (( photo["id"], photo["stats"]["views"] )) returnDict = { "datestamp":datestamp, "viewed":returnList } return returnDict except: pass def getPhotoImageSizes(self, photoId): params = {"photo_id":photoId} result = self.flickrAPI.get("flickr.photos.getSizes", params) if result["stat"] != "ok": print "cannot get photo info for photo " + photoId return result["sizes"]["size"] def getTagsForPhoto(self, photoId): params = {"photo_id":photoId} result = self.flickrAPI.get("flickr.tags.getListPhoto", params) if result["stat"] != "ok": print "cannot get photo info for photo " + photoId tags = [] for tag in result["photo"]["tags"]["tag"]: tags.append(tag["raw"]) return tags