gists/danbooru_atomizer/get.py
2018-10-11 16:45:30 +02:00

19 lines
563 B
Python

from retry import retry
import requests, requests.exceptions
class StatusCodeError(Exception):
def __init__(self, code, url):
self.code = code
self.url = url
def __str__(self):
return 'request for {} returned status code {}'.format(self.url, self.code)
@retry((requests.exceptions.ConnectionError, StatusCodeError, ValueError), tries=6, wait=300)
def get(uri, json=False):
r = requests.get(uri)
if r.status_code != 200:
raise StatusCodeError(r.status_code, uri)
if json:
return r.json()
return r