Created
March 31, 2020 14:46
-
-
Save sagunsh/d414306948ff003eeeb80d993466a2f0 to your computer and use it in GitHub Desktop.
This file contains 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
import sys | |
from config import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET | |
import tweepy | |
import requests | |
from lxml import html | |
def create_tweet(): | |
response = requests.get('https://www.worldometers.info/coronavirus/') | |
doc = html.fromstring(response.content) | |
total, deaths, recovered = doc.xpath('//div[@class="maincounter-number"]/span/text()') | |
tweet = f'''Coronavirus Latest Updates | |
Total cases: {total} | |
Recovered: {recovered} | |
Deaths: {deaths} | |
Source: https://www.worldometers.info/coronavirus/ | |
#coronavirus #covid19 #coronavirusnews #coronavirusupdates | |
''' | |
return tweet | |
if __name__ == '__main__': | |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) | |
# Create API object | |
api = tweepy.API(auth) | |
try: | |
api.verify_credentials() | |
print('Authentication Successful') | |
except: | |
print('Error while authenticating API') | |
sys.exit(1) | |
tweet = create_tweet() | |
api.update_status(tweet) | |
print('Tweet successful') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment