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'})
# 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']) if ('collection' in json_dict): # A collection is _always_ a list so we need to process all of them for list_entry in json_dict['collection']: recursive_json_dict_walker(list_entry, (so_far))
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)
oauth_token=auth['TOKEN'], oauth_token_secret=auth['VERIFIER']) page = 1 photos_per_page = 500 all_photos = [] stop_next = False while True: print '\r%d'%len(all_photos), sys.stdout.flush() params = {'page': page, 'per_page' : photos_per_page, 'user_id' : 'me', 'extras': 'original_format'} try: result = f.get("flickr.people.getPhotos", params=params) except: continue ps = result['photos']['photo'] all_photos.extend(ps) page+=1 if stop_next: break if len(ps) != photos_per_page: stop_next = True urls = [] for a in tqdm.tqdm(all_photos): vals = a['farm'], a['server'], a['id'],a['originalsecret'],a['originalformat'] url = 'https://farm%s.staticflickr.com/%s/%s_%s_o.%s'% vals urls.append(url)
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)
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)
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