diff --git a/flickr/flickr.py b/flickr/flickr.py index 252391d..b1836f4 100644 --- a/flickr/flickr.py +++ b/flickr/flickr.py @@ -17,11 +17,11 @@ fh.setFormatter(formatter) logger.addHandler(fh) class Flickr(InputPlugin): - + name = "flickr" hasWizard = False - - + + def __init__(self): #Try and read the labels file labels_filename = self.name+".labels" @@ -32,25 +32,27 @@ def __init__(self): logger.debug("Trying to load the labels file for the "+self.name+" plugin .") self.labels = labels_config['labels'] except Exception,err: - self.labels = None - logger.error("Could not load the labels file for the "+self.name+" plugin .") - logger.exception(err) + self.labels = None + logger.error("Could not load the labels file for the "+self.name+" plugin .") + logger.exception(err) self.config, self.options_string = self.readConfiguration("string_options") - self.api = flickrapi.FlickrAPI(self.options_string["hidden_api_key"]) + logger.debug("Loaded " + self.name+" plugin string_options.") + self.api = flickrapi.FlickrAPI(self.options_string["hidden_api_key"], self.options_string["hidden_api_secret"], self.options_string["username"]) + logger.debug("Loaded FlickrAPI, recieved an obect back, we think.") def searchForTargets(self, search_term): - + possibleTargets = [] try: - + # Try to distinguish between mail and username in the search term - if re.match("[\w\-\.+]+@(\w[\w\-]+\.)+[\w\-]+", search_term): + if re.match("[\w\-\.+]+@(\w[\w\-]+\.)+[\w\-]+", search_term): results = self.api.people_findByEmail(find_email=search_term) else: results = self.api.people_findByUsername(username=search_term) - + for userid in results.find('user').items(): possibleTargets.append(self.getUserInfo(userid[1])) - + except Exception, e: logger.error(e) if e.message == 'Error: 1: User not found': @@ -61,11 +63,11 @@ def searchForTargets(self, search_term): return [dict(t) for t in set([tuple(d.items()) for d in possibleTargets])] else: return [] - + def getUserInfo(self, userId): """ Retrieves a target's username, real name and location as provided by flickr API - + Returns a target dictionary """ try: @@ -87,19 +89,20 @@ def getUserInfo(self, userId): logger.error("Error getting target info from Flickr for target "+userId) logger.error(err) return None - + def isConfigured(self): try: if not self.options_string: self.options_string = self.readConfiguration("string_options")[1] - api = flickrapi.FlickrAPI(self.options_string["hidden_api_key"]) - api.people_findByUsername(username="testAPIKey"); + #api = flickrapi.FlickrAPI(self.options_string["hidden_api_key"]) + api = flickrapi.FlickrAPI(self.options_string["hidden_api_key"], self.options_string["hidden_api_secret"], self.options_string["username"]) + api.people_findByUsername(username="chi-diver"); return (True, "") except Exception, e: logger.error("Error establishing connection to Flickr API.") logger.error(e) return (False, "Error establishing connection to Flickr API. ") - + def getPhotosByPage(self, userid, page_nr): try: results = self.api.people_getPublicPhotos(user_id=userid, extras="geo, date_taken", per_page=500, page=page_nr) @@ -108,8 +111,8 @@ def getPhotosByPage(self, userid, page_nr): except Exception , err: logger.error("Error getting photos per page from Flickr") logger.error(err) - - def getLocationsFromPhotos(self, photos): + + def getLocationsFromPhotos(self, photos): locations = [] if photos: for photo in photos: @@ -131,14 +134,14 @@ def getLocationsFromPhotos(self, photos): locations.append(loc) except Exception,err: logger.error(err) - return locations - + return locations + def returnLocations(self, target, search_params): photosList = [] locationsList = [] try: results = self.api.people_getPublicPhotos(user_id=target['targetUserid'], extras="geo, date_taken", per_page=500) - + if results.attrib['stat'] == 'ok': res = results.find('photos') total_photos = res.attrib['total'] @@ -149,22 +152,22 @@ def returnLocations(self, target, search_params): photosList.extend(self.getPhotosByPage(target['targetUserid'], i)) else: photosList = results.find('photos').findall('photo') - + locationsList = self.getLocationsFromPhotos(photosList) return locationsList - + except FlickrError, err: logger.error("Error getting locations from Flickr") logger.error(err) - - + + def constructContextInfoWindow(self, link, date): html = unicode(self.options_string['infowindow_html'], 'utf-8') return html.replace("@LINK@",link).replace("@DATE@",date.strftime("%Y-%m-%d %H:%M:%S %z")).replace("@PLUGIN@", u"flickr") - + def getLabelForKey(self, key): ''' - read the plugin_name.labels + read the plugin_name.labels file and try to get label text for the key that was asked ''' if not self.labels: @@ -172,4 +175,3 @@ def getLabelForKey(self, key): if not key in self.labels.keys(): return key return self.labels[key] - \ No newline at end of file