forked from buu700/napster.fm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
executable file
·109 lines (74 loc) · 2.81 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import re
import time
from bs4 import BeautifulSoup
# https://github.com/openlabs/Microsoft-Translator-Python-API
# Also, yes, my API key is public
from microsofttranslator import Translator
def translate(text, language):
global translator
for i in range(5):
try:
translation = translator.translate(text, language)
if 'ArgumentException' in translation:
raise Exception(translation)
return re.sub(u'peer.fm', u'Peer.fm', translation, flags = re.IGNORECASE)
except Exception, e:
f = open('translate.log', 'w+')
f.write(str(e))
f.close()
time.sleep(20)
translator = Translator('peerfm', 'bJHtNJK4zTLAGtaoyxnO5wI1N3XHN8Fygz2pEn11WTQ=')
return text
def swapStringWithChild(elem, s, child):
newElem = BeautifulSoup(unicode(elem).replace(unicode(s), unicode(child))).find_all()[2]
elem.replace_with(newElem)
return newElem
codec = 'utf8'
placeholder = u'FUCKMAINE'
f = open('languages.json', 'r')
languages = json.loads(f.read())
f.close()
f = open('index.html', 'r')
baseHtml = f.read()
f.close()
for language in languages:
f = open(language + '.html', 'w')
html = BeautifulSoup(baseHtml)
for elem in html.find_all():
descendants = elem.find_all()
shouldTranslate = True
shouldTranslate = shouldTranslate and elem.get('notranslate') is None
shouldTranslate = shouldTranslate and (not descendants or unicode(elem.parent.get('hash-location')) == u'#about')
shouldTranslate = shouldTranslate and (elem.parent is None or elem.parent.parent is None or unicode(elem.parent.parent.get('hash-location')) != u'#about')
if not shouldTranslate:
continue
content = elem.get('content')
if content is not None and elem.get('name') in ['description', 'keywords', 'author']:
elem['content'] = translate(unicode(content), language)
# Swap out children with placeholders
for i in range(len(descendants)):
descendants[i].string = translate(unicode(descendants[i].string), language)
descendants[i].replace_with(placeholder + unicode(i))
if descendants:
elem.string = elem.text
text = unicode(elem.string)
if elem.string is None or text.strip() == u'':
continue
bindings = re.findall('\\{\\{.*?\\}\\}', text)
# Swap out Angular bindings with placeholders
for i in range(len(bindings)):
text = text.replace(bindings[i], placeholder + str(i))
text = translate(text, language)
# Swap out placeholders with Angular bindings
for i in range(len(bindings)):
text = text.replace(placeholder + str(i), bindings[i])
print(text)
elem.string = text
# Swap out placeholders with children
for i in range(len(descendants)):
elem = swapStringWithChild(elem, placeholder + unicode(i), descendants[i])
f.write(unicode(html).encode(codec))
f.close()