def update_database(): """Updates the location database with least possible online queries.""" connection = sqlite.create_connection() with connection: init_database(connection) servers_ = servers.get_servers() printer = Printer() progress_bar = printer.incremental_bar("Updating location database", max=len(servers_)) for server in servers_: longitude = str(server["location"]["long"]) latitude = str(server["location"]["lat"]) connection = sqlite.create_connection() with connection: if sqlite.location_exists(connection, latitude, longitude): progress_bar.next() continue else: location_d = query_location(latitude, longitude) display_name = location_d["display_name"] city_keys = ["city", "town", "village", "residential", "state"] for key in city_keys: try: city = location_d["address"][key] break except KeyError: continue else: city = "Unknown" country = server["country"] country_code = server["flag"].lower() location = ( latitude, longitude, display_name, city, country, country_code, None, # map ) connection = sqlite.create_connection() with connection: sqlite.create_location(connection, location) # if not printer.quiet: progress_bar.next() progress_bar.finish()
def unzip(): """Unzip the configuration files""" printer = Printer() zip_dir = resources.get_zip_dir(create=True) with printer.Do("Deleting old configuration files"): for ovpn_dir in ("ovpn_udp", "ovpn_tcp"): remove_dir = "{}/{}".format(zip_dir, ovpn_dir) if os.path.exists(remove_dir): rmtree(remove_dir, ignore_errors=True) for key, archive in __ARCHIVES.items(): zip_file = resources.get_zip_file(archive) with ZipFile(zip_file, "r") as zip_stream: name_list = zip_stream.namelist() with printer.incremental_bar( "Unzipping {} configurations".format(key), max=len(name_list) ) as incremental_bar: for file_name in name_list: zip_stream.extract(file_name, zip_dir) incremental_bar.next()