-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatePatients.py
76 lines (69 loc) · 2.75 KB
/
updatePatients.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
import json
import requests
from bs4 import BeautifulSoup
import datetime
import pytz
import boto3
client = boto3.resource('dynamodb')
table = client.Table('Patients')
def getTS(month, date):
now = datetime.datetime(2020, month, date)
tz = pytz.timezone('Asia/Seoul')
seoul = now.replace(tzinfo=pytz.utc).astimezone(tz).timestamp()
return int(seoul) - 9 * 3600
def lambda_handler(event, context):
url = 'http://www.busan.go.kr/corona19/index'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
banner = soup.find('div', class_='banner').find('span', class_='item1').text.split(' ')
updatedDate = f'질병관리본부 {banner[0]} {banner[1]} {banner[3]} {banner[4]}'
busanTable = soup.find('div', class_='list_body').find_all('ul')
for item in busanTable:
basic = item.find_all('li')[0].text.split('(')
patientNo = int(basic[0].replace('부산-', '').strip())
age = 2021-int(basic[1].split('/')[0].replace('년생', ''))
gender = basic[1].split('/')[1].strip()
residence = basic[1].split('/')[2].replace(')', '').strip()
if '외국인' in basic[1]:
country = '외국'
else:
country = '한국'
hospital = item.find_all('li')[3].text
if '퇴원' in hospital:
status = '퇴원'
hospital = hospital[3:-1]
elif '사망' in hospital:
status = '사망'
else:
status = '입원'
formatDate = item.find_all('li')[4].text.split('/')
confirmDate = f'{int(formatDate[0])}월 {int(formatDate[1])}월'
timestamp = getTS(int(formatDate[0]), int(formatDate[1]))
table.update_item(
Key={
'province': '부산',
'patientID': f'부산 {patientNo}',
},
UpdateExpression='SET city = :city, patientNo = :patientNo, confirmDate = :confirmDate, #timestamp = :timestamp, gender = :gender, age = :age, country = :country, residence = :residence, hospital = :hospital, #status = :status, updatedDate = :updatedDate',
ExpressionAttributeNames={
'#status': 'status',
'#timestamp': 'timestamp'
},
ExpressionAttributeValues={
':city': '부산',
':patientNo': patientNo,
':confirmDate': confirmDate,
':timestamp': timestamp,
':gender': gender,
':age': age,
':country': country,
':residence': residence,
':hospital': hospital,
':status': status,
':updatedDate': updatedDate
}
)
return {
'statusCode': 200,
'body': 'Update Complete'
}