mercredi 19 avril 2017

Flickr API Photo ID Issue

I am having issues with parsing through data after an API request to Flickr. Below is my function that makes a request to flickr based on a given search criteria for 10 photos. And the prints out a messy JSON dictionary. I defined a class that will look through that given JSON and it is suppose to return a list of the photo ids. However, this is not occurring and I am not getting any errors. Any help would be great.

def get_flickr_data(search_data,photo_number=10):
    baseurl ="http://ift.tt/MVfi6Q"
    params_d={}
    params_d["text"]=search_data
    params_d["per_page"]=photo_number
    params_d["format"]= "json"
    params_d["method"]= "flickr.photos.search"
    params_d["tag_mode"]= "all"
    params_d["api_key"] = FLICKR_KEY
    unique_indent = params_unique_combination(baseurl,params_d)
    if unique_indent in CACHE_DICTION:
        flickr_info = CACHE_DICTION[unique_indent]
    else:
        resp = requests.get(baseurl,params_d)
        python_flickr = json.loads(resp.text[14:-1])
        CACHE_DICTION[unique_indent] = python_flickr
        f = open(CACHE_FNAME,"w")
        cache_str_tmp = json.dumps(CACHE_DICTION)
        f.write(cache_str_tmp)
        f.close()
    return CACHE_DICTION[unique_indent]

for word in all_first_words: 
    print get_flickr_data(word)

JSON:

{"photos": {"total": "15773", "photo": [{"isfamily": 0, "title": "Gullfoss.", "farm": 3, "ispublic": 1, "server": "2819", "isfriend": 0, "secret": "37da5017a7", "owner": "69226321@N03", "id": "33933758702"}, {"isfamily": 0, "title": "Eldv\u00c3\u00b6rp - Still hot after more than 800 years", "farm": 3, "ispublic": 1, "server": "2847", "isfriend": 0, "secret": "b7bda827a4", "owner": "62657714@N00", "id": "34026557576"}, {"isfamily": 0, "title": "Granite-gneiss over limestone, Cowaramup Bay, near Margaret River, WA, 13/02/17", "farm": 4, "ispublic": 1, "server": "3941", "isfriend": 0, "secret": "253eb4fb80", "owner": "58828131@N07", "id": "33931062741"}, {"isfamily": 0, "title": "Limestone and granite-gneiss, Cowaramup Bay, near Margaret River, WA, 13/02/17", "farm": 3, "ispublic": 1, "server": "2948", "isfriend": 0, "secret": "8e2a954bcf", "owner": "58828131@N07", "id": "33675713670"}

Class:

class Photo():
    def __init__(self,CACHE_DICTION):
        empty_list =[]
        empty_list2 =[]
        empty_list3 =[]
        for photos in CACHE_DICTION['photos']['photo']:
            empty_list.append(photos["id"])
            empty_list2.append(photos["title"])
            empty_list3.append(photos["owner"])
        self.owner = empty_list3
        self.title = empty_list2
        self.photos = empty_list
    def emptylist(self,photos):
        for items in empty_list:
            print items  # prints a list of photo ids 
            print get_flickr_data(items)






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire