Skip to content

Commit

Permalink
add new sogou mt plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
xulihang committed Dec 18, 2018
1 parent 4401677 commit e2cd9dc
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
46 changes: 46 additions & 0 deletions plugins/sogouDeepIMT/sogouDeepIMT.b4j
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
AppType=JavaFX
Build1=Default,org.xulihang.basiccat
Group=Default Group
Library1=jcore
Library10=okhttp
Library11=encryption
Library12=byteconverter
Library2=jfx
Library3=json
Library4=jxmlsax
Library5=xmlbuilder
Library6=jxui
Library7=javaobject
Library8=jokhttputils2
Library9=jstringutils
Module1=sogouDeepIMTPlugin
NumberOfFiles=0
NumberOfLibraries=12
NumberOfModules=1
Version=6.8
@EndOfDesignText@
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region

Sub Process_Globals
Private fx As JFX
Public MainForm As Form

End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show
'Dim n As sogouDeepIMTPlugin
'n.Initialize
'wait for (n.translate("Hello","en","zh",Null)) complete (result As String)
'Log(result)
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
8 changes: 8 additions & 0 deletions plugins/sogouDeepIMT/sogouDeepIMT.b4j.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ModuleBookmarks0=
ModuleBookmarks1=
ModuleBreakpoints0=
ModuleBreakpoints1=
ModuleClosedNodes0=
ModuleClosedNodes1=
SelectedBuild=0
VisibleModules=1
95 changes: 95 additions & 0 deletions plugins/sogouDeepIMT/sogouDeepIMTPlugin.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=4.2
@EndOfDesignText@
Sub Class_Globals
Private fx As JFX
Private Bconv As ByteConverter
End Sub

'Initializes the object. You can NOT add parameters to this method!
Public Sub Initialize() As String
Log("Initializing plugin " & GetNiceName)
' Here return a key to prevent running unauthorized plugins
Return "MyKey"
End Sub

' must be available
public Sub GetNiceName() As String
Return "sogouDeepIMT"
End Sub

' must be available
public Sub Run(Tag As String, Params As Map) As ResumableSub
Log("run"&Params)
Select Tag
Case "getParams"
Dim paramsList As List
paramsList.Initialize
paramsList.Add("pid")
paramsList.Add("key")
Return paramsList
Case "translate"
wait for (translate(Params.Get("source"),Params.Get("sourceLang"),Params.Get("targetLang"),Params.Get("preferencesMap"))) complete (result As String)
Return result
End Select
Return ""
End Sub


Sub translate(source As String, sourceLang As String, targetLang As String,preferencesMap As Map) As ResumableSub
Dim target As String
Dim pid,key As String
pid=getMap("sogouDeepI",getMap("mt",preferencesMap)).Get("pid")
key=getMap("sogouDeepI",getMap("mt",preferencesMap)).Get("key")
Dim su As StringUtils

If sourceLang="zh" Then
sourceLang="zh-CHS"
End If
If targetLang="zh" Then
targetLang="zh-CHS"
End If

Dim params As String

Dim salt As String
salt=Rnd(1,1000)
Dim sign As String
sign=pid.Trim&source.Trim&salt.trim&key.Trim
Dim md As MessageDigest
sign=Bconv.HexFromBytes(md.GetMessageDigest(Bconv.StringToBytes(sign,"UTF-8"),"MD5"))
sign=sign.ToLowerCase

source=su.EncodeUrl(source,"UTF-8")

params="from="&sourceLang&"&q="&source&"&to="&targetLang&"&pid="&pid&"&salt="&salt&"&sign="&sign

Dim job As HttpJob
job.Initialize("job",Me)
job.PostString("http://fanyi.sogou.com/reventondc/api/sogouTranslate",params)
job.GetRequest.SetContentType("application/x-www-form-urlencoded")
job.GetRequest.SetHeader("Accept","application/json")

wait For (job) JobDone(job As HttpJob)
If job.Success Then
Log(job.GetString)
Try
Dim json As JSONParser
json.Initialize(job.GetString)
target=json.NextObject.Get("translation")
Catch
Log(LastException)
End Try
Else
target=""
End If
job.Release
Return target
End Sub

Sub getMap(key As String,parentmap As Map) As Map
Return parentmap.Get(key)
End Sub

0 comments on commit e2cd9dc

Please sign in to comment.