Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to work with the changed flickr api #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions flickr/flickr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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':
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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']
Expand All @@ -149,27 +152,26 @@ 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:
return key
if not key in self.labels.keys():
return key
return self.labels[key]