-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_of_the_day_bot
More file actions
73 lines (63 loc) · 2.46 KB
/
word_of_the_day_bot
File metadata and controls
73 lines (63 loc) · 2.46 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import tweepy
import datetime
import time
import requests
# API credentials
API_KEY = '5cQGdD31CxIIDX094FyEGFLkZ'
API_SECRET = 'zzli7r6XM0vZR1x1S1IgcR0wPUKTQsyR1twk1bbEyctTfZdIKr'
ACCESS_TOKEN = '1807846037756715008-JFO2kKsyk36n3wQQhmdV5D6MldqnGg'
ACCESS_SECRET = 'aWoQROOl7RjfWx0NYbSMvDHS4ACbSxA9P5BcG1692gPSg'
BEARER_TOKEN = 'AAAAAAAAAAAAAAAAAAAAAGBzugEAAAAAT%2F6DCQGluxdpiodSA4yZf3Udir4%3DwANbPZ30DYvTDsq3KrFdh48FomxckrMKpFc1v0VG6fI3fbrytq'
# WordsAPI credentials
WORDS_API_KEY = '1c9daddb3bmsh0579778b08064afp13b039jsnc5840de11bc1'
WORDS_URL = 'https://wordsapiv1.p.rapidapi.com/words/'
# Authenticating to Twitter
client = tweepy.Client(bearer_token=BEARER_TOKEN, consumer_key=API_KEY, consumer_secret=API_SECRET,
access_token=ACCESS_TOKEN, access_token_secret=ACCESS_SECRET)
# try:
# user = api.verify_credentials()
# if user:
# print(f"User name: {user.name}")
# print(f"User I.D: {user.id}")
# except tweepy.TweepyException as e:
# print(f"Error: {e}")
# Function that fetches the word for the day
def fetch_word_of_the_day():
headers = {
"x-rapidapi-key": WORDS_API_KEY,
"x-rapidapi-host": "wordsapiv1.p.rapidapi.com"
}
try:
response = requests.get(f"{WORDS_URL}?random=true", headers=headers)
print(f"Response status code: {response.status_code}")
print(f"Response content: {response.text}")
if response.status_code == 200:
word_data = response.json()
word = word_data.get('word')
definitions = word_data.get('results', [])
if definitions:
definition = definitions[0].get('definition')
return {"word": word, "definition": definition}
else:
print(f"Error fetching word: {response.status_code} - {response.text}")
except requests.RequestException as e:
print(f"Request failed: {e}")
return None
new_word = fetch_word_of_the_day()
def post_word_of_the_day():
word_of_the_day = new_word
if word_of_the_day:
tweet = f"Word of the day: {word_of_the_day['word']}\nDefinition: {word_of_the_day['definition']}"
try:
client.create_tweet(text=tweet)
print(f"Tweeted: {tweet}")
except tweepy.TweepyException as e:
print(f"Error: {e}")
else:
print("Failed to fetch a new word")
def main():
while True:
post_word_of_the_day()
time.sleep(86400)
if __name__ == "__main__":
main()