This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataloader.py
576 lines (504 loc) · 25.4 KB
/
dataloader.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import datetime
import csv
import json
import glob
import os
import urllib.request
import jsonschema
import config
import schemas
import sys
import numpy as np
import collections
import shutil
import geopandas as gpd
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from collections import Counter
import japanize_matplotlib
import requests
from bs4 import BeautifulSoup
import re
import feedparser
import locale
import calendar
class CovidDataManager:
#日本標準時
JST = datetime.timezone(datetime.timedelta(hours=+9), "JST")
#設定ファイル
REMOTE_SOURCES = config.REMOTE_SOURCES #外部ファイルの参照設定
HEADER_TRANSLATIONS = config.HEADER_TRANSLATIONS #headerの変換一覧
INT_CAST_KEYS = config.INT_CAST_KEYS #intにキャストすべきkey
CODECS = config.CODECS #ファイルエンコーディングリスト
#バリデーション用のスキーマ定義
SCHEMAS = schemas.SCHEMAS
def __init__(self):
now = datetime.datetime.now(self.JST)
self.now_str = now.strftime("%Y/%m/%d %H:%M")
self.data = {
"last_update":self.now_str,
}
def fetch_datas(self):
for key in self.REMOTE_SOURCES:
#print(key)
datatype = self.REMOTE_SOURCES[key]['type']
dataurl = self.REMOTE_SOURCES[key]['url']
data = {}
if datatype == 'csv':
data = self.import_csv_from(dataurl)
else:
sys.exit("Unsupported file type")
self.data[key] = data
def import_csv_from(self, csvurl):
request_file = urllib.request.urlopen(csvurl)
if not request_file.getcode() == 200:
sys.exit("HTTP status: " + str(request_file.getcode()))
f = self.decode_csv(request_file.read())
#filename = os.path.splitext(os.path.basename(csvurl))[0]
datas = self.csvstr_to_dicts(f)
return {
'last_update': self.now_str,
'data': datas
}
#デコード出来るまでCODECS内全コーデックでトライする
def decode_csv(self, csv_data)->str:
print('csv decoding')
for codec in self.CODECS:
try:
csv_str = csv_data.decode(codec)
print('ok:' + codec)
return csv_str
except:
print('ng:' + codec)
continue
print('Appropriate codec is not found.')
#CSV文字列を[dict]型に変換
def csvstr_to_dicts(self, csvstr) -> list:
datas = []
rows = [row for row in csv.reader(csvstr.splitlines())]
header = rows[0]
header = self.translate_header(header)
maindatas = rows[1:]
print(len(header))
for d in maindatas:
#空行はスキップ
if self.is_empty(d):
continue
#print(d, len(d))
if d == maindatas[-1] and len(d) < 5:
continue
data = {}
for i in range(len(header)):
data[header[i]] = d[i]
if header[i] in self.INT_CAST_KEYS:
data[header[i]] = int(d[i])
datas.append(data)
return datas
#HEADER_TRANSLATIONSに基づきデータのヘッダ(key)を変換
def translate_header(self, header:list)->list:
for i in range(len(header)):
for key in self.HEADER_TRANSLATIONS:
if header[i] == key:
header[i] = self.HEADER_TRANSLATIONS[key]
return header
#生成されるjsonの正当性チェック
def validate(self):
for key in self.data:
jsonschema.validate(self.data[key], self.SCHEMAS[key])
def export_jsons(self, directory='origin_data/'):
for key in self.data:
print(key + '.json')
self.export_json_of(key, directory)
def export_json_of(self, key, directory='origin_data/'):
if not os.path.exists(directory):
os.makedirs(directory)
with open(directory + key + '.json', 'w', encoding='utf-8') as f:
json.dump(self.data[key], f, indent=4, ensure_ascii=False)
def is_empty(self, data):
if data == []: return True
elif data[0] == '': return True
return False
class GraphData:
def __init__(self):
self.outfile = [
"last_update.json",
"patients_cnt.json",
"patients.json",
"inspections.json",
"inspections_person.json",
"hospitalizations.json",
"querents.json",
"map_update.json",
"news.json"
]
#origin_file_list = glob.glob("./origin_data/*.json")
#print(origin_file_list)
def main(self):
self.generate_update()
self.generate_patients_cnt()
self.generate_patients()
self.generate_inspections_person()
self.generate_hospitalizations()
self.generate_querents()
self.generate_maps()
self.generate_news()
def generate_update(self, origin_directory='origin_data/', out_directory='data/'):
if not os.path.exists(out_directory):
os.makedirs(out_directory)
shutil.copyfile(origin_directory+self.outfile[0], out_directory+self.outfile[0])
def generate_patients_cnt(self, origin_directory='origin_data/', out_directory='data/'):
with open(origin_directory + "patients.json", encoding='utf-8') as f:
data = json.load(f)
with open("previous_data/patients_cnt.json", encoding='utf-8') as f:
prev_data = json.load(f)
prev_data["last_update"] = data["last_update"]
prev_data = self.add_patiennts_data(prev_data, data)
with open(out_directory+ self.outfile[1], 'w') as f:
json.dump(prev_data, f, ensure_ascii=False, indent=4, separators=(',', ': '))
def generate_patients(self, origin_directory='origin_data/', out_directory='data/'):
with open(origin_directory + "patients.json", encoding='utf-8') as f:
data = json.load(f)
#out = [{elem:dic[elem] for elem in dic if not (elem in ['都道府県名', '全国地方公共団体コード'])} for dic in data["data"]]
out = []
for dic in data["data"]:
dic["居住地"] = dic.pop("市区町村名")
dic["年代"] = dic.pop("患者_年代")
dic["性別"] = dic.pop("患者_性別")
#TODO: 例外処理ちゃんとかく
try:
dic["公表日"] = self.format_date(dic["公表日"]) + "T08:00:00.000Z"
except Exception as e:
print("公表日未検出")
dic["公表日"] = dic["公表日"]
pass
try:
dic["陽性確定日"] = self.format_date(dic["陽性確定日"]) + "T08:00:00.000Z"
except Exception as e:
print(str(e) + '陽性確定日未検出')
dic["陽性確定日"] = dic["陽性確定日"]
pass
del_list = ['都道府県名', '全国地方公共団体コード']
[dic.pop(d) for d in del_list]
out.append(dic)
data["data"] = out
with open(out_directory+ self.outfile[2], 'w') as f:
json.dump(data, f, ensure_ascii=False, indent=4, separators=(',', ': '))
def generate_inspections(self, origin_directory='origin_data/', out_directory='data/'):
with open(origin_directory + "inspections.json", encoding='utf-8') as f:
data = json.load(f)
with open("previous_data/inspections.json", encoding='utf-8') as f:
prev_data = json.load(f)
out = []
for dic in data["data"]:
dic["日付"] = dic.pop("実施年月日")
dic["日付"] = self.format_date(dic["日付"])
dic["日付"] += "T08:00:00.000Z"
dic["小計"] = dic.pop("検査実施_件数")
del_list = ['全国地方公共団体コード', '都道府県名', '市区町村名', '備考']
[dic.pop(d) for d in del_list]
out.append(dic)
prev_data["data"].extend(out)
# 昨日までのデータがない場合は暫定で最後のデータを入力
# 土日だけ抜けてるとめんどくさい...月曜に土日のデータもいれてほしい
prev_data = self.add_data(prev_data, data)
prev_data["last_update"] = data["last_update"]
with open(out_directory+ self.outfile[3], 'w') as f:
json.dump(prev_data, f, ensure_ascii=False, indent=4, separators=(',', ': '))
def generate_inspections_person(self, origin_directory='origin_data/', out_directory='data/'):
with open(origin_directory + "inspections_people.json", encoding='utf-8') as f:
data = json.load(f)
with open("previous_data/inspections_person.json", encoding='utf-8') as f:
prev_data = json.load(f)
out = []
for dic in data["data"]:
dic["日付"] = dic.pop("実施_年月日")
dic["日付"] = self.format_date(dic["日付"])
dic["日付"] += "T08:00:00.000Z"
dic["小計"] = dic.pop("検査実施_人数 ")
del_list = ['全国地方公共団体コード', '都道府県名 ', '市区町村名 ', '備考']
[dic.pop(d) for d in del_list]
out.append(dic)
prev_data["data"].extend(out)
# 昨日までのデータがない場合は暫定で最後のデータを入力
# 土日だけ抜けてるとめんどくさい...月曜に土日のデータもいれてほしい
prev_data = self.add_data(prev_data, data)
prev_data["last_update"] = data["last_update"]
with open(out_directory + self.outfile[4], 'w') as f:
json.dump(prev_data, f, ensure_ascii=False, indent=4, separators=(',', ': '))
def generate_hospitalizations(self, origin_directory='origin_data/', out_directory='data/'):
with open(origin_directory + "inspections_people.json", encoding='utf-8') as f:
data = json.load(f)
with open(origin_directory + "hospitalizations.json", encoding='utf-8') as f:
data2 = json.load(f)
with open("previous_data/hospitalizations.json", encoding='utf-8') as f:
prev_data = json.load(f)
prev_data["last_update"] = data2["last_update"]
prev_data["data"][0]["検査実施人数"] = data["data"][-1]["検査実施_人数 "]
prev_data["data"][0]["入院中"] = data2["data"][-1]["入院等"]
prev_data["data"][0]["退院"] = data2["data"][-1]["退院"]
prev_data["data"][0]["死亡"] = data2["data"][-1]["死亡"]
prev_data["data"][0]["陽性患者数"] = data2["data"][-1]["入院等"] + data2["data"][-1]["退院"] + data2["data"][-1]["死亡"]
with open(out_directory+ self.outfile[5], 'w') as f:
json.dump(prev_data, f, ensure_ascii=False, indent=4, separators=(',', ': '))
def generate_querents(self, origin_directory='origin_data/', out_directory='data/'):
with open(origin_directory + "querents.json", encoding='utf-8') as f:
data = json.load(f)
with open("previous_data/querents.json", encoding='utf-8') as f:
prev_data = json.load(f)
out = []
for dic in data["data"]:
dic["日付"] = dic.pop("受付_年月日")
dic["日付"] = self.format_date(dic["日付"])
dic["日付"] += "T08:00:00.000Z"
dic["小計"] = dic.pop("相談件数")
del_list = ['全国地方公共団体コード', ' 都道府県名', ' 市区町村名 ']
[dic.pop(d) for d in del_list]
out.append(dic)
prev_data["data"].extend(out)
# 昨日までのデータがない場合は暫定で最後のデータを入力
# 土日だけ抜けてるとめんどくさい...月曜に土日のデータもいれてほしい
prev_data = self.add_data(prev_data, data)
prev_data["last_update"] = data["last_update"]
with open(out_directory+ self.outfile[6], 'w') as f:
json.dump(prev_data, f, ensure_ascii=False, indent=4, separators=(',', ': '))
def generate_maps(self, origin_directory='origin_data/', out_directory='data/'):
with open(origin_directory + "patients.json", encoding='utf-8') as f:
data = json.load(f)
city_list = [
"下関市", "宇部市", "山口市", "萩市", "防府市", "下松市", "光市", "岩国市", "長門市", "柳井市",
"美祢市", "周南市", "山陽小野田市", "周防大島町", "和木町", "上関町", "田布施町", "平生町", "阿武町"
]
num_list = np.zeros(len(city_list), int).tolist()
city_dict = dict(zip(city_list, num_list)) # 各自治体の陽性患者人数のdictを作成
heat_colorlist = ["#DCF8DC", "#95EA95", "#2BD52B", "#1D8D1D", "#0E470E"]
for d in data["data"]:
if d["市区町村名"] in city_dict: city_dict[d["市区町村名"]] += 1
else: continue
color_dict = city_dict.copy()
for key in city_dict.keys():
if city_dict[key] == 0:
color_dict[key] = "white"
elif city_dict[key] <= 200:
color_dict[key] = "#DCF8DC"
elif city_dict[key] <= 400:
color_dict[key] = "#95EA95"
elif city_dict[key] <= 600:
color_dict[key] = "#2BD52B"
elif color_dict[key] <= 800:
color_dict[key] = "#1D8D1D"
else:
color_dict[key] = "#0E470E"
#color_num = (city_dict[key] - min(city_dict.values())) / (max(city_dict.values()) - min(city_dict.values()))
df = gpd.read_file('./N03-190101_35_GML/N03-19_35_190101.shp', encoding='SHIFT-JIS')
#df = gpd.read_file('./N03-190101_35_GML/N03-19_35_190101.geojson', encoding='SHIFT-JIS')
df = df[df["N03_004"].isin(city_list)]
base = df.plot(color="white", edgecolor="black")
# グラフの枠線を削除
base.axes.xaxis.set_visible(False)
base.axes.yaxis.set_visible(False)
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['left'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['bottom'].set_visible(False)
for key in color_dict.keys():
df[df["N03_004"] == key].plot(ax=base, color=color_dict[key], edgecolor="black") # , color=color_dict[key] , cmap='Greens'
long_lat = [
[130.98, 34.08], [131.25, 33.98], [131.48, 34.13], [131.41, 34.38], [131.56, 34.05], [131.88, 34.02],
[132.08, 34.20], [131.95, 33.98], [131.18, 34.34], [132.12, 33.98], [131.21, 34.18], [131.80, 34.16],
[131.17, 34.02], [132.25, 33.92], [132.21, 34.19], [132.04, 33.82], [132.03, 33.94], [132.08, 33.93], [131.56, 34.54]
]
city_text = [
[130.70, 33.70], [131.16, 33.66], [131.29, 33.82], [131.26, 34.72], [131.41, 33.66], [131.56, 33.82],
[131.64, 33.66], [132.08, 34.57], [130.82, 34.67], [132.32, 34.35], [131.09, 34.58], [131.82, 34.54],
[130.86, 33.82], [132.32, 34.19], [132.28, 34.53], [132.065, 33.62], [131.80, 33.62], [132.28, 33.62], [131.40, 34.78]
]
city_text2 = [
[0.00, -0.07], [0.00, -0.07],
[-0.01, -0.07], [0.01, -0.07],
[0.03, -0.07], [0.02, -0.07],
[-0.01, -0.07], [0.00, -0.07],
[0.03, -0.07], [0.04, -0.07],
[0.03, -0.07], [0.00, -0.07],
[0.10, -0.07], [0.10, -0.07],
[0.04, -0.07], [0.04, -0.07],
[0.06, -0.07], [0.04, -0.07],
[0.04, -0.07]
]
plt_line = [
[[long_lat[0][0]-x for x in np.arange(0, 0.24, 0.06)], [long_lat[0][1]-y for y in np.arange(0, 0.4, 0.1)]],
[[long_lat[1][0]]*4, [long_lat[1][1]-y for y in np.arange(0.0, 0.32, 0.08)]],
[[long_lat[2][0]-x for x in np.arange(0, 0.12, 0.03)], [long_lat[2][1]-y for y in np.arange(0.0, 0.32, 0.08)]],
[[long_lat[3][0]-x for x in np.arange(0, 0.12, 0.03)], [long_lat[3][1]+y for y in np.arange(0.0, 0.32, 0.08)]],
[[long_lat[4][0]-x for x in np.arange(0, 0.08, 0.02)], [long_lat[4][1]-y for y in np.arange(0.0, 0.40, 0.10)]],
[[long_lat[5][0]-x for x in np.arange(0, 0.28, 0.07)], [long_lat[5][1]-y for y in np.arange(0.0, 0.18, 0.045)]],
[[long_lat[6][0]+x for x in np.arange(0, 0.12, 0.03)], [long_lat[6][1]+y for y in np.arange(0.0, 0.36, 0.09)]],
[[long_lat[7][0]-x for x in np.arange(0, 0.32, 0.08)], [long_lat[7][1]-y for y in np.arange(0.0, 0.36, 0.09)]],
[[long_lat[8][0]-x for x in np.arange(0, 0.32, 0.08)], [long_lat[8][1]+y for y in np.arange(0.0, 0.32, 0.08)]],
[[long_lat[9][0]+x for x in np.arange(0, 0.32, 0.08)], [long_lat[9][1]+y for y in np.arange(0.0, 0.40, 0.10)]],
[[long_lat[10][0]-x for x in np.arange(0, 0.02, 0.005)], [long_lat[10][1]+y for y in np.arange(0.0, 0.40, 0.10)]],
[[long_lat[11][0]+x for x in np.arange(0, 0.14, 0.035)], [long_lat[11][1]+y for y in np.arange(0.0, 0.38, 0.095)]],
[[long_lat[12][0]-x for x in np.arange(0, 0.12, 0.03)], [long_lat[12][1]-y for y in np.arange(0.0, 0.16, 0.04)]],
[[long_lat[13][0]+x for x in np.arange(0, 0.28, 0.07)], [long_lat[13][1]+y for y in np.arange(0.0, 0.24, 0.06)]],
[[long_lat[14][0]+x for x in np.arange(0, 0.16, 0.04)], [long_lat[14][1]+y for y in np.arange(0.0, 0.34, 0.085)]],
[[long_lat[15][0]+x for x in np.arange(0, 0.14, 0.035)], [long_lat[15][1]-y for y in np.arange(0.0, 0.18, 0.045)]],
[[long_lat[16][0]-x for x in np.arange(0, 0.12, 0.03)], [long_lat[16][1]-y for y in np.arange(0.0, 0.32, 0.08)]],
[[long_lat[17][0]+x for x in np.arange(0, 0.36, 0.09)], [long_lat[17][1]-y for y in np.arange(0.0, 0.32, 0.08)]],
[[long_lat[18][0]-x for x in np.arange(0, 0.08, 0.02)], [long_lat[18][1]+y for y in np.arange(0.0, 0.18, 0.045)]],
]
for fig,pline,cname,cplace,cplace2 in zip(long_lat, plt_line, city_list, city_text, city_text2):
#plt.plot(fig[0], fig[1], marker='.', color="blue", markersize=6)
base.plot(pline[0], pline[1], color="black", linewidth = 0.8)
base.text(cplace[0], cplace[1], cname, size=10, color="black")
#base.text(cplace[0], cplace[1]-0.03, "ー"*len(cname), size=10, color="black")
base.text(cplace[0]+cplace2[0], cplace[1]+cplace2[1], str(city_dict[cname])+"人", size=11, color="black")
base.text(131.92, 35.30, "陽性患者数【人】", size=12, color="black")
base.add_patch(patches.Rectangle(xy=(131.80, 34.70), width=0.81, height=0.55, ec="black", fill=False))
for i,heat in enumerate(heat_colorlist):
base.add_patch(patches.Rectangle(xy=(131.83, 35.12-i*0.1), width=0.25, height=0.1, fc=heat, ec="black", fill=True))
if i == 4:
base.text(132.09, 35.05-i*0.1+0.1, "・・・"+str(200*i+1)+"以上")
else:
base.text(132.09, 35.05-i*0.1+0.1, "・・・"+str(200*i+1)+"-"+str(200*(i+1)))
plt.savefig(out_directory+"yamaguchi-map.png", bbox_inches='tight')
#plt.show()
with open(out_directory+ self.outfile[7], 'w') as f:
json.dump(data["last_update"], f, ensure_ascii=False, indent=4, separators=(',', ': '))
def generate_news(self, origin_directory='origin_data/', out_directory='data/'):
with open("previous_data/"+self.outfile[8], encoding='utf-8') as f:
prev_data = json.load(f)
newinfo_url = "https://www.pref.yamaguchi.lg.jp/press/rss.xml"
newinfo = self.new_info(newinfo_url)
for info in newinfo:
date, url, text = info
prev_data["newsItems"].append(
{
"date": date,
"url": url,
"text": text
}
)
with open(out_directory+ self.outfile[8], 'w') as f:
json.dump(prev_data, f, ensure_ascii=False, indent=4, separators=(',', ': '), sort_keys=True)
# TODO: バグの原因。年越しまでに要変更
def interpolate_year(self, datestr):
print(datestr)
datestr = re.sub(r"[年月]", "/", datestr)
datestr = re.sub(r"[日]", "", datestr)
fmtstr = re.split("\D", datestr) # 数字以外で区切る
if len(fmtstr) == 3:
return datestr
else:
return "2021/" + datestr
def format_date(self, date_str):
try:
#date_str = self.interpolate_year(date_str)
#print(datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=+9), "JST")).isoformat())
date_dt = datetime.datetime.strptime(date_str, "%Y/%m/%d")
return date_dt.strftime("%Y-%m-%d")
except Exception:
raise
def format_date2(self, date_str):
#print(datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=+9), "JST")).isoformat())
date_dt = datetime.datetime.strptime(str(datetime.date.today().year)+"年"+date_str, "%Y年%m月%d日")
return date_dt.strftime("%Y/%m/%d")
def format_date3(self, date_str):
#print(datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=+9), "JST")).isoformat())
#locale.setlocale(locale.LC_TIME, 'en_US.UTF-8') # 英語表記なのでロケールを変更
#date_dt = datetime.datetime.strptime(date_str, "%a, %d %b %Y %H:%M:%S %Z")
# Sat, 2 May 2020 10:00:00 JST
#date_str = re.findall(r'[0-9]{1,2} ', date_str)
date_strlist = date_str.split(" ")[1:4]
months = {
"Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04", "May": "05", "Jun": "06",
"Jul": "07", "Aug": "08", "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"
}
if len(date_strlist[0]) == 1:
date_day = "0" + date_strlist[0]
else:
date_day = date_strlist[0]
return date_strlist[2]+"/"+months[date_strlist[1]]+"/"+date_day
def add_patiennts_data(self, prev_data, data):
lastday = prev_data["data"][-1]["日付"][:10]
lastday = datetime.date(int(lastday[:4]), int(lastday[5:7]), int(lastday[8:10]))
today = datetime.date.today() # timezoneはどうなるのか調査が必要
period = today - lastday
daily_cnt = self.daily_patients(data["data"])
if period.days == 0:
if today in daily_cnt.keys():
prev_data["data"][-1]["小計"] = daily_cnt[today]
for d in range(period.days):
write_day = lastday + datetime.timedelta(days=d+1)
if write_day not in daily_cnt.keys():
#print("この日の陽性患者はいません")
pat_num = 0
else:
pat_num = daily_cnt[write_day]
prev_data["data"].append(
{
"日付": write_day.strftime("%Y-%m-%d") + "T08:00:00.000Z",
"小計": pat_num
}
)
#print(write_day)
return prev_data
def daily_patients(self, data):
date_list = []
for d in data:
date_str = d.get("公表日")
#TODO: 例外処理周りちゃんとかく
if date_str == '欠番' or date_str == '':
continue
dt = self.format_date(date_str)
dt = datetime.date(int(dt[:4]), int(dt[5:7]), int(dt[8:10]))
if '欠番' not in d.get('備考'):
date_list.append(dt)
c = collections.Counter(date_list)
return c
def add_data(self, prev_data, data):
lastday = prev_data["data"][-1]["日付"][:10]
lastday = datetime.date(int(lastday[:4]), int(lastday[5:7]), int(lastday[8:10]))
today = datetime.date.today() # timezoneはどうなるのか調査が必要
period = today - lastday
if period.days == 1: # こちらの場合はorigin_dataが対応してない土日だけ考えれば良い
return prev_data
for d in range(1, period.days):
write_day = lastday + datetime.timedelta(days=d)
prev_data["data"].append(
{
"日付": write_day.strftime("%Y-%m-%d") + "T08:00:00.000Z",
"小計": prev_data["data"][-1]["小計"]
}
)
#print(write_day)
return prev_data
def new_patients(self, url):
res = requests.get(url)
res.encoding = res.apparent_encoding # 日本語文字化け対応
soup = BeautifulSoup(res.text, 'html.parser')
out = soup.find('span', class_="fs3").get_text()
lists = re.findall(r'[1-9][0-9]*', out)
patients_num = max(lists)
#print(out, patients_num)
out2 = soup.find('h2', class_="mn0").get_text()
lists = re.findall(r'[1-9][0-9]?月[1-9][0-9]?日', out2)
patients_date = self.format_date2(lists[0])
return patients_date, patients_num
# RSSを用いて最新の4件のデータを取得
def new_info(self, url):
rss = feedparser.parse(url)
entries_data = []
for entry in rss.entries:
title = entry.title
word = "新型コロナウイルス" # ワクチンの場合もあるので'感染症'までは含めない
if word in title:
entries_data.append(entry)
if len(entries_data) == 4: break
newdata = [[self.format_date3(i.published), i.link, self.delete_date(i.title)] for i in entries_data]
return newdata
def delete_date(self, text):
out = re.sub(r'([1-9][0-9]?月[1-9][0-9]?日)', '', text)
return out
if __name__ == "__main__":
gd = GraphData()
gd.main()