m-wrzr / populartimes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A more graceful result for a failed request.

raffiebr opened this issue · comments

I use populartimes.getid() and periodically pull the populartimes data using a list of place_ids I have saved. At times, a place_id may get depreciated. This will cause a problem because as long as either the API KEY or the place id cannot be recognized by google, the API call will breakdown and the following error msg printed:
ERROR:root:The query string is malformed, check params.json if your formatting for lat/lng and radius is correct.
Kernel died, restarting

Reproducible example:
populartimes.get_id(API_KEY1,"hoho")

Is there a way to make the function fail more gracefully? For example, just output an empty list?

I've faced a similar error, you could try a simple try and except? something like this:

import populartimes
import sys
API_KEY='YOUR_API_KEY'
try:
    pt=populartimes.get_id(API_KEY,"hoho")
except populartimes.crawler.PopulartimesException as e:
    e=str(e)
    print(e)
    '''
    #Now you have the error, you can handle it as you want
    #If you wanted to catch that specific error you could try something
    #like this, I'm not sure if this is the best way to do it, but it kinda
    #works!
    e=e.split(",")
    e=e[0]
    e=e.replace("(","")
    e=e.replace("'","")
    print(e)
    if e=="Google Places INVALID_REQUEST":
        #Handle this error as you want
    else:
        #Handle other
        
    '''

It doesn't work for me.

try:
populartimes.get_id(API_KEY, 'ChIJk85Oa40Z2jERvIeNvwD4E2s')
except populartimes.crawler.PopulartimesException as e:
e=str(e)
print(e)
e=e.split(",")
e=e[0]
e=e.replace("(","")
e=e.replace("'","")
print(e)

However, all I get is "Kernel died, restarting" and nothing prints out.

Try just printing e.

I've tried what you posted and works fine, I think that the problem has nothing to do with the python code.
Maybe that helps

@raffie- you are using an old version of populartimes, the error message you posted (below) isn't available anymore since commit a931eb7. You should update to the current version and than catch errors as @Moyaga suggested.

ERROR:root:The query string is malformed, check params.json if your formatting for lat/lng and radius is correct.

Okay I reinstalled the package and it works now! thanks!