Skip to content

Commit 4941383

Browse files
committed
initial commit
0 parents  commit 4941383

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.vscode
2+
*.png
3+
*.code-workspace
4+
__pycache__

config.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
csvUrl: 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv'
2+
3+
TwitterAPI:
4+
consumer:
5+
key: "xxxxxxxxxxxxxxxxx"
6+
secret: "xxxxxxxxxxxxxxxxxxxxxxxx"
7+
8+
access:
9+
token: "xxxxxxxxxxxxxxxxxxxx"
10+
secret: "xxxxxxxxxxxxxxxxxxxxxxxxxxx"

coronaStats.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import pandas as pd
2+
import matplotlib.pyplot as plt
3+
4+
class CoronaStats:
5+
6+
def __init__(self, csv_url):
7+
self.population = {'germany': 83019213, 'overall': 7418000000}
8+
self.csv_url = csv_url
9+
self.get_clean_data()
10+
11+
def get_clean_data(self):
12+
df = pd.read_csv(self.csv_url)
13+
df = df.drop(columns=['Lat', 'Long'])
14+
15+
df_group = df.groupby(['Country/Region'])
16+
17+
germany = df_group.get_group('Germany')
18+
19+
overall = df.agg(['sum'])
20+
germany = germany.agg(['sum'])
21+
22+
self.overall = self.clean_Data(overall, 'overall', ['Country/Region'])
23+
self.germany = self.clean_Data(germany, 'germany', ['Province/State', 'Country/Region'])
24+
25+
def clean_Data(self, data_frame, country, drop_columns):
26+
cleaned_data_frame = data_frame.drop(columns = drop_columns)
27+
cleaned_data_frame = cleaned_data_frame.T
28+
29+
cleaned_data_frame['related'] = (100/self.population[country]) * cleaned_data_frame['sum']
30+
31+
return cleaned_data_frame
32+
33+
def plot_diagram_related(self, filename='confirmed_compared_related'):
34+
diagram_file = self.plot_diagram('related',
35+
'Date',
36+
'Confirmed infections related to population (%)',
37+
'Confirmed COVID-19 - Related to population',
38+
filename)
39+
40+
return diagram_file
41+
42+
def plot_diagram(self, yvalue, xlabel, ylabel, title, filename):
43+
plt.figure(figsize=(8,5))
44+
ax = plt.gca()
45+
46+
self.overall.plot(kind='line', y=yvalue, ax=ax, label="Overall", color='black')
47+
self.germany.plot(kind='line', y=yvalue, ax=ax, label="Germany", color='red')
48+
49+
plt.xlabel(xlabel)
50+
plt.ylabel(ylabel)
51+
plt.title(title)
52+
plt.savefig(filename + '.png', dpi=300)
53+
54+
return filename + '.png'
55+
56+
def get_actual_infected(self):
57+
germany_changed = self.germany.tail(2)['sum'][1] - self.germany.tail(2)['sum'][0]
58+
overall_changed = self.overall.tail(2)['sum'][1] - self.overall.tail(2)['sum'][0]
59+
60+
germany = '+' if germany_changed > 0 else '-'
61+
overall = '+' if overall_changed > 0 else '-'
62+
63+
germany += str(germany_changed)
64+
overall += str(overall_changed)
65+
66+
return {
67+
'germany': [
68+
self.germany.tail(1)['sum'][0],
69+
germany
70+
],
71+
'overall': [
72+
self.overall.tail(1)['sum'][0],
73+
overall
74+
]
75+
}
76+
77+
def get_latest_update_date(self):
78+
[month, day, year] = self.overall.index[-1].split('/', 3)
79+
date = '.'.join([day, month, '20'+year])
80+
return date
81+
82+
def get_weekly_growth(self):
83+
frame = self.germany.tail(7)
84+
actual = frame['sum'][-1]
85+
days_before = frame['sum'][0]
86+
growth_rate = (actual*100/days_before) - 100
87+
88+
return growth_rate

statsbot.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import tweepy
2+
import yaml
3+
import coronaStats as st
4+
5+
def load_config():
6+
with open("config.yaml", 'r') as stream:
7+
try:
8+
config = yaml.safe_load(stream)
9+
return config
10+
11+
except yaml.YAMLError as exc:
12+
print(exc)
13+
exit
14+
15+
def tweetStats(text, config, image = None):
16+
config = load_config()
17+
18+
# Authenticate to Twitter
19+
auth = tweepy.OAuthHandler(config['TwitterAPI']['consumer']['key'], config['TwitterAPI']['consumer']['secret'])
20+
auth.set_access_token(config['TwitterAPI']['access']['token'], config['TwitterAPI']['access']['secret'])
21+
22+
api = tweepy.API(auth)
23+
24+
#Create a tweet
25+
if image:
26+
api.update_with_media(image, text)
27+
else:
28+
api.update_status(text)
29+
30+
config = load_config()
31+
myfilename = 'compared_related'
32+
stats = st.CoronaStats(config['csvUrl'])
33+
myfile = stats.plot_diagram_related(myfilename)
34+
infected = stats.get_actual_infected()
35+
date = stats.get_latest_update_date()
36+
growth_germany = stats.get_weekly_growth()
37+
38+
tweet_text = "Am " + date + " gab es " + str(infected['overall'][0]) + " (" + infected['overall'][1] +") erfasste Infektionen weltweit.\n"
39+
tweet_text += "In Deutschland: " + str(infected['germany'][0]) + " (" + infected['germany'][1] +") Infektionen.\n\n"
40+
tweet_text += "Wachstumsrate in Deutschland der letzten 7 Tage: " + "{0:.2f}".format(growth_germany) + "%\n\n"
41+
tweet_text += "Dashboard: https://my-covid-dash.herokuapp.com/"
42+
tweet_text += "\n\n#corona #mycoronastats #COVID19deutschland"
43+
44+
tweetStats(tweet_text, config, myfile)
45+
46+
exit

0 commit comments

Comments
 (0)