Switch to Google Cloud Translate API, improve string handling, remove URLs

This commit is contained in:
Klaus-Uwe Mitterer 2017-02-28 13:53:06 +01:00
parent 9cea3612ea
commit 9d389494f9

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python3
import html.parser, tweepy, os, setuptools, translate, twitools
import html.parser, tweepy, os, setuptools, google.cloud.translate, twitools, re
lang = setuptools.getListSetting("Translate", "lang")
ato = setuptools.getListSetting("Translate", "ato")
@ -35,15 +35,18 @@ timeline = twitools.twoHelper().search(search, savepoint)
tw_counter = 0
er_counter = 0
translator = google.cloud.translate.Client()
for status in timeline:
text = html.parser.HTMLParser().unescape(status.text)
if text[0] == "@":
if text[0] == "@" or text[:4] == "RT @":
continue
for a in accounts:
two = twitools.twObject(ato=a[1], ase=a[2])
tstring = translate.Translator(from_lang=setuptools.getSetting("Translate", "original"), to_lang=a[0]).translate(text).replace("@", "@")
intext = re.sub(r'https?:\/\/[\S]*', '', text)
tstring = translator.translate(text, target_language=a[0])['translatedText'].replace("@", "@")
try:
two.tweet(tstring[:140])