-
Notifications
You must be signed in to change notification settings - Fork 0
/
engBot.py
59 lines (48 loc) · 1.7 KB
/
engBot.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
48
49
50
51
52
53
54
55
56
57
58
59
from flask_api import FlaskAPI
from flask import request
app = FlaskAPI(__name__)
def getMessage(wordSupply = None):
import json
import random
import tweepy
from PyDictionary import PyDictionary
from googletrans import Translator
translator = Translator()
auth = tweepy.OAuthHandler("CONSUMER_KEY", "CONSUMER_SECRET")
auth.set_access_token("ACCESS_TOKEN", "ACCESS_TOKEN_SECRET")
tweeterApi = tweepy.API(auth)
dic = PyDictionary()
if wordSupply:
generatedWord = wordSupply
else:
with open("wordList.json") as fo:
data = json.load(fo)
generatedWord = random.choice(data)
print(generatedWord, "------ generated word")
meaningData = dic.meaning(generatedWord)
try:
tweetMsg = ""
tweetMsg += "Word: " + generatedWord.capitalize() + "\n"
for key in meaningData.keys():
oneByOne = meaningData[key]
if len(oneByOne) != 0:
tweetMsg += key + ": " + oneByOne[0].capitalize() + "\n"
tweetMsg += "French Word: "+ str(translator.translate(generatedWord, dest='fr').text).capitalize() +"\n"
tweetMsg += "#"+generatedWord + " #engBotPy"
print(tweetMsg)
try:
tweeterApi.update_status(tweetMsg)
return tweetMsg
except tweepy.error.TweepError:
return {"error_msg":"tweet already present"}
except AttributeError:
return {"error_msg":"No definition found"}
print(meaningData)
@app.route('/doTweet/')
def doTweet():
return {'tweetMsg': getMessage()}
@app.route("/doTweet/<string:key>/")
def notes_detail(key):
return getMessage(key)
if __name__ == "__main__":
app.run(debug=False)