2017-02-28 10:03:20 +00:00
|
|
|
|
#!/usr/bin/env python3
|
2015-03-19 00:28:56 +00:00
|
|
|
|
|
2017-02-28 12:53:06 +00:00
|
|
|
|
import html.parser, tweepy, os, setuptools, google.cloud.translate, twitools, re
|
2015-03-19 00:28:56 +00:00
|
|
|
|
|
2017-02-28 11:01:35 +00:00
|
|
|
|
lang = setuptools.getListSetting("Translate", "lang")
|
|
|
|
|
ato = setuptools.getListSetting("Translate", "ato")
|
|
|
|
|
ase = setuptools.getListSetting("Translate", "ase")
|
2015-03-19 00:28:56 +00:00
|
|
|
|
|
2017-02-28 11:01:35 +00:00
|
|
|
|
if not (len(lang) == len(ato) and len(ato) == len(ase)):
|
|
|
|
|
raise setuptools.SetupException("Lists do not match in config.cfg.")
|
2015-03-19 00:28:56 +00:00
|
|
|
|
|
2017-02-28 11:01:35 +00:00
|
|
|
|
accounts = []
|
|
|
|
|
i = 0
|
2015-03-19 00:31:18 +00:00
|
|
|
|
|
2017-02-28 11:01:35 +00:00
|
|
|
|
while i < len(lang):
|
|
|
|
|
accounts += [[lang[i], ato[i], ase[i]]]
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
origin = twitools.twoHelper().whoami()
|
2015-03-19 00:31:18 +00:00
|
|
|
|
search = "from:" + origin
|
2015-03-19 00:28:56 +00:00
|
|
|
|
|
|
|
|
|
last_id_filename = "last_id"
|
|
|
|
|
rt_bot_path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
last_id_file = os.path.join(rt_bot_path, last_id_filename)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with open(last_id_file, "r") as file:
|
|
|
|
|
savepoint = file.read()
|
|
|
|
|
except IOError:
|
|
|
|
|
savepoint = ""
|
2017-02-28 10:02:43 +00:00
|
|
|
|
print("No savepoint found. Trying to get as many results as possible.")
|
2015-03-19 00:28:56 +00:00
|
|
|
|
|
2017-02-28 11:01:35 +00:00
|
|
|
|
timeline = twitools.twoHelper().search(search, savepoint)
|
2015-03-19 00:28:56 +00:00
|
|
|
|
|
|
|
|
|
tw_counter = 0
|
|
|
|
|
er_counter = 0
|
|
|
|
|
|
2017-02-28 12:53:06 +00:00
|
|
|
|
translator = google.cloud.translate.Client()
|
|
|
|
|
|
2015-03-19 00:28:56 +00:00
|
|
|
|
for status in timeline:
|
2017-02-28 11:03:12 +00:00
|
|
|
|
text = html.parser.HTMLParser().unescape(status.text)
|
2015-03-19 00:28:56 +00:00
|
|
|
|
|
2017-02-28 12:53:06 +00:00
|
|
|
|
if text[0] == "@" or text[:4] == "RT @":
|
2015-03-19 00:28:56 +00:00
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
for a in accounts:
|
2017-02-28 11:01:35 +00:00
|
|
|
|
two = twitools.twObject(ato=a[1], ase=a[2])
|
2017-02-28 12:53:06 +00:00
|
|
|
|
intext = re.sub(r'https?:\/\/[\S]*', '', text)
|
2017-03-01 19:55:39 +00:00
|
|
|
|
tstring = translator.translate(intext, target_language=a[0])['translatedText'].replace("@", "@")
|
2015-03-19 00:57:48 +00:00
|
|
|
|
|
2015-03-19 00:28:56 +00:00
|
|
|
|
try:
|
2017-02-28 11:01:35 +00:00
|
|
|
|
two.tweet(tstring[:140])
|
2015-03-19 00:28:56 +00:00
|
|
|
|
tw_counter += 1
|
2017-02-28 11:01:35 +00:00
|
|
|
|
except Exception as e:
|
2017-02-28 10:02:43 +00:00
|
|
|
|
print(e)
|
2015-03-19 00:28:56 +00:00
|
|
|
|
er_counter += 1
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
with open(last_id_file, "w") as file:
|
|
|
|
|
file.write(str(status.id))
|
|
|
|
|
|
2017-02-28 10:02:43 +00:00
|
|
|
|
print("Finished. %d Tweets retweeted. %d errors occurred." % (tw_counter, er_counter))
|