Page Menu
Home
desp's stash
Search
Configure Global Search
Log In
Files
F607994
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.List
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.ThreadLocalRandom
;
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.channel.concrete.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: `"
+
String
.
join
(
", "
,
this
.
supportedLangs
)
+
"`"
);
this
.
examples
=
Arrays
.
asList
(
"-ja あなたも文化人だと思います"
,
"-es despacito"
);
this
.
alias
=
Arrays
.
asList
(
"tl"
);
}
//TODO could make this dynamically fetched, but need to parse from net
private
List
<
String
>
supportedLangs
=
Arrays
.
asList
(
"ar"
,
"bg"
,
"zh"
,
"cs"
,
"da"
,
"nl"
,
"en"
,
"et"
,
"fi"
,
"fr"
,
"de"
,
"el"
,
"hu"
,
"id"
,
"it"
,
"ja"
,
"ko"
,
"lv"
,
"lt"
,
"nb"
,
"pl"
,
"pt"
,
"ro"
,
"ru"
,
"sk"
,
"sl"
,
"es"
,
"sv"
,
"tr"
,
"uk"
);
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"
;
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
(
supportedLangs
.
contains
(
args
[
0
].
substring
(
1
).
toLowerCase
()))
{
langF
=
args
[
0
].
substring
(
1
).
toUpperCase
();
query
=
String
.
join
(
" "
,
Arrays
.
copyOfRange
(
args
,
1
,
args
.
length
));
}
if
(
supportedLangs
.
contains
(
args
[
args
.
length
-
1
].
substring
(
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?method=LMT_handle_jobs"
).
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
.
sendMessageEmbeds
(
eb
.
build
()).
queue
();
return
new
CommandResult
(
CommandResultType
.
SUCCESS
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
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
));
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Fri, Apr 3, 3:49 AM (1 d, 12 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
be/ef/b5182c60ad4786b35d97bda2b1a3
Attached To
rDESB despbot
Event Timeline
Log In to Comment