utils.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:script.module.metadatautils 作者: marcelveldt 项目源码 文件源码
def get_json(url, params=None, retries=0, ratelimit=None):
    '''get info from a rest api'''
    result = {}
    if not params:
        params = {}
    # apply rate limiting if needed
    rate_limiter(ratelimit)
    try:
        response = requests.get(url, params=params, timeout=20)
        if response and response.content and response.status_code == 200:
            result = json.loads(response.content.decode('utf-8', 'replace'))
            if "results" in result:
                result = result["results"]
            elif "result" in result:
                result = result["result"]
        elif response.status_code in (429, 503, 504):
            raise Exception('Read timed out')
    except Exception as exc:
        result = None
        if "Read timed out" in str(exc) and retries < 5 and not ratelimit:
            # retry on connection error or http server limiting
            monitor = xbmc.Monitor()
            if not monitor.waitForAbort(2):
                result = get_json(url, params, retries + 1)
            del monitor
        else:
            log_exception(__name__, exc)
    # return result
    return result
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号