-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
53 lines (44 loc) · 1.59 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import xml.etree.ElementTree as ET
from urllib import request
import requests
import os
import deepl
def main():
# download rss feeds
response = requests.get("https://www.nature.com/nature.rss")
with open("nature.rss", "w") as file:
file.write(response.text)
response = requests.get("http://mediagazer.com/feed.xml")
with open("mediagazer.rss", "w") as file:
file.write(response.text)
# deepl
API_KEY = os.environ.get("DEEPL_KEY")
translator = deepl.Translator(API_KEY)
target_language = "JA"
#
# # parse Nature
# tree = ET.parse("nature.rss")
# root = tree.getroot()
# items = root.findall("item", {"": "http://purl.org/rss/1.0/"})
#
# for child in items:
# result = translator.translate_text(
# child.findall("title", {"", "http://purl.org/dc/elements/1.1/"})[0].text,
# target_lang=target_language,
# )
#
# child.findall("title", {"", "http://purl.org/dc/elements/1.1/"})[
# 0
# ].text = result.text
# child.findall("title", {"": "http://purl.org/rss/1.0/"})[0].text = result.text
# tree.write("nature.rss", encoding="utf-8", xml_declaration=True)
# parse mediagazer
tree = ET.parse("mediagazer.rss")
root = tree.getroot()
for child in root.find("channel").findall("item"):
title = child.find("title")
t_title = translator.translate_text(title.text, target_lang=target_language)
title.text = t_title.text
tree.write("mediagazer.rss", encoding="utf-8", xml_declaration=True)
if __name__ == "__main__":
main()