def GeoCode(address): from urllib.request import urlopen as UrlOpen from urllib.parse import quote as Quote # Encode query string into URL url = 'http://maps.googleapis.com/maps/api/geocode/json?address={}&sensor=false'.format(Quote(address)) # Call API and extract JSON PrintNow('Calling Google Maps API for `{:s}` ... '.format(address), end = '') json = UrlOpen(url).read() json = JSONLoad(json.decode('utf-8')) # Extract longitude and latitude if json.get('status') == 'ZERO_RESULTS': latitude, longitude = None, None PrintNow('it was not found') else: latitude, longitude = (value for key, value in sorted(json.get('results')[0].get('geometry').get('location').items())) PrintNow('it is located at {:f}/{:f}'.format(latitude, longitude)) return Address(address, latitude, longitude)
def reporttohub(self, requestdata): if requestdata == "": webrequest = GenerateWebRequest(self.urlendpoint) else: webrequest = GenerateWebRequest(self.urlendpoint, data=requestdata.encode( 'ascii', 'ignore')) tries = 0 outcome = "" while tries < self.maximumtrieslimit: try: outcome = GetWebPage(webrequest) tries = 99999 except WebError as errorobject: tries = tries + 1 print("Error accessing Hub: ", errorobject.reason) if tries == 99999: outcome = outcome.read() outcome = outcome.decode('utf-8', 'ignore') else: print("Gave up accessing Hub") return outcome
def retrievewebpages(self, specifiedyear, currentdateobject): tries = 0 print("Downloading astro data for:", specifiedyear) while tries < self.webcalltries: try: webresponse = {} for datamode in ("Day", "Nau", "Civ", "Ast"): webresponse[datamode] = "" url = self.buildurl(specifiedyear, datamode) #print(url) webrequest = GenerateWebRequest(url) if self.connectionmode == True: rawwebresponse = GetWebPage(webrequest, context=self.securitycontext).read(20000) webresponse[datamode] = rawwebresponse.decode("utf-8") else: webresponse[datamode] = ScraperFunction.generatedummydata(datamode) tries = 99999 except WebError as errorobject: tries = tries + 1 print("Error accessing website: ", errorobject.reason) if tries == 99999: #print("Access to website data Succeeded") for datamode in ("Day", "Nau", "Civ", "Ast"): self.lastresult[datamode] = webresponse[datamode].split("\n") self.lastsuccessfulwebcall = DateTime.createfromobject(currentdateobject) self.lastsuccessfulyear = specifiedyear else: print("Access to website data Failed")
def performwebcall(self, webaddress, datadictionary): tries = 0 #Logging.printrawline("Triggering Deluge-Monitor at " + datetimestamp + "...") while tries < self.webcalltries: try: if datadictionary is None: rawwebresponse = GetWebPage( webaddress, context=self.securitycontext).read(10000) else: unencodedpostdata = MakeJson(datadictionary) postdata = unencodedpostdata.encode("ascii") rawwebresponse = GetWebPage(webaddress, context=self.securitycontext, data=postdata).read(10000) webresponse = rawwebresponse.decode("utf-8") tries = 99999 self.latestresult = webresponse self.latestdatetime = DateTime.getnow() except WebError as errorobject: tries = tries + 1 #Logging.printrawline(" - Error Triggering Deluge-Monitor: " + errorobject.reason) if tries != 99999: currentdatetime = DateTime.getnow() Logging.printrawline(" - Gave up Triggering Deluge-Manager at " + currentdatetime.getiso())