def get_data(archive_date, year, output_dir): """ Download USAspending .csv files for specified assistance types (e.g., grants, loans, insurance, direct payments, and/or other), extract, and save to disk """ usaspending_base_url = "http://download.usaspending.gov" usaspending_url = "{}/data_archives/{}/csv/".format(usaspending_base_url, archive_date[0:6]) for name in ASSISTANCE_LIST: print("Downloading {}".format(name)) filename = "{}_All_{}_Full_{}.csv".format(year, name, archive_date) filepath = os.path.join(output_dir, filename) zipfilename = "{}.zip".format(filename) zipfilepath = os.path.join(output_dir, zipfilename) # if USASpending .zip archive file not already downloaded, do that try: with open(zipfilepath) as f: pass except: zipfileurl = urllib.parse.urljoin(usaspending_url, zipfilename) r = download.download_file(zipfileurl, zipfilepath) if not r[0]: print("Unable to download {} (status = {})".format(zipfileurl, r[1])) continue # if USASpending file not already unzipped, do that try: with open(filepath) as f: f.close() except: download.unzip_file(zipfilepath, output_dir)
def get_data(archive_date, year, output_dir): ''' Download USAspending .csv contract archive file, extract, and save to disk ''' print ('Downloading Contracts') usaspending_base_url = 'http://download.usaspending.gov' usaspending_url = '{}/data_archives/{}/csv/'.format( usaspending_base_url, archive_date[0:6]) filename = '{}_All_Contracts_Full_{}.csv'.format( year, archive_date) filepath = os.path.join(output_dir, filename) zipfilename = '{}.zip'.format(filename) zipfilepath = os.path.join(output_dir, zipfilename) #if USASpending .zip archive file not already downloaded, do that try: with open(zipfilepath) as f: pass except: zipfileurl = urllib.parse.urljoin(usaspending_url, zipfilename) r = download.download_file(zipfileurl, zipfilepath) if not r[0]: print ('Unable to download {} (status = {})'.format( zipfileurl, r[1])) return #if USASpending file not already unzipped, do that try: with open(filepath) as f: f.close() except: download.unzip_file(zipfilepath, output_dir)