-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.py
47 lines (36 loc) · 1.3 KB
/
twitter.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
import os
import sys
import asyncio
import logging
import tweepy
from datetime import date
from core import LyricsBot
async def main():
#setup logging
logpath = "logs"
if not os.path.exists(logpath):
os.mkdir(logpath)
logging.basicConfig(filename=os.path.join(logpath, str(date.today()) + '.log'), level=logging.INFO)
logging.info("Program started.")
try:
auth = tweepy.OAuthHandler(os.environ["TwitterConsumerKey"], os.environ["TwitterConsumerSecret"])
auth.set_access_token(os.environ["TwitterAccessToken"], os.environ["TwitterAccessSecret"])
except KeyError as missing_key:
logging.error(f"Environment variable is missing: {missing_key}. Exiting program.")
sys.exit(1)
api = tweepy.API(auth)
bot = LyricsBot()
try:
api.verify_credentials()
logging.info("Authentication OK")
while True:
print(bot.get_lyrics())
await asyncio.sleep(60 * 3)
except tweepy.TweepError as twitter_error:
error_json = twitter_error.response.text
logging.error(f"Error {error_json.code}: {error_json.message}. Exiting program.")
sys.exit(1)
except Exception as ex:
logging.error(f"An unknown exception occurred: {ex}. Exiting program.")
sys.exit(1)
asyncio.run(main())