Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions corona_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
logging.basicConfig(format=FORMAT, level=logging.DEBUG, filename='bot.log', filemode='a')

URL = 'https://www.mohfw.gov.in/'
SHORT_HEADERS = ['Sno', 'State','In','Fr','Cd','Dt']
SHORT_HEADERS = ['S. No.', 'State','Total Confirmed cases ','Cured/Discharged/Migrated','Death']
FILE_NAME = 'corona_india_data.json'
extract_contents = lambda row: [x.text.replace('\n', '') for x in row]

Expand Down Expand Up @@ -48,7 +48,7 @@ def load():
for row in all_rows:
stat = extract_contents(row.find_all('td'))
if stat:
if len(stat) == 5:
if len(stat) != 5:
# last row
stat = ['', *stat]
stats.append(stat)
Expand All @@ -59,19 +59,28 @@ def load():
cur_data = {x[1]: {current_time: x[2:]} for x in stats}

changed = False
changed_or_new_states = []

def display_change(data):
msg = ''
for i in range(3):
msg += f"{SHORT_HEADERS[i+2]}: {data[i]}\n"
return msg

for state in cur_data:
if state not in past_data:
# new state has emerged
info.append(f'NEW_STATE {state} got corona virus: {cur_data[state][current_time]}')
past_data[state] = {}
changed = True
changed_or_new_states.append(state)
else:
past = past_data[state]['latest']
cur = cur_data[state][current_time]
if past != cur:
changed = True
info.append(f'Change for {state}: {past}->{cur}')
changed_or_new_states.append(state)
info.append(f'Change for {state}:\nBefore:\n{display_change(past)}Now:\n{display_change(cur)}')

events_info = ''
for event in info:
Expand All @@ -80,13 +89,13 @@ def load():

if changed:
# override the latest one now
for state in cur_data:
for state in changed_or_new_states:
past_data[state]['latest'] = cur_data[state][current_time]
past_data[state][current_time] = cur_data[state][current_time]
save(past_data)

table = tabulate(stats, headers=SHORT_HEADERS, tablefmt='psql')
slack_text = f'Please find CoronaVirus Summary for India below:\n{events_info}\n```{table}```'
slack_text = f'Please find CoronaVirus Summary for India below:\n{events_info}\n{table}'
slacker()(slack_text)
except Exception as e:
logging.exception('oops, corono script failed.')
Expand Down
2 changes: 1 addition & 1 deletion corona_india_data.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Andhra Pradesh": {"19/03/2020 06:14": ["1", "0", "0", "0"], "latest": ["1", "0", "0", "0"]}, "Delhi": {"19/03/2020 06:14": ["11", "1", "2", "1"], "latest": ["11", "1", "2", "1"]}, "Haryana": {"19/03/2020 06:14": ["3", "14", "0", "0"], "latest": ["3", "14", "0", "0"]}, "Karnataka": {"19/03/2020 06:14": ["14", "0", "0", "1"], "latest": ["14", "0", "0", "1"]}, "Kerala": {"19/03/2020 06:14": ["25", "2", "3", "0"], "latest": ["25", "2", "3", "0"]}, "Maharashtra": {"19/03/2020 06:14": ["42", "3", "0", "1"], "latest": ["42", "3", "0", "1"]}, "Odisha": {"19/03/2020 06:14": ["1", "0", "0", "0"], "latest": ["1", "0", "0", "0"]}, "Pondicherry": {"19/03/2020 06:14": ["1", "0", "0", "0"], "latest": ["1", "0", "0", "0"]}, "Punjab": {"19/03/2020 06:14": ["1", "0", "0", "0"], "latest": ["1", "0", "0", "0"]}, "Rajasthan": {"19/03/2020 06:14": ["5", "2", "3", "0"], "latest": ["5", "2", "3", "0"]}, "Tamil Nadu": {"19/03/2020 06:14": ["2", "0", "1", "0"], "latest": ["2", "0", "1", "0"]}, "Telengana": {"19/03/2020 06:14": ["4", "2", "1", "0"], "latest": ["4", "2", "1", "0"]}, "Union Territory of Chandigarh": {"19/03/2020 06:14": ["1", "0", "0", "0"], "latest": ["1", "0", "0", "0"]}, "Union Territory of Jammu and Kashmir": {"19/03/2020 06:14": ["4", "0", "0", "0"], "latest": ["4", "0", "0", "0"]}, "Union Territory of Ladakh": {"19/03/2020 06:14": ["8", "0", "0", "0"], "latest": ["8", "0", "0", "0"]}, "Uttar Pradesh": {"19/03/2020 06:14": ["16", "1", "5", "0"], "latest": ["16", "1", "5", "0"]}, "Uttarakhand": {"19/03/2020 06:14": ["1", "0", "0", "0"], "latest": ["1", "0", "0", "0"]}, "West Bengal": {"19/03/2020 06:14": ["1", "0", "0", "0"], "latest": ["1", "0", "0", "0"]}, "Total number of confirmed cases in India": {"19/03/2020 06:14": ["141", "25", "15", "3"], "latest": ["141", "25", "15", "3"]}}
{}