Skip to content

Commit eefb5fa

Browse files
authored
Add files via upload
1 parent b1df999 commit eefb5fa

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

ka.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from flask import Flask, render_template
2+
from threading import Thread
3+
4+
app = Flask(__name__)
5+
6+
7+
@app.route('/')
8+
def index():
9+
return "Word Of the Day is running! https://twitter.com/Word_OTDay"
10+
11+
12+
def run():
13+
app.run(host='0.0.0.0', port=8080)
14+
15+
16+
def keep_alive():
17+
t = Thread(target=run)
18+
t.start()

main.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import random
2+
import tweepy
3+
from PyDictionary import PyDictionary
4+
from random_words import RandomWords
5+
import datetime
6+
import time
7+
from ka import keep_alive
8+
9+
keep_alive()
10+
11+
# Twitter Setup
12+
consumer_key = "CONSUMER_KEY"
13+
consumer_secret_key = "CONSUMER_SECRET_KEY"
14+
access_token = "ACCESS_TOKEN"
15+
access_token_secret = "ACCESS_TOKEN_SECRET"
16+
17+
auth = tweepy.OAuthHandler(consumer_key, consumer_secret_key)
18+
auth.set_access_token(access_token, access_token_secret)
19+
20+
21+
# Actual Code
22+
def load_random_word():
23+
rw = RandomWords()
24+
dictionary = PyDictionary()
25+
26+
word = rw.random_word()
27+
definitions = dictionary.meaning(word)
28+
29+
try:
30+
part_of_speech = random.choice(list(definitions.keys()))
31+
definition = random.choice(definitions[part_of_speech])
32+
except:
33+
return "NULL_DEFINITION"
34+
35+
return {
36+
"word": word,
37+
"definition": definition.capitalize(),
38+
"part_of_speech": part_of_speech
39+
}
40+
41+
42+
while True:
43+
if 8 <= datetime.datetime.now().hour <= 10:
44+
word_of_the_day = load_random_word()
45+
46+
while word_of_the_day == "NULL_DEFINITION":
47+
word_of_the_day = load_random_word()
48+
49+
# Twitter Post
50+
wotd_tweet = f'Today\'s #WordOfTheDay is {word_of_the_day["word"]}! ({word_of_the_day["part_of_speech"]}) \n\n{word_of_the_day["definition"]}.'
51+
api = tweepy.API(auth)
52+
api.update_status(wotd_tweet)
53+
54+
time.sleep(1.75 * 3600) # Sleep for 1.45 hours

0 commit comments

Comments
 (0)