Skip to content

Commit

Permalink
Refactored fromJSONBackup to handle gzip backup files
Browse files Browse the repository at this point in the history
  • Loading branch information
tholzheim committed Jul 5, 2021
1 parent d5efcb4 commit 61a7d2c
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions geograpy/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import gzip
import shutil
import json
from pathlib import Path
from sklearn.neighbors import BallTree
from geograpy.wikidata import Wikidata
from lodstorage.sql import SQLDB
Expand Down Expand Up @@ -112,6 +113,45 @@ def getURLContent(url:str):
content = urlResponse.read().decode()
return content

@staticmethod
def getFileContent(path:str):
with open(path, "r") as file:
content=file.read()
return content

@staticmethod
def getBackupDirectory():
home = str(Path.home())
path = f"{home}/.geograpy3"
return path

@staticmethod
def downloadBackupFile(url:str, fileName:str):
'''
Downloads from the given url the zip-file and extracts the file corresponding to the given fileName.
Args:
url: url linking to a downloadable gzip file
fileName: Name of the file that should be extracted from gzip file
Returns:
Name of the extracted file with path to the backup directory
'''
backupDirectory=LocationList.getBackupDirectory()
if not os.path.isdir(backupDirectory):
os.makedirs(backupDirectory)
extractTo= f"{backupDirectory}/{fileName}"
zipped = f"{extractTo}.gz"
print(f"Downloading {zipped} from {url} ... this might take a few seconds")
urllib.request.urlretrieve(url, zipped)
print(f"unzipping {extractTo} from {zipped}")
with gzip.open(zipped, 'rb') as gzipped:
with open(extractTo, 'wb') as unzipped:
shutil.copyfileobj(gzipped, unzipped)
if not os.path.isfile(extractTo):
raise (f"could extract {fileName} from {zipped}")
return extractTo


class CountryList(LocationList):
'''
Expand Down Expand Up @@ -190,8 +230,10 @@ def fromJSONBackup(cls):
CountryList based on the json backup
'''
countryList = CountryList()
url="https://raw.githubusercontent.com/wiki/somnathrakshit/geograpy3/data/countries_geograpy3.json"
jsonStr = LocationList.getURLContent(url)
fileName="countries_geograpy3.json"
url="https://raw.githubusercontent.com/wiki/somnathrakshit/geograpy3/data/countries_geograpy3.json.gz"
backupFile=LocationList.downloadBackupFile(url, fileName)
jsonStr = LocationList.getFileContent(backupFile)
countryList.restoreFromJsonStr(jsonStr)
return countryList

Expand Down Expand Up @@ -286,8 +328,10 @@ def fromJSONBackup(cls):
RegionList based on the json backup
'''
regionList = RegionList()
url = "https://raw.githubusercontent.com/wiki/somnathrakshit/geograpy3/data/regions_geograpy3.json"
jsonStr=LocationList.getURLContent(url)
fileName="regions_geograpy3.json"
url = "https://raw.githubusercontent.com/wiki/somnathrakshit/geograpy3/data/regions_geograpy3.json.gz"
backupFile = LocationList.downloadBackupFile(url, fileName)
jsonStr = LocationList.getFileContent(backupFile)
regionList.restoreFromJsonStr(jsonStr)
return regionList

Expand Down Expand Up @@ -399,8 +443,10 @@ def fromJSONBackup(cls, jsonStr:str=None):
'''
cityList = CityList()
if jsonStr is None:
url = "https://raw.githubusercontent.com/wiki/somnathrakshit/geograpy3/data/cities_geograpy3.json"
jsonStr = LocationList.getURLContent(url)
fileName="cities_geograpy3.json"
url = "https://raw.githubusercontent.com/wiki/somnathrakshit/geograpy3/data/cities_geograpy3.json.gz"
backupFile = LocationList.downloadBackupFile(url, fileName)
jsonStr = LocationList.getFileContent(backupFile)
cityList.restoreFromJsonStr(jsonStr)
return cityList

Expand Down

0 comments on commit 61a7d2c

Please sign in to comment.