-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate-bot.py
79 lines (66 loc) · 2.41 KB
/
translate-bot.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import praw
from googletrans import Translator
import iso639
# Connect to Reddit
pass_file = open("password.txt")
pw = pass_file.read()
print(pw)
id_file = open("id.txt")
id = id_file.read()
print(id)
secret_file = open("secret.txt")
secret = secret_file.read()
print(secret)
reddit = praw.Reddit(client_id=id,
client_secret=secret,
username="gtranslatebot",
password=pw,
user_agent="google translate bot by /u/chirag03k")
subreddits = reddit.subreddit("all")
# Do the Bot Stuff
keyphrase = "!translate"
translator = Translator()
for comment in subreddits.stream.comments():
comment_text = comment.body
if keyphrase in comment_text:
try:
to_translate = comment.parent().body
print(to_translate)
except:
to_translate = None
comment_tokens = comment_text.split()
# Parsing
if "from" in comment_text:
try:
from_lang = comment_tokens[comment_tokens.index("from") + 1]
from_lang_code = iso639.to_iso639_1(from_lang)
except:
from_lang_code = "Failure"
else:
from_lang_code = None
if "to" in comment_text:
try:
to_lang = comment_tokens[comment_tokens.index("to") + 1]
to_lang_code = iso639.to_iso639_1(to_lang)
except:
to_lang_code = "Failure"
else:
to_lang_code = "en"
# Generating Output
if from_lang_code is "Failure":
output = "I don't know how to translate from " + from_lang
elif to_lang_code is "Failure":
output = "I don't know how to translate into " + to_lang
elif to_translate is None:
output = "I couldn't load the parent comment"
elif from_lang_code is None:
translation = translator.translate(to_translate, dest=to_lang_code)
output = 'Translated from ' + iso639.to_name(translation.src) + '''.
''' + translation.text
else:
translation = translator.translate(to_translate, src=from_lang_code, dest=to_lang_code)
output = 'Transated from ' + iso639.to_name(translation.src) + 'to' + iso639.to_name(translation.dest) + '''.
''' + translation.text
#Replying
comment.reply(output)
print("replied " + output)