Last active
April 5, 2020 15:58
-
-
Save sr6033/8542827b5d0e87cb9de07bcd49f5b118 to your computer and use it in GitHub Desktop.
Alerts you about new cases of COVID-19 inside India
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Running using CRONTAB: | |
| 1. crontab -e | |
| 2. * * * * * /usr/bin/python3 /home/{username}/covid_alert.py >> ~/cron.log 2>&1 | |
| 3. Save & Exit | |
| (You can also use any other scheduler to run the script) | |
| """ | |
| #!usr/bin/python3 | |
| import os | |
| import requests | |
| def notify(title, subtitle, message): | |
| """ | |
| The notifier function | |
| """ | |
| t = '-title {!r}'.format(title) | |
| s = '-subtitle {!r}'.format(subtitle) | |
| m = '-message {!r}'.format(message) | |
| os.system('terminal-notifier {} -contentImage https://www.covid19india.org/icon.png'.format(' '.join([m, t, s]))) | |
| def get_covid_date(filename): | |
| """ | |
| """ | |
| resp = requests.get('https://api.covid19india.org/data.json') | |
| if resp.status_code == 200: | |
| data = resp.json() | |
| statewise_data = data['statewise'] | |
| total_dict = [el for el in statewise_data if el['state'] == 'Total'] | |
| total_dict = total_dict[0] | |
| new_str = 'New: {}' | |
| new_cases = 0 | |
| is_notify = False | |
| message_str = 'Active:{} | Recovered:{} | Death:{} | Total:{}'.format(total_dict['active'], | |
| total_dict['recovered'], | |
| total_dict['deaths'], | |
| total_dict['confirmed']) | |
| if not os.path.exists(filename): | |
| open(filename, 'w').close() | |
| file = open(filename, 'r+') | |
| value = file.read() | |
| if value == '': | |
| prev_confirmed = 0 | |
| else: | |
| prev_confirmed = int(value) | |
| if prev_confirmed == 0: | |
| new_cases = 0 | |
| file.write(total_dict['confirmed']) | |
| is_notify = True | |
| else: | |
| total_confirmed = int(total_dict['confirmed']) | |
| if prev_confirmed < total_confirmed: | |
| file.truncate(0) | |
| file.write(total_dict['confirmed']) | |
| new_cases = total_confirmed - prev_confirmed | |
| is_notify = True | |
| file.close() | |
| # Notifying | |
| if is_notify: | |
| notify(title=new_str.format(new_cases), | |
| subtitle='Active: ' + total_dict['active'], | |
| message=message_str) | |
| if __name__ == "__main__": | |
| get_covid_date('covid_data.txt') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment