Refactoring.
This commit is contained in:
parent
429d3ffba2
commit
72392f1b72
1 changed files with 19 additions and 40 deletions
59
transbot.py
59
transbot.py
|
@ -1,24 +1,28 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import html.parser, tweepy, os, translate
|
||||
import html.parser, tweepy, os, setuptools, translate, twitools
|
||||
|
||||
# [target_language,cons_key,cons_secret,access_token,access_secret]
|
||||
lang = setuptools.getListSetting("Translate", "lang")
|
||||
ato = setuptools.getListSetting("Translate", "ato")
|
||||
ase = setuptools.getListSetting("Translate", "ase")
|
||||
|
||||
accounts = [
|
||||
]
|
||||
if not (len(lang) == len(ato) and len(ato) == len(ase)):
|
||||
raise setuptools.SetupException("Lists do not match in config.cfg.")
|
||||
|
||||
origin = "Handle of original account (without @)"
|
||||
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)
|
||||
|
||||
auth = tweepy.OAuthHandler(accounts[0][1], accounts[0][2])
|
||||
auth.set_access_token(accounts[0][3], accounts[0][4])
|
||||
api = tweepy.API(auth)
|
||||
|
||||
try:
|
||||
with open(last_id_file, "r") as file:
|
||||
savepoint = file.read()
|
||||
|
@ -26,50 +30,25 @@ except IOError:
|
|||
savepoint = ""
|
||||
print("No savepoint found. Trying to get as many results as possible.")
|
||||
|
||||
timelineIterator = list(tweepy.Cursor(api.search, q=search, since_id=savepoint).items())
|
||||
|
||||
timeline = []
|
||||
|
||||
for status in timelineIterator:
|
||||
timeline.append(status)
|
||||
|
||||
timeline.reverse()
|
||||
timeline = twitools.twoHelper().search(search, savepoint)
|
||||
|
||||
tw_counter = 0
|
||||
er_counter = 0
|
||||
|
||||
for status in timeline:
|
||||
print("(%(date)s) %(name)s: %(message)s\n" % \
|
||||
{ "date" : status.created_at,
|
||||
"name" : status.author.screen_name.encode('utf-8'),
|
||||
"message" : status.text.encode('utf-8') })
|
||||
|
||||
text = html.parser.HTMLParser().unescape(status.text).encode('utf-8')
|
||||
text = html.parser.HTMLParser().unescape(status.text))
|
||||
|
||||
if text[0] == "@":
|
||||
continue
|
||||
|
||||
for a in accounts:
|
||||
auth = tweepy.OAuthHandler(a[1], a[2])
|
||||
auth.set_access_token(a[3], a[4])
|
||||
api = tweepy.API(auth)
|
||||
|
||||
tstring = ""
|
||||
curstr = ""
|
||||
|
||||
for word in text.split(" "):
|
||||
curstr += word + " "
|
||||
if word[-1] in "!?.":
|
||||
tstring += translate.Translator(to_lang=a[0]).translate(curstr) + " "
|
||||
curstr = ""
|
||||
|
||||
if curstr != "":
|
||||
tstring += translate.Translator(to_lang=a[0]).translate(curstr)
|
||||
two = twitools.twObject(ato=a[1], ase=a[2])
|
||||
tstring += translate.Translator(to_lang=a[0]).translate(text)
|
||||
|
||||
try:
|
||||
api.update_status(tstring.encode('utf-8')[:140])
|
||||
two.tweet(tstring[:140])
|
||||
tw_counter += 1
|
||||
except tweepy.error.TweepError as e:
|
||||
except Exception as e:
|
||||
print(e)
|
||||
er_counter += 1
|
||||
continue
|
||||
|
|
Loading…
Reference in a new issue