Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/chrisvander/coronavirus-modeling. Final Project for Graph Mining, utilizing Python 3 and NetworkX
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128import jsonimport requestsfrom urllib.parse import urlencodeimport hashlibimport os.pathimport pickleimport codecsfrom tqdm import tqdmimport zipfileimport shutil
headers = { "Content-Type": "application/json"}
def init_gis(): if os.path.exists('./data/ncgis/nc_gaston_parcels_pt.shp'): return dirpath = './data/ncgis' if os.path.exists(dirpath) and os.path.isdir(dirpath): shutil.rmtree(dirpath) download_gis() def download_gis(): print("Downloading NCGIS data") chunk_size = 512 url = "https://dit-cgia-gis-data.s3.amazonaws.com/NCOM-data/parcels/gaston_parcels.zip" save_path = "./data/ncgis.zip" r = requests.get(url, stream=True) if not os.path.exists(os.path.dirname(save_path)): try: os.makedirs(os.path.dirname(save_path)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise file_size = int(requests.head(url).headers["Content-Length"]) with open(save_path, 'wb') as fd: for chunk in tqdm(r.iter_content(chunk_size=chunk_size), total=int(file_size/chunk_size)): fd.write(chunk) print('Unzipping...') with zipfile.ZipFile('./data/ncgis.zip', 'r') as zip_ref: zip_ref.extractall('./data/ncgis')
def init_nhts(): if os.path.exists('./data/nhts/trippub.csv'): return dirpath = './data/nhts/' if os.path.exists(dirpath) and os.path.isdir(dirpath): shutil.rmtree(dirpath) download_nhts()
def download_nhts(): print("Downloading CSV files from NHTS...") chunk_size = 512 url = "https://nhts.ornl.gov/assets/2016/download/Csv.zip" save_path = './data/nhts.zip' r = requests.get(url, stream=True) if not os.path.exists(os.path.dirname(save_path)): try: os.makedirs(os.path.dirname(save_path)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise file_size = int(requests.head(url).headers["Content-Length"]) with open(save_path, 'wb') as fd: for chunk in tqdm(r.iter_content(chunk_size=chunk_size), total=int(file_size/chunk_size)): fd.write(chunk) print('Unzipping...') with zipfile.ZipFile('./data/nhts.zip', 'r') as zip_ref: zip_ref.extractall('./data/nhts')
def get_file(f_id, folder=None): md5 = hashlib.md5(f_id.encode('utf-8')).hexdigest() filename='./cache/' + md5 if folder: filename='./cache/' + folder + '/' + md5 if os.path.isfile(filename): with open(filename, 'rb') as f: return pickle.load(f) else: return None
def write_file(f_id, raw, folder=None): md5 = hashlib.md5(f_id.encode('utf-8')).hexdigest() filename='./cache/' + md5 if folder: filename='./cache/' + folder + '/' + md5 if not os.path.exists(os.path.dirname(filename)): try: os.makedirs(os.path.dirname(filename)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise with open(filename, 'wb') as f: pickle.dump(raw, f, protocol=pickle.HIGHEST_PROTOCOL)
def cache(f_id, funct, folder=None): res = get_file(f_id, folder=folder) if res is None: print("> Caching: " + f_id) raw = funct() write_file(f_id, raw, folder=folder) print("\t> Done") return raw return res
def parse_query(query): """ Convert all lists to strings within a given query dict. """ ret = {} for k, v in query.items(): if isinstance(v, list): ret[k] = ",".join(v) else: ret[k] = v return ret
def fetch_data(url, query=None): print("Fetching " + url) res = requests.get(url, params=query, headers=headers) if res.status_code == 200: return res.content.decode("utf-8") else: return None
def get_json(url, query=None): """ Perform a GET request and return the resulting JSON payload as an object. """ return json.loads(cache(url, lambda: fetch_data(url, query), folder='web_requests'))