This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
db.py
138 lines (110 loc) · 4.93 KB
/
db.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import csv
import peewee
from peewee import DatabaseProxy, Model, SqliteDatabase
from util import get_data, get_place_data, get_overview_data
from openpyxl import Workbook
database_proxy = DatabaseProxy()
class Base(Model):
class Meta:
database = database_proxy
legacy_table_names = False
class Overview(Base):
id = peewee.AutoField()
date = peewee.DateField(null=False, index=True)
total = peewee.IntegerField(null=False)
confirmed = peewee.IntegerField(null=False)
asymptomatic = peewee.IntegerField(null=False)
asymptomatic_to_confirmed = peewee.IntegerField(null=False)
deaths = peewee.IntegerField(null=False)
class DisctrictOverview(Base):
id = peewee.AutoField()
date = peewee.DateField(null=False, index=True)
district_name = peewee.CharField(null=False, index=True)
total = peewee.IntegerField(null=False)
confirmed = peewee.IntegerField(null=False)
asymptomatic = peewee.IntegerField(null=False)
class Place(Base):
id = peewee.AutoField()
date = peewee.DateField(null=False, index=True)
district_name = peewee.CharField(null=False, index=True)
place_name = peewee.CharField(null=False, index=True)
lng = peewee.DoubleField(null=False)
lat = peewee.DoubleField(null=False)
def write_csv(filename: str, rows: list, recreate=True):
if recreate and os.path.exists(filename):
os.remove(filename)
with open(filename, 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row in rows:
spamwriter.writerow(row)
def write_excel(filename: str, sheets: list, recreate=True):
if recreate and os.path.exists(filename):
os.remove(filename)
wb = Workbook()
for sheet in sheets:
ws = wb.create_sheet(sheet['name'])
for row in sheet['rows']:
ws.append(row)
wb.save(filename)
def create_db_files():
db_location = "./data/db/covid19-shanghai-lockdown.db"
if os.path.exists(db_location):
os.remove(db_location)
database = SqliteDatabase(db_location, pragmas={
'journal_mode': 'wal',
'cache_size': -1 * 8000,
'foreign_keys': 1,
'ignore_check_constraints': 0,
'synchronous': 0})
database_proxy.initialize(database)
database.create_tables(
[Overview, DisctrictOverview, Place], safe=True)
# Overview.truncate_table()
# DisctrictOverview.truncate_table()
# Place.truncate_table()
sheets = []
overview_data = get_overview_data()
overview_rows = [['Date', 'Total', 'Confirmed', 'Deaths',
'Asymptomatic', 'Asymptomatic to Confirmed']]
for date, item in overview_data.items():
Overview.create(date=date, total=item['total'], confirmed=item['confirmed'], deaths=item['deaths'],
asymptomatic=item['asymptomatic'], asymptomatic_to_confirmed=item['asymptomatic_to_confirmed'])
overview_rows.append([date, item['total'], item['confirmed'], item['deaths'],
item['asymptomatic'], item['asymptomatic_to_confirmed']])
sheets.append({'name': 'Overview', 'rows': overview_rows})
write_csv("./data/db/overview.csv", overview_rows)
data = get_data()
district_overview_rows = [
['Date', 'District', 'Total', 'Confirmed', 'Asymptomatic']]
for item in data:
date = item['date']
districts = item['districts']
for district in districts:
DisctrictOverview.create(date=date, district_name=district['district_name'],
total=district['total'], confirmed=district['confirmed'],
asymptomatic=district['asymptomatic'])
district_overview_rows.append([date, district['district_name'],
district['total'], district['confirmed'],
district['asymptomatic']])
sheets.append({'name': 'District Overview',
'rows': district_overview_rows})
write_csv("./data/db/district_overview.csv", district_overview_rows)
places = get_place_data()
place_rows = [["Date", "District", "Place", "Lng", "Lat"]]
for place in places:
Place.create(date=place['date'], district_name=place['district_name'],
place_name=place['place_name'],
lng=place['lnglat'][0], lat=place['lnglat'][1])
place_rows.append([place['date'], place['district_name'], place['place_name'],
place['lnglat'][0], place['lnglat'][1]])
sheets.append({'name': 'Place', 'rows': place_rows})
write_csv("./data/db/place.csv", place_rows)
write_excel("./data/db/covid19-shanghai-lockdown.xlsx", sheets)
if __name__ == "__main__":
# If you want to get the latest db files,
# uncomment the underline and run this file
#
# create_db_files()
pass