class Helpers(): def __init__(self): self.conn = SQLiteHelper('mirrors.db') self.log = logging.getLogger(__name__) if not self.conn.table_exists('metadata'): self.conn.execute(''' CREATE TABLE metadata ( IP TEXT, Location TEXT, Time TIMESTAMP ) ''') def get_location(self): row = self.conn.execute("SELECT IP, Location, Time FROM metadata").fetchone() if row is not None and len(row): self.log.info("Loaded our IP from the database: {0}, {1}", row['IP'], row['Location']) return (row['IP'], row['Location']) else: ip = requests.get("http://checkip.amazonaws.com").text.strip() self.log.info("Amazon reports our IP as {0}", ip) location = geolite2.reader().get(ip) iso = location['registered_country']['iso_code'] self.conn.execute("INSERT INTO metadata (IP, Location, Time) VALUES (?,?,?)", [ip, iso, time.time()]) self.conn.commit() return ip, iso def get_mirror_location(self, ip): location = geolite2.reader().get(ip) return location def http_ping(self, url): start = time.clock() try: headers = {'User-Agent': 'Dodgy HTTP Ping Script ([email protected])'} requests.head(url, timeout=0.75, headers=headers) finish = time.clock() except: finish = 999 return url, (finish - start) def average_http_ping(self, url): results = [self.http_ping(url)[1] * 3] return (url, sum(results, 0.0) / len(results)) def async_http_ping(self,urls): mylist = [] pool = TPE(4) for x in urls: mylist.append(pool.submit(self.average_http_ping,x)) return mylist
i += 1 database = SQLiteHelper("packagecontents.db") distros = """ CREATE TABLE distros ( ID INTEGER PRIMARY KEY, Name TEXT ) """ if not database.table_exists("distros"): database.execute(distros) database.commit() database.execute("INSERT INTO distros (Name) VALUES ('Debian')") database.commit() package_contents = """ CREATE TABLE package_contents_temp ( DistroID INTEGER, File TEXT, Package TEXT, FOREIGN KEY(DistroID) REFERENCES distros(ID) ) """ if not database.table_exists("package_contents_temp"): database.execute(package_contents) database.commit()