-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscrape_sd_zipcode_daily_summary.py
99 lines (73 loc) · 2.86 KB
/
scrape_sd_zipcode_daily_summary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-
'''
Code downloads pdf files from SD County website and converts information to
json output
Website: https://www.sandiegocounty.gov/content/dam/sdc/hhsa/programs/phs/Epidemiology/COVID-19%20Summary%20of%20Cases%20by%20Zip%20Code.pdf
'''
import requests
from datetime import date, timedelta, datetime
import tabula
import pandas as pd
import re
#For testing
import os
#Download PDF locally
def download_pdf(filename,URL):
myfile = requests.get(URL)
open(filename,'wb').write(myfile.content)
#Read pdf file and
def convert_pdf(filename, download_date):
columns = ['zipcode', 'confirmed_cases']
#Loading data from pdf
pdf_list = tabula.read_pdf(filename, pages = "all", multiple_tables = True)[0]
#Splitting data into proper format
pdf_df = pd.DataFrame(pdf_list)
pdf_df1 = pd.DataFrame(pdf_df.iloc[:,0:2])
pdf_df1.columns = columns
pdf_df2 = pd.DataFrame(pdf_df.iloc[:,2:4])
pdf_df2.columns = columns
pdf_df = pd.concat([pdf_df1,pdf_df2])
pdf_df.reset_index(drop=True, inplace=True)
#Determine date through time. This is the date that the sum goes until found
#in the PDF
text = pdf_df.loc[0,"zipcode"]
match = re.search(r'\d{1}/\d{1}/\d{4}', text)
if match == None:
match = re.search(r'\d{1}/\d{2}/\d{4}', text)
elif match == None:
match = re.search(r'\d{2}/\d{2}/\d{4}', text)
date_through = datetime.strptime(match.group(), "%m/%d/%Y")
pdf_df.drop(0,axis=0,inplace=True)
#Add updated and date through columns
pdf_df.insert(0,'updated', date.today())
pdf_df.insert(1,'date through', date_through)
#Find names Zip Code and remove
zipcode_index = pdf_df[pdf_df["zipcode"] == "Zip Code"].index
for zi in zipcode_index:
pdf_df.drop(zi, axis=0, inplace=True)
#Find total and append to bottom of dataframe
total_line = pdf_df[pdf_df["zipcode"] == "TOTAL"]
pdf_df.drop(total_line.index,inplace=True)
pdf_df = pdf_df.append(total_line)
#drop nan values
pdf_df.dropna(inplace=True)
return pdf_df
if __name__=="__main__":
yesterdate = str(date.today() - timedelta(days=1))
file = "sd_daily_update_zipcode_" + yesterdate + ".pdf"
filepath = "sd_daily_zipcode_pdfs/"
filename = filepath + file
URL = "https://www.sandiegocounty.gov/content/dam/sdc/hhsa/programs/" +\
"phs/Epidemiology/COVID-19%20Summary%20of%20Cases%20by%20Zip%20Code.pdf"
#Downloading and converting data
download_pdf(filename,URL)
df = convert_pdf(filename, yesterdate)
#Writing data
csv_file = 'sd_daily_zipcode_summary.csv'
csv_mode = 'a'
csv_header = False
if(not os.path.exists(csv_file)):
csv_mode = 'w'
csv_header = True
df.to_csv(csv_file,mode=csv_mode,header=csv_header,\
index=False)