-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.py
59 lines (47 loc) · 1.96 KB
/
global.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
import tweepy
import requests
import time
from datetime import datetime
import logging
import re
import credentials #credentials.py file contains all 4 tokens and secrets
auth = tweepy.OAuthHandler(credentials.consumer_key, credentials.consumer_secret)
auth.set_access_token(credentials.access_token, credentials.access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True, wait_on_rate_limit_notify=True) #Auth Responce Stored in Variable Api.
logging.basicConfig(filename="global_error.log", format='%(asctime)s %(message)s', filemode='w')
logger=logging.getLogger()
logger.setLevel(logging.DEBUG)
#Tweet Global Statistics
def globalDataTweet():
#API End Point
url = "https://covid19-server.chrismichael.now.sh/api/v1/AllReports"
res = requests.get(url)
if res.status_code == 200:
data = res.json()
cases = data['reports'][0]['cases']
deaths = data['reports'][0]['deaths']
recovered = data['reports'][0]['recovered']
cases = format (cases, ',d')
deaths =format (deaths, ',d')
recovered = format (recovered, ',d')
sendTweet(cases,deaths,recovered)
# print("Total Cases "+str(cases))
# print("Total Death "+str(deaths))
# print("Total Recovered "+str(recovered))
else:
print("API Error - " + str(datetime.now()))
logger.error("API Error")
# Function to Send Tweets / This function called in globalDataTweet() function
def sendTweet(cases,deaths,recovered):
tweetText = "🦠COVID-19 CORONAVIRUS🦠 PANDEMIC \n🌎Global Data🌐 \n\n😟Total Cases : "+str(cases)+"\n😀Total Recoverd : "+str(recovered)+"\n😔Total Deaths : "+str(deaths)+"\n#Corona #Covid19 #Coronavirus #Follow #CoronaUpdate"
if api.update_status(tweetText):
print('tweeted')
else:
print('error in tweepy')
#Do Tweet Every Hour
while True:
try:
globalDataTweet()
except tweepy.TweepError as error:
print(error)
time.sleep(3600)