| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
import io |
| 15 |
import sys |
| 16 |
import time |
| 17 |
import setuptools._distutils.util |
| 18 |
import xml.etree.ElementTree as ET |
| 19 |
import itertools as IT |
| 20 |
from googletrans import Translator |
| 21 |
from bs4 import BeautifulSoup |
| 22 |
|
| 23 |
lang_from = sys.argv[1] |
| 24 |
lang_to = sys.argv[2] |
| 25 |
parallel = bool(setuptools._distutils.util.strtobool(sys.argv[3])) |
| 26 |
|
| 27 |
if (len(sys.argv) >= 5): |
| 28 |
src_enc = sys.argv[4] |
| 29 |
else: |
| 30 |
src_enc = "utf-8" |
| 31 |
|
| 32 |
if parallel: |
| 33 |
import asyncio |
| 34 |
from concurrent.futures import ThreadPoolExecutor |
| 35 |
|
| 36 |
sys.stdin.reconfigure(encoding = src_enc) |
| 37 |
rss_data = sys.stdin.read() |
| 38 |
|
| 39 |
try: |
| 40 |
rss_document = ET.fromstring(rss_data) |
| 41 |
except ET.ParseError as err: |
| 42 |
lineno, column = err.position |
| 43 |
line = next(IT.islice(io.StringIO(rss_data), lineno)) |
| 44 |
caret = '{:=>{}}'.format('^', column) |
| 45 |
err.msg = '{}\n{}\n{}'.format(err, line, caret) |
| 46 |
raise |
| 47 |
|
| 48 |
translator = Translator() |
| 49 |
|
| 50 |
atom_ns = {"ns": "http://www.w3.org/2005/Atom"} |
| 51 |
cont_ns = {"cont": "http://purl.org/rss/1.0/modules/content/"} |
| 52 |
|
| 53 |
def translate_string(to_translate): |
| 54 |
try: |
| 55 |
if to_translate is None: |
| 56 |
return to_translate |
| 57 |
|
| 58 |
translated_text = translator.translate(to_translate, src = lang_from, dest = lang_to) |
| 59 |
|
| 60 |
if not parallel: |
| 61 |
time.sleep(0.2) |
| 62 |
|
| 63 |
return translated_text.text |
| 64 |
except: |
| 65 |
return to_translate |
| 66 |
|
| 67 |
def process_article(article): |
| 68 |
title = article.find("title") |
| 69 |
|
| 70 |
if title is None: |
| 71 |
title = article.find("ns:title", atom_ns) |
| 72 |
|
| 73 |
if title is not None: |
| 74 |
title.text = translate_string(title.text) |
| 75 |
|
| 76 |
|
| 77 |
contents = article.find("cont:encoded", cont_ns) |
| 78 |
|
| 79 |
if contents is None: |
| 80 |
|
| 81 |
contents = article.find("description") |
| 82 |
|
| 83 |
if contents is None: |
| 84 |
|
| 85 |
contents = article.find("ns:content", atom_ns) |
| 86 |
|
| 87 |
if contents is not None: |
| 88 |
htmll = "<div>{}</div>".format(contents.text) |
| 89 |
|
| 90 |
soup = BeautifulSoup(htmll, features = "lxml") |
| 91 |
contents.text = translate_string(soup.get_text()) |
| 92 |
contents.text = contents.text.replace("\n", "<br/>") |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
channel = rss_document.find("channel") |
| 97 |
|
| 98 |
if channel is not None: |
| 99 |
title = channel.find("title") |
| 100 |
|
| 101 |
if (channel is None) or (title is None): |
| 102 |
|
| 103 |
title = rss_document.find("ns:title", atom_ns) |
| 104 |
|
| 105 |
if title is not None: |
| 106 |
title.text = translate_string(title.text) |
| 107 |
|
| 108 |
|
| 109 |
if parallel: |
| 110 |
with ThreadPoolExecutor(max_workers = 2) as executor: |
| 111 |
futures = [] |
| 112 |
for article in rss_document.findall(".//item"): |
| 113 |
futures.append(executor.submit(process_article, article)) |
| 114 |
for article in rss_document.findall(".//ns:entry", atom_ns): |
| 115 |
futures.append(executor.submit(process_article, article)) |
| 116 |
for future in futures: |
| 117 |
future.result() |
| 118 |
else: |
| 119 |
for article in rss_document.findall(".//item"): |
| 120 |
process_article(article) |
| 121 |
for article in rss_document.findall(".//ns:entry", atom_ns): |
| 122 |
process_article(article) |
| 123 |
|
| 124 |
out_xml = ET.tostring(rss_document) |
| 125 |
out_decoded_xml = out_xml.decode() |
| 126 |
|
| 127 |
print(out_decoded_xml) |