def getzip(code, noerror=False): try: code = str(int(code.strip()[:5])) while len(code) < 5: # preceding 0's code = '0' + code except: if noerror: return from cantools.util import error error("invalid zip code: %s" % (code, )) zipcode = ZipCode.query().filter(ZipCode.code == code).get() if not zipcode: if config.map: # old configuration style city, state, county = fetch(config.map.zipdomain, path="/%s" % (code, ), asjson=True) else: # use mkpi-style ctmap.zip.domain city, state, county = fetch("%s/geo?action=zip&code=%s" % (config.ctmap.zip.domain, code), ctjson=True) zipcode = ZipCode(code=code, city=city, state=state, county=county) zipcode.put() if config.ctmap.zip.latlng and not zipcode.latitude: zipcode.latlng() return zipcode
def response(): log("cronswarm (up)", important=True) down = [] for addr in config.ctswarm.monitor.split("|"): proto = "http" host = addr port = 80 if "://" in addr: proto, host = addr.split("://") if ":" in host: host, port = host.split(":") elif proto == "https": port = 443 try: fetch(host, port=port, timeout=int(config.ctswarm.montime), protocol=proto, fakeua=True) except: log("SERVER DOWN: %s" % (addr, )) down.append(addr) if down: dz = "\r\n".join(down) for contact in config.admin.contacts: mail.send_mail(to=contact, subject="Server(s) Unreachable", body=TMP % (dz, )) log("cronswarm (up) complete")
def _fetch(self, api, *args): # api is latlng/zip path = self.apis[api]["path"].format( *[urllib.quote(str(a).replace(" ", "+")) for a in args]) host = self.apis[api]["host"] user = self.apis[api]["user"] prop = self.apis[api]["property"] keys = config.geo.user[user] onum = num = self.apis[api]["index"] sig = self.apis[api]["sig"] kwargs = {"asjson": True} result = [] while True: fullpath = path if keys[num]: fullpath += "&%s=%s" % (sig, keys[num]) if user == "google": kwargs["protocol"] = "https" raw = fetch(host, fullpath, **kwargs) result = raw.get(prop, []) if len(result): break log("0-length %s result (got: '%s') - changing user" % (api, raw), important=True) num = (num + 1) % len(keys) if num == onum: log("exhausted key list -- returning [] :'(") break self.apis[api]["index"] = num return result
def _fetch(self, api, *args): # api is latlng/zip path = self.apis[api]["path"].format(*[urllib.quote(str(a).replace(" ", "+")) for a in args]) host = self.apis[api]["host"] user = self.apis[api]["user"] prop = self.apis[api]["property"] keys = config.geo.user[user] onum = num = self.apis[api]["index"] sig = self.apis[api]["sig"] kwargs = { "asjson": True } result = [] while True: fullpath = path if keys[num]: fullpath += "&%s=%s"%(sig, keys[num]) if user == "google": kwargs["protocol"] = "https" raw = fetch(host, fullpath, **kwargs) result = raw.get(prop, []) if len(result): break log("0-length %s result (got: '%s') - changing user"%(api, raw), important=True) num = (num + 1) % len(keys) if num == onum: log("exhausted key list -- returning [] :'(") break self.apis[api]["index"] = num return result
def acquire(self, url, path): fname = url.split("/").pop() fpath = os.path.join(path, fname) if os.path.exists(fpath): return read(fpath) log("acquiring: %s"%(url,)) data = fetch(url) write(data, fpath) return data
def refresh(self, cbatch): from .util import getContribution freshies = fetch("api.github.com", "/repos/%s/%s/contributors" % (self.owner, self.repo), asjson=True, protocol="https") pcount = 0 ccount = 0 for item in freshies: log("checking for: %s" % (item["login"], ), 1) contrib = getContribution(self, item["login"]) if contrib: pcount += 1 ccount += contrib.refresh(item["contributions"], cbatch) return "%s/%s: %s contributors, %s contributions" % ( self.owner, self.repo, pcount, ccount)
def getzip(code): if len(code) < 5: from cantools.util import error error("invalid zip code: %s"%(code,)) try: code = str(int(code.strip()[:5])) while len(code) < 5: # preceding 0's code = '0'+code except: from cantools.util import error error("invalid zip code: %s"%(code,)) zipcode = ZipCode.query().filter(ZipCode.code == code).get() if not zipcode: from cantools.web import fetch city, state, county = fetch(ZIPDOMAIN, path="/%s"%(code,), asjson=True) zipcode = ZipCode(code=code, city=city, state=state, county=county) zipcode.put() return zipcode
def getzip(code): if len(code) < 5: from cantools.net import fail fail("invalid zip code") try: code = str(int(code.strip()[:5])) while len(code) < 5: # preceding 0's code = '0' + code except: from cantools.net import fail fail("invalid zip code") zipcode = ZipCode.query().filter(ZipCode.code == code).get() if not zipcode: from cantools.web import fetch from cantools import config city, state, county = fetch(config.zipdomain, path="/geo?action=zip&code=%s" % (code, ), ctjson=True) zipcode = ZipCode(code=code, city=city, state=state, county=county) zipcode.put() return zipcode