2020-11-16 02:30:33 +00:00
|
|
|
import logging
|
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
level=logging.DEBUG)
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2020-11-23 21:36:31 +00:00
|
|
|
GOOGLEBOT_USER_AGENT = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
|
|
|
|
GOOGLEBOT_IP = '66.249.66.1'
|
|
|
|
TIMEOUT = 30
|
2020-11-16 02:30:33 +00:00
|
|
|
|
2020-11-23 21:36:31 +00:00
|
|
|
def xml(route, ref=None, headers=dict(), use_googlebot=True):
|
2020-11-16 02:30:33 +00:00
|
|
|
try:
|
2020-11-23 21:36:31 +00:00
|
|
|
if use_googlebot:
|
|
|
|
headers['User-Agent'] = GOOGLEBOT_USER_AGENT
|
|
|
|
headers['X-Forwarded-For'] = GOOGLEBOT_IP
|
|
|
|
r = requests.get(route(ref), headers=headers, timeout=TIMEOUT)
|
2020-11-16 02:30:33 +00:00
|
|
|
if r.status_code != 200:
|
|
|
|
raise Exception('Bad response code ' + str(r.status_code))
|
|
|
|
return r.text
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
|
|
|
except BaseException as e:
|
|
|
|
logging.error('Problem hitting URL: {}'.format(str(e)))
|
|
|
|
return False
|
|
|
|
|
2020-11-23 21:36:31 +00:00
|
|
|
def json(route, ref=None, headers=dict(), use_googlebot=True):
|
2020-11-16 02:30:33 +00:00
|
|
|
try:
|
2020-11-23 21:36:31 +00:00
|
|
|
if use_googlebot:
|
|
|
|
headers['User-Agent'] = GOOGLEBOT_USER_AGENT
|
|
|
|
headers['X-Forwarded-For'] = GOOGLEBOT_IP
|
|
|
|
r = requests.get(route(ref), headers=headers, timeout=TIMEOUT)
|
2020-11-16 02:30:33 +00:00
|
|
|
if r.status_code != 200:
|
|
|
|
raise Exception('Bad response code ' + str(r.status_code))
|
|
|
|
return r.json()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
|
|
|
except BaseException as e:
|
|
|
|
logging.error('Problem hitting URL: {}'.format(str(e)))
|
|
|
|
return False
|