Page Menu
Home
desp's stash
Search
Configure Global Search
Log In
Files
F369258
Translate.java
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Subscribers
None
Translate.java
View Options
package
me.despawningbone.discordbot.command.info
;
import
java.io.IOException
;
import
java.time.OffsetDateTime
;
import
java.util.Arrays
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.ThreadLocalRandom
;
import
java.util.concurrent.TimeUnit
;
import
org.jsoup.Jsoup
;
import
org.apache.commons.lang3.exception.ExceptionUtils
;
import
org.json.JSONObject
;
import
org.json.JSONTokener
;
import
org.jsoup.Connection
;
import
org.jsoup.Connection.Method
;
import
org.jsoup.Connection.Response
;
import
org.jsoup.HttpStatusException
;
import
com.google.common.util.concurrent.ThreadFactoryBuilder
;
import
me.despawningbone.discordbot.command.Command
;
import
me.despawningbone.discordbot.command.CommandResult
;
import
me.despawningbone.discordbot.command.CommandResult.CommandResultType
;
import
net.dv8tion.jda.api.EmbedBuilder
;
import
net.dv8tion.jda.api.entities.Message
;
import
net.dv8tion.jda.api.entities.TextChannel
;
import
net.dv8tion.jda.api.entities.User
;
public
class
Translate
extends
Command
{
public
Translate
()
{
this
.
desc
=
"Translate some sentences!"
;
this
.
usage
=
"[-fromlang] <sentence> [-tolang]"
;
this
.
remarks
=
Arrays
.
asList
(
"Supported Languages: `en, ja, zh, de, es, fr, it, ru, pl, pt, nl`"
);
this
.
examples
=
Arrays
.
asList
(
"-ja あなたも文化人だと思います"
,
"-es despacito"
);
this
.
alias
=
Arrays
.
asList
(
"tl"
);
executor
.
scheduleAtFixedRate
(()
->
refresh
(),
0
,
300
,
TimeUnit
.
SECONDS
);
//5 mins refresh
}
private
int
id
=
(
ThreadLocalRandom
.
current
().
nextInt
(
1000
,
10000
)
+
1
)
*
10000
+
1
;
private
String
cookie
=
null
,
agent
=
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0"
;
private
JSONObject
cVars
=
null
;
final
ScheduledExecutorService
executor
=
Executors
.
newSingleThreadScheduledExecutor
(
new
ThreadFactoryBuilder
().
setDaemon
(
true
).
setNameFormat
(
"tl-scheduler-%d"
).
build
());
@Override
public
CommandResult
execute
(
TextChannel
channel
,
User
author
,
Message
msg
,
String
[]
args
)
{
if
(
args
.
length
<
0
)
return
new
CommandResult
(
CommandResultType
.
INVALIDARGS
,
"Please enter something to translate!"
);
try
{
String
langF
=
"auto"
,
langT
=
"EN"
,
query
=
String
.
join
(
" "
,
args
).
replaceAll
(
"\n"
,
" "
).
replace
(
"-"
,
""
);
//since it hates new lines and hyphens apparently
if
(
Arrays
.
asList
(
"-en"
,
"-ja"
,
"-zh"
,
"-de"
,
"-es"
,
"-fr"
,
"-it"
,
"-ru"
,
"-pl"
,
"-pt"
,
"-nl"
).
contains
(
args
[
0
].
toLowerCase
()))
{
langF
=
args
[
0
].
substring
(
1
).
toUpperCase
();
query
=
String
.
join
(
" "
,
Arrays
.
copyOfRange
(
args
,
1
,
args
.
length
));
}
if
(
Arrays
.
asList
(
"-en"
,
"-ja"
,
"-zh"
,
"-de"
,
"-es"
,
"-fr"
,
"-it"
,
"-ru"
,
"-pl"
,
"-pt"
,
"-nl"
).
contains
(
args
[
args
.
length
-
1
].
toLowerCase
()))
{
langT
=
args
[
args
.
length
-
1
].
substring
(
1
).
toUpperCase
();
query
=
query
.
substring
(
0
,
query
.
length
()
-
3
);
}
if
((
args
.
length
<
5
?
query
.
length
()
>
40
:
args
.
length
>
20
))
return
new
CommandResult
(
CommandResultType
.
INVALIDARGS
,
"Please type a shorter phrase to translate!"
);
//need OPTION call before calling this?
//req
Connection
con
=
Jsoup
.
connect
(
"https://www2.deepl.com/jsonrpc"
).
userAgent
(
agent
)
.
header
(
"Content-type"
,
"application/json"
).
ignoreContentType
(
true
)
.
requestBody
(
"{\"jsonrpc\":\"2.0\",\"method\": \"LMT_handle_jobs\",\"params\":{\"jobs\":[{\"kind\":\"default\",\"raw_en_sentence\":\""
+
query
+
"\",\"raw_en_context_before\":[],\"raw_en_context_after\":[],\"preferred_num_beams\":4"
+
((
args
.
length
<
2
?
query
.
length
()
>
5
:
args
.
length
>
5
)
?
""
:
",\"quality\":\"fast\""
)
+
"}],\"commonJobParams\":{}"
+
",\"lang\":{\"user_preferred_langs\":[\"EN\"],\"source_lang_user_selected\":\""
+
langF
+
"\",\"target_lang\":\""
+
langT
+
"\"},\"priority\":1,\"timestamp\":"
+
OffsetDateTime
.
now
().
toEpochSecond
()
+
"000},\"id\":"
+
id
++
+
"}"
);
if
(
cookie
!=
null
)
con
.
cookie
(
"LMTBID"
,
cookie
);
Response
resp
=
con
.
method
(
Method
.
POST
).
execute
();
if
(
resp
.
hasCookie
(
"LMTBID"
))
cookie
=
resp
.
cookie
(
"LMTBID"
);
//set cookies; only LMTBID is useful, and its set on first call to jsonrpc
//formatting
JSONObject
main
=
new
JSONObject
(
new
JSONTokener
(
resp
.
body
())).
getJSONObject
(
"result"
);
EmbedBuilder
eb
=
new
EmbedBuilder
();
eb
.
setColor
(
0x0f2b46
);
eb
.
setTitle
(
"Translation: "
+
(
langF
.
equals
(
"auto"
)
?
main
.
getString
(
"source_lang"
)
+
"(detected)"
:
langF
)
+
" -> "
+
main
.
getString
(
"target_lang"
));
eb
.
addField
(
"Original"
,
query
,
true
);
String
tl
=
""
;
for
(
Object
obj
:
main
.
getJSONArray
(
"translations"
).
getJSONObject
(
0
).
getJSONArray
(
"beams"
))
{
String
p
=
((
JSONObject
)
obj
).
getString
(
"postprocessed_sentence"
);
if
(
tl
.
isEmpty
())
tl
=
p
+
"\n"
;
else
tl
+=
"*"
+
p
+
"*\n"
;
}
eb
.
addField
(
"Translated"
,
tl
,
true
);
eb
.
setFooter
(
"Translated by DeepL"
,
"https://egress.storeden.net/gallery/5a2577c9ffe48e7e4b418464"
);
eb
.
setTimestamp
(
OffsetDateTime
.
now
());
channel
.
sendMessage
(
eb
.
build
()).
queue
();
return
new
CommandResult
(
CommandResultType
.
SUCCESS
);
}
catch
(
IOException
e
)
{
if
(
e
instanceof
HttpStatusException
)
//aka pending code fix
return
new
CommandResult
(
CommandResultType
.
FAILURE
,
"The translator is currently temporarily unavailable, please try again later."
);
return
new
CommandResult
(
CommandResultType
.
ERROR
,
ExceptionUtils
.
getStackTrace
(
e
));
}
}
private
void
refresh
()
{
try
{
Connection
con
=
Jsoup
.
connect
(
"https://www.deepl.com/PHP/backend/clientState.php?request_type=jsonrpc&il=EN"
).
userAgent
(
agent
)
.
requestBody
(
"{\"jsonrpc\":\"2.0\",\"method\":\"getClientState\",\"params\":{\"v\":\"20180814\""
+
(
cVars
==
null
?
""
:
",\"clientVars\":"
+
cVars
.
toString
())
+
"},\"id\":"
+
id
++
+
"}"
);
if
(
cookie
!=
null
)
con
.
cookie
(
"LMTBID"
,
cookie
);
JSONObject
json
=
new
JSONObject
(
new
JSONTokener
(
con
.
ignoreContentType
(
true
).
post
().
text
()));
//System.out.println(json);
if
(
cVars
==
null
)
cVars
=
json
.
getJSONObject
(
"result"
).
getJSONObject
(
"clientVars"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Mon, Jul 7, 5:11 AM (1 d, 10 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
ab/23/1af8b20a7d68aabc4cfdb975501d
Attached To
rDESB despbot
Event Timeline
Log In to Comment