Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

改变百度翻译的调用思路 #16

Open
wants to merge 87 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
8282ce1
Create README.md
PantsuDango Feb 29, 2020
bfae802
Update README.md
PantsuDango Feb 29, 2020
bfa2488
Update README.md
PantsuDango Feb 29, 2020
a116347
Merge branch 'master' of https://github.com/PantsuDango/Dango-Translator
PantsuDango Feb 29, 2020
39c23f8
Update README.md
PantsuDango Feb 29, 2020
bb92239
Update README.md
PantsuDango Mar 2, 2020
1107a0b
Update README.md
PantsuDango Mar 2, 2020
0f90581
Update README.md
PantsuDango Mar 2, 2020
ada6654
Update README.md
PantsuDango Mar 2, 2020
3efbf43
Update README.md
PantsuDango Mar 2, 2020
7367d0b
Update README.md
PantsuDango Mar 2, 2020
1beb3b0
Update README.md
PantsuDango Mar 2, 2020
1978e9f
Update README.md
PantsuDango Mar 2, 2020
73dde9d
Update README.md
PantsuDango Mar 2, 2020
040aa4b
Update README.md
PantsuDango Mar 2, 2020
5fdaf4c
Update README.md
PantsuDango Mar 2, 2020
03fe3cf
Update README.md
PantsuDango Mar 2, 2020
8dfacde
Update README.md
PantsuDango Mar 2, 2020
543736e
Update README.md
PantsuDango Mar 2, 2020
c3082b1
Update README.md
PantsuDango Mar 2, 2020
edd6e0d
Update README.md
PantsuDango Mar 2, 2020
1b02c02
Update README.md
PantsuDango Mar 2, 2020
d678dd6
Update README.md
PantsuDango Mar 2, 2020
50b273b
Update README.md
PantsuDango Mar 2, 2020
f49900a
commit Ver1.0
PantsuDango May 9, 2020
d8128f6
commit Ver2.0
PantsuDango May 9, 2020
ae2dae0
commit Ver2.1
PantsuDango May 9, 2020
e55e826
commit Ver3.0
PantsuDango May 9, 2020
fe95420
commit Ver3.1
PantsuDango May 9, 2020
a619731
commit Ver3.2
PantsuDango May 9, 2020
80848b1
Ver3.3
PantsuDango May 9, 2020
8ab8b43
commit Ver3.3 image
PantsuDango May 10, 2020
6fe9a0d
Create README.md
PantsuDango May 10, 2020
1f31227
Update README.md
PantsuDango May 10, 2020
809827e
Update README.md
PantsuDango May 10, 2020
464e123
Update README.md
PantsuDango May 10, 2020
43ef845
Update README.md
PantsuDango May 10, 2020
c4724a3
Update README.md
PantsuDango May 10, 2020
678bdc3
Update README.md
PantsuDango May 10, 2020
b321619
Update README.md
PantsuDango May 10, 2020
593e3eb
Update README.md
PantsuDango May 10, 2020
ffeb8a7
Update README.md
PantsuDango May 10, 2020
d787936
Update README.md
PantsuDango May 10, 2020
6fe4b60
Update README.md
PantsuDango May 10, 2020
9dbebc3
Update README.md
PantsuDango May 10, 2020
e83311d
Update README.md
PantsuDango May 10, 2020
d10a206
Update README.md
PantsuDango May 10, 2020
72169b8
Update README.md
PantsuDango May 10, 2020
271d541
Update README.md
PantsuDango May 10, 2020
d5efb0e
Update README.md
PantsuDango May 10, 2020
e8af25e
Update README.md
PantsuDango May 10, 2020
fd21865
Update README.md
PantsuDango May 10, 2020
fc6fcb7
Update README.md
PantsuDango May 10, 2020
4485b65
commit Ver2.0 image
PantsuDango May 10, 2020
15adabb
commit Ver2.1 image
PantsuDango May 10, 2020
2a6743f
commit Ver3.0 image
PantsuDango May 10, 2020
150cfbb
commit Ver3.0
PantsuDango May 10, 2020
0088602
commit Ver3.1
PantsuDango May 10, 2020
3b31e88
commit Ver3.1
PantsuDango May 10, 2020
7677d14
commit Ver3.2 image
PantsuDango May 10, 2020
16ac02e
Ver3.3
PantsuDango May 10, 2020
8d9ca8c
Update README.md
PantsuDango May 10, 2020
db0bab3
Update README.md
PantsuDango May 10, 2020
ec491ff
Update README.md
PantsuDango May 10, 2020
fb61190
Update README.md
PantsuDango May 10, 2020
1f4b8d8
Update README.md
PantsuDango May 10, 2020
ccc0fef
Update README.md
PantsuDango May 10, 2020
3f88a17
Update README.md
PantsuDango May 10, 2020
1e13b36
Update README.md
PantsuDango May 10, 2020
49b6ae4
Update README.md
PantsuDango May 11, 2020
4eb31ff
Add files via upload
PantsuDango May 11, 2020
8c19532
Delete image.png
PantsuDango May 11, 2020
dbaa7da
Update README.md
PantsuDango May 11, 2020
c24c2cd
Update README.md
PantsuDango May 13, 2020
f146b22
Update README.md
PantsuDango May 14, 2020
200d174
Create requirements.txt
PantsuDango May 24, 2020
e1bab48
Update README.md
PantsuDango May 30, 2020
f2670a3
Update README.md
PantsuDango May 30, 2020
eeb51e6
Update README.md
PantsuDango May 30, 2020
75738fe
Update README.md
PantsuDango May 30, 2020
3ea7ecf
Update README.md
PantsuDango May 31, 2020
c25f212
Update README.md
PantsuDango May 31, 2020
efc328d
Update README.md
PantsuDango Jun 8, 2020
17de647
Update README.md
PantsuDango Jun 8, 2020
0a07572
Update README.md
PantsuDango Jul 4, 2020
3558302
Update README.md
PantsuDango Jul 4, 2020
9f9226e
update baidu translator
zxjlm Jul 9, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
505 changes: 505 additions & 0 deletions API.py

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions Bing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import urllib.request
import urllib.parse
from traceback import print_exc
import re


