twitools/transbot.py
Klaus-Uwe Mitterer 72392f1b72 Refactoring.
2017-02-28 12:01:35 +01:00

60 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env python3
import html.parser, tweepy, os, setuptools, translate, twitools
lang = setuptools.getListSetting("Translate", "lang")
ato = setuptools.getListSetting("Translate", "ato")
ase = setuptools.getListSetting("Translate", "ase")
if not (len(lang) == len(ato) and len(ato) == len(ase)):
raise setuptools.SetupException("Lists do not match in config.cfg.")
accounts = []
i = 0
while i < len(lang):
accounts += [[lang[i], ato[i], ase[i]]]
i += 1
origin = twitools.twoHelper().whoami()
search = "from:" + origin
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 = ""
print("No savepoint found. Trying to get as many results as possible.")
timeline = twitools.twoHelper().search(search, savepoint)
tw_counter = 0
er_counter = 0
for status in timeline:
text = html.parser.HTMLParser().unescape(status.text))
if text[0] == "@":
continue
for a in accounts:
two = twitools.twObject(ato=a[1], ase=a[2])
tstring += translate.Translator(to_lang=a[0]).translate(text)
try:
two.tweet(tstring[:140])
tw_counter += 1
except Exception as e:
print(e)
er_counter += 1
continue
with open(last_id_file, "w") as file:
file.write(str(status.id))
print("Finished. %d Tweets retweeted. %d errors occurred." % (tw_counter, er_counter))