RyanKilkelly February 2016

Run only if geo-info available in python

Here is a snippet of the code:

import flickrapi

api_key = "xxxxxxxxxxxxxxxxxxxxx"
secret_api_key = "xxxxxxxxxx"
flickr = flickrapi.FlickrAPI(api_key, secret_api_key)

def obtainImages3():

    group_list = flickr.groups.search (api_key=api_key, text = 'Paris', per_page = 10)

    for group in group_list[0]:

        group_images = flickr.groups.pools.getPhotos (api_key=api_key, group_id = group.attrib['nsid'], extras = 'geo, tags, url_s')

        for image in group_images[0]:
        url = image.attrib['url_s']
        tags = image.attrib['tags']

        if image.attrib['geo'] != 'null':

            photo_location = flickr.photos_geo_getLocation(photo_id=image.attrib['id'])
            lat = float(photo_location[0][0].attrib['latitude'])
            lon = float(photo_location[0][0].attrib['longitude'])

I want to get information about images if and only if they have a geo-tag connected to them. I tried to do this with the line if image.attrib['geo'] != 'null' but I don't think this works. Can anyone suggest a way I might be able to do it, thanks in advance!

Answers


raggingfox February 2016

Replace your if image.attrib['geo']!='null' condition with a try and exception block as below. Since the API returns the data in JSON format you can check the presence of key using:

try:
    image.attrib['geo']
    photo_location=flickr.photos_geo_getLocation(photo_id=image.attrib['id'])
    lat = float(photo_location[0][0].attrib['latitude'])
    lon = float(photo_location[0][0].attrib['longitude'])
except KeyError:
    pass

Post Status

Asked in February 2016
Viewed 2,853 times
Voted 14
Answered 1 times

Search




Leave an answer