def download(self): head = self._build_headers() status_code = -1 body = '' try: print self._url req = urlopen(urlrequest(self._url, headers=head), timeout=self._timeout) status_code = req.code body = req.read() except urlerror.HTTPError as E: code = E.getcode() # More portability hax (no reason property in 2.6?) try: reason = E.reason except AttributeError: reason = "Connection error" if code == 404: raise HTTPFileNotFoundError("File not found") elif code == 304: raise HTTPStale(str(self._last_modified)) else: raise HTTPError("Server connection failed: {0} ({1})".format( reason, code)) except (timeout, urlerror.URLError): raise HTTPTimeoutError("Server took too long to respond") lm = req.headers.get("last-modified") self._last_modified = lm return body
def download(self): head = self._build_headers() status_code = -1 body = '' try: print self._url req = urlopen(urlrequest(self._url, headers = head), timeout = self._timeout) status_code = req.code body = req.read() except urlerror.HTTPError as E: code = E.getcode() # More portability hax (no reason property in 2.6?) try: reason = E.reason except AttributeError: reason = "Connection error" if code == 404: raise HTTPFileNotFoundError("File not found") elif code == 304: raise HTTPStale(str(self._last_modified)) else: raise HTTPError("Server connection failed: {0} ({1})".format(reason, code)) except (timeout, urlerror.URLError): raise HTTPTimeoutError("Server took too long to respond") lm = req.headers.get("last-modified") self._last_modified = lm return body
def __new__(cls, paste, private=True, lang="plain", key="public", desc="", expire=0, views=0, encrypted=False): if not paste: raise PasteError("No paste provided") if expire and views: # API incorrectly returns success so we raise error locally raise PasteError("Options 'expire' and 'views' are mutually exclusive") request = urlrequest( "https://paste.ee/api", data=urlencode( { 'paste': paste, 'private': bool(private), 'language': lang, 'key': key, 'description': desc, 'expire': expire, 'views': views, 'encrypted': bool(encrypted), 'format': "json" } ).encode("utf-8"), headers={'User-Agent': 'Mozilla/5.0'} ) try: result = json.loads(urlopen(request).read().decode("utf-8")) return result["paste"] except urlerror.HTTPError: print("Couldn't send paste") raise except KeyError: raise PasteError("Invalid paste option: %s" % (result["error"]))
def __new__(cls, paste, private=True, lang="plain", key="public", desc="", expire=0, views=0, encrypted=False): if not paste: raise PasteError("No paste provided") if expire and views: # API incorrectly returns success so we raise error locally raise PasteError("Options 'expire' and 'views' are mutually exclusive") request = urlrequest( "https://paste.ee/api", data=urlencode( { 'paste': paste, 'private': bool(private), 'language': lang, 'key': key, 'description': desc, 'expire': expire, 'views': views, 'encrypted': bool(encrypted), 'format': "json" } ).encode("utf-8") ) try: result = json.loads(urlopen(request).read().decode("utf-8")) return result["paste"] except urlerror.HTTPError: print("Couldn't send paste") raise except KeyError: raise PasteError("Invalid paste option: %s" % (result["error"]))
def get_travel_times(origin=[0, 0], destinations=[0, 0], travel_method='DRIVING', config_path='/home/'): #read in api_key config = configparser.ConfigParser() config.read('{}googleMapsApiKey.ini'.format(config_path)) key = config['api']['api_number'] #set up query strings - origin origin_str = ','.join(map(str, origin)) #set up destination string destination_str = '' for element in destinations: destination_str = '{0}|{1}'.format(destination_str, ','.join(map(str, element))) destination_str = destination_str.strip('|') prefix = 'https://maps.googleapis.com/maps/api/distancematrix/json?mode=' url = urlparse( '{0}{1}&units=imperial&avoid=tolls|ferries&origins={2}&destinations={3}&key={4}' .format(prefix, travel_method, origin_str, destination_str, key)) full_url = url.scheme + '://' + url.netloc + url.path + '?' + url.query req = urlrequest(full_url) opener = build_opener() f = opener.open(req) d = json.load(f) return d
def download(self): head = self._build_headers() status_code = -1 body = '' try: req = urlopen(urlrequest(self._url, headers=head), timeout=self._timeout) status_code = req.code body = req.read() except urlerror.HTTPError as E: code = E.getcode() if code == 404: raise HTTPFileNotFoundError("File not found") elif code == 304: raise HTTPStale(str(self._last_modified)) else: raise HTTPError( "Server connection failed: {0.reason} ({1})".format( E, code)) except timeout: raise HTTPTimeoutError("Server took too long to respond") lm = req.headers.get("last-modified") self._last_modified = lm return body
def communicate_put(path, command, filename=None): args = dict() command = command.strip('/ ') if filename: filename = filename.strip('/ ') args['session_id'] = TESTD_SESSION if filename is None: filename = os.path.basename(path) with open(path, 'rb') as f: req = urlrequest(url='http://'+TESTD_HOST+':'+str(TESTD_PORT)+'/'+command+'/'+filename+'?'+urlencode(args), method='PUT', data=f.read()) urlopen(req)
def geocode_home(url=''): #get geocode of address req = urlrequest(url) opener = build_opener() f = opener.open(req) d = json.load(f) geocode = [ d['results'][0]['geometry']['location']['lat'], d['results'][0]['geometry']['location']['lng'] ] return geocode
def communicate_put(path, command, filename=None): args = dict() command = command.strip('/ ') if filename: filename = filename.strip('/ ') args['session_id'] = TESTD_SESSION if filename is None: filename = os.path.basename(path) with open(path, 'rb') as f: req = urlrequest(url='http://' + TESTD_HOST + ':' + str(TESTD_PORT) + '/' + command + '/' + filename + '?' + urlencode(args), method='PUT', data=f.read()) urlopen(req)
def download(self): head = self._build_headers() status_code = -1 body = '' try: req = urlopen(urlrequest(self._url, headers = head), timeout = self._timeout) status_code = req.code body = req.read() except urlerror.HTTPError as E: code = E.getcode() if code == 404: raise HTTPFileNotFoundError("File not found") elif code == 304: raise HTTPStale(str(self._last_modified)) else: raise HTTPError("Server connection failed: {0.reason} ({1})".format(E, code)) except timeout: raise HTTPTimeoutError("Server took too long to respond") lm = req.headers.get("last-modified") self._last_modified = lm return body
def esiRequest(route, arguments={}, auth=None): # Look in the database if we have a cached result for this call cachedCall = EsiCall.objects.filter(route=route, arguments=json.dumps(arguments)).order_by('cachedUntil').last() # Compare cache time and return result if cachedCall and (not cachedCall.cachedUntil or cachedCall.cachedUntil > timezone.now()): try: log("Cache Return for ", route) return json.loads(cachedCall.cachedResult) except Exception as e: log(cachedCall.cachedResult, "HELP", e) return None elif not cachedCall: print("Route :"+route) cachedCall = EsiCall(route=route, arguments=json.dumps(arguments)) # Prepare auth headers if needed headers = {} if auth: # if access token is expired, use refresh token if auth.tokenExpiry < timezone.now(): auth = refreshAccessToken(auth) log("Using refresh token", ) authorization = "Bearer "+auth.accessToken headers['Authorization'] = authorization # Acess ESI if not arguments: request = urlrequest(ESI_BASE + route, headers=headers) else: headers['content-type'] = 'application/json' data = json.dumps(arguments).encode('utf-8') request = urlrequest(ESI_BASE + route, data=data, headers=headers) log(request.data) log("ESI request to "+ request.get_method()+" "+route) try: response = urlopen(request) result = response.read().decode("utf-8") cachedCall.cachedResult = result result = json.loads(result) cachedUntil = response.info().get('expires') if cachedUntil: cachedCall.cachedUntil = datetime.strptime(cachedUntil, '%a, %d %b %Y %H:%M:%S GMT').replace(tzinfo=pytz.UTC) cachedCall.save() return result except urllib.error.HTTPError as e: r = e.read() log("ESI Exception:", r) return []