class BingTranslate(object):

def __init__(self):

self.url = "http://api.microsofttranslator.com/v2/ajax.svc/TranslateArray2?"


def translate(self, BingLanguage, content):

data = {}
data['from'] = '"' + BingLanguage + '"'
data['to'] = '"' + 'zh' + '"'
data['texts'] = '["'
data['texts'] += content
data['texts'] += '"]'
data['options'] = "{}"
data['oncomplete'] = 'onComplete_3'
data['onerror'] = 'onError_3'
data['_'] = '1430745999189'

try:
data = urllib.parse.urlencode(data).encode('utf-8')
strUrl = self.url + data.decode() + "&appId=%223DAEE5B978BA031557E739EE1E2A68CB1FAD5909%22"
response = urllib.request.urlopen(strUrl)
str_data = response.read().decode('utf-8')
tmp, str_data = str_data.split('"TranslatedText":')
translate_data = str_data[1:str_data.find('",', 1)].replace('\\"','')

except Exception:
print_exc()
translate_data = "Bing:我抽风啦!"

return translate_data


if __name__ == '__main__':

content = 'そうすると、可笑しいことや変なこと、滑稽なことや正しくないこと、反対にやるべきことが见えてくるから。とにかく、何かにどっぷりはまっていると、周りのことが见えなくなってしまう。だから、时々一歩引くと物事が见えてくる。'
#content = "Hooray! It's snowing! It's time to make a snowman.James runs out. He makes a big pile of snow. He puts a big snowball on top. He adds a scarf and a hat. He adds an orange for the nose. He adds coal for the eyes and buttons.In the evening, James opens the door. What does he see? The snowman is moving! James invites him in. The snowman has never been inside a house. He says hello to the cat. He plays with paper towels.A moment later, the snowman takes James's hand and goes out.They go up, up, up into the air! They are flying! What a wonderful night!The next morning, James jumps out of bed. He runs to the door.He wants to thank the snowman. But he's gone."
#content = "낙성대는 ‘별이 떨어진 곳’ 이라는 뜻이다.고려시대 때 어는 날 하늘에서 가장 크고 빛나는 별 하나가 땅에 떨어졌는데 그 곳에서 명장 강감찬 장군이 태어났다.그 후부터 그 곳을 낙성대라고 불렀다."
# ja en ko
bing = BingTranslate()
print(bing.translate('ja', content))
101 changes: 101 additions & 0 deletions Google.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# -*- coding:utf-8 -*-

import urllib.request
import urllib.parse
from requests import Session
from traceback import print_exc
from js2py import EvalJs


class GoogleTranslate():

def __init__(self):

self.headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'}
self.session = Session()
self.session.keep_alive = False


def getTk(self, text):

with open('.\\config\\GoogleJS.js', encoding='utf8') as f:
js_data = f.read()

context = EvalJs()
context.execute(js_data)
tk = context.TL(text)

return tk


def buildUrl(self, text ,tk):

baseUrl = 'http://translate.google.cn/translate_a/single'
baseUrl += '?client=webapp&'
baseUrl += 'sl=auto&'
baseUrl += 'tl=' + 'zh-CN' + '&'
baseUrl += 'hl=zh-CN&'
baseUrl += 'dt=at&'
baseUrl += 'dt=bd&'
baseUrl += 'dt=ex&'
baseUrl += 'dt=ld&'
baseUrl += 'dt=md&'
baseUrl += 'dt=qca&'
baseUrl += 'dt=rw&'
baseUrl += 'dt=rm&'
baseUrl += 'dt=ss&'
baseUrl += 'dt=t&'
baseUrl += 'ie=UTF-8&'
baseUrl += 'oe=UTF-8&'
baseUrl += 'clearbtn=1&'
baseUrl += 'otf=1&'
baseUrl += 'pc=1&'
baseUrl += 'srcrom=0&'
baseUrl += 'ssel=0&'
baseUrl += 'tsel=0&'
baseUrl += 'kc=2&'
baseUrl += 'tk=' + str(tk) + '&'
content = urllib.parse.quote(text)
baseUrl += 'q=' + content

return baseUrl


def getHtml(self, session, url, headers):

try:
html = session.get(url, headers=headers)
return html.json()
except Exception:
print_exc()
return None


def translate(self, text):

tk = self.getTk(text)
url = self.buildUrl(text, tk)

try:
result = self.getHtml(self.session, url, self.headers)

if result != None:
sentence = ''
for i in result[0]:
if i[0] != None:
sentence += i[0]
else:
sentence = "谷歌:我抽风啦!"

except Exception:
print_exc()
sentence = "谷歌:我抽风啦!"

return sentence


if __name__ == '__main__':

text = "そうすると、可笑しいことや変なこと、滑稽なことや正しくないこと、反対にやるべきことが见えてくるから。とにかく、何かにどっぷりはまっていると、周りのことが见えなくなってしまう。だから、时々一歩引くと物事が见えてくる。"
google = GoogleTranslate()
print(google.translate(text))
Loading