Switch to Google Cloud Translate API, improve string handling, remove URLs
This commit is contained in:
parent
9cea3612ea
commit
9d389494f9
1 changed files with 6 additions and 3 deletions
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/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")
|
lang = setuptools.getListSetting("Translate", "lang")
|
||||||
ato = setuptools.getListSetting("Translate", "ato")
|
ato = setuptools.getListSetting("Translate", "ato")
|
||||||
|
@ -35,15 +35,18 @@ timeline = twitools.twoHelper().search(search, savepoint)
|
||||||
tw_counter = 0
|
tw_counter = 0
|
||||||
er_counter = 0
|
er_counter = 0
|
||||||
|
|
||||||
|
translator = google.cloud.translate.Client()
|
||||||
|
|
||||||
for status in timeline:
|
for status in timeline:
|
||||||
text = html.parser.HTMLParser().unescape(status.text)
|
text = html.parser.HTMLParser().unescape(status.text)
|
||||||
|
|
||||||
if text[0] == "@":
|
if text[0] == "@" or text[:4] == "RT @":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for a in accounts:
|
for a in accounts:
|
||||||
two = twitools.twObject(ato=a[1], ase=a[2])
|
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:
|
try:
|
||||||
two.tweet(tstring[:140])
|
two.tweet(tstring[:140])
|
||||||
|
|
Loading…
Reference in a new issue