python - Imgur API: Dictionary values magically turns into None? -
i know voodoo magic isn't cause of - sure seems it!
i have following code snippets, making use of imgur api. imgur
object client imgur api uses , contains attribute credits
displays number of access credits user has on website.
imgur = imgurpython.imgurclient(client_id, client_secret)
calling:
imgur.credits
returns credits normal, i.e.:
{'clientlimit': 12500, 'userreset': 1503185179, 'userlimit': 500, 'userremaining': 0, 'clientremaining': 12000}
however when attempt call dictionary in later function:
def check_credits(imgur): ''' receives client - , if there not credits left, wait until credit refills - i.e. pause program ''' credits = imgur.credits credits_remaining = credits['userremaining'] reset_time = credits['userreset'] if credits_remaining < 10: print('not enough credits, remaining: %i' % credits_remaining) = int(dt.utcnow().timestamp()) wait_time = reset_time - print('waiting for: %i' % wait_time) time.sleep(wait_time)
sometimes values in dictionaries seem turn none
instead of integers supposed be. in case both reset_time
, credits_remaining
turn out none
. in order allow code run i'm having add try-catches on code , it's getting quite frustrating. way, function called whenever error imgurclientratelimiterror
, when imgur.credits['userremaining'] == 0
. i'm wondering if know why may have been case.
upon looking @ source code client seems updated automatically upon each request. updated values obtained response headers after call imgurclient.make_request
. header values obtained dict.get
can return none if key not exist in headers dictionary. code reference here: https://github.com/imgur/imgurpython/blob/master/imgurpython/client.py#l143
i not sure if these headers still used on errors 404 or 403 investigate further there. seems though because of behavior need either cache previous values or manually call imgurclient.get_credits method in these cases real values. whichever fix go you.
Comments
Post a Comment