-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
47 lines (36 loc) · 1.47 KB
/
main.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 asyncio
import requests
from twikit import Client
import os
import base64
import json
# Initialize client with locale
client = Client('en-US')
wordnik_url = f'http://api.wordnik.com/v4/words.json/wordOfTheDay?api_key={os.getenv("WORDNIK_API_KEY")}'
async def main():
try:
# Decode the cookies.json content from the environment variable
cookies_json_content = os.getenv('COOKIES_JSON')
if not cookies_json_content:
raise ValueError("COOKIES_JSON environment variable is not set")
cookies_json = base64.b64decode(cookies_json_content).decode('utf-8')
# Save the decoded content to a temporary file
with open('cookies.json', 'w') as file:
file.write(cookies_json)
client.load_cookies('cookies.json')
# Get the word of the day from Wordnik
response = requests.get(wordnik_url)
response.raise_for_status() # Ensure we notice bad responses
data = response.json()
word_of_the_day = data['word']
definition = data['definitions'][0]['text']
# Format the tweet
tweet = f"📚 Word of the day: {word_of_the_day}\n\nDefinition: {definition}"
# Create a tweet with the provided text
await client.create_tweet(text=tweet)
print("Tweet posted successfully!")
except requests.RequestException as e:
print(f"HTTP error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
asyncio.run(main())