-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9688435
commit f08920e
Showing
12 changed files
with
454 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
"use server"; | ||
import Base64 from "crypto-js/enc-base64"; | ||
import HmacMD5 from "crypto-js/hmac-md5"; | ||
|
||
const url = "https://papago.naver.com/apis/n2mt/translate"; | ||
|
||
export const papagoTranslateAction = async ( | ||
text: string, | ||
locale = "zh-CN", | ||
): Promise<TranslateResult> => { | ||
const deviceId = uuid(); | ||
const body = { | ||
deviceId, | ||
locale, | ||
dict: true, | ||
dictDisplay: 30, | ||
honorific: false, | ||
instant: false, | ||
paging: false, | ||
source: "ko", | ||
target: locale, | ||
text, | ||
usageAgreed: false, | ||
}; | ||
const headers = { | ||
Accept: "application/json", | ||
"Accept-Language": "zh-CN", | ||
"device-type": "pc", | ||
"x-apigw-partnerid": "papago", | ||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", | ||
"User-Agent": | ||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36", | ||
Referer: "https://papago.naver.com/", | ||
Origin: "https://papago.naver.com", | ||
...authorization(url, deviceId), | ||
}; | ||
|
||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: headers, | ||
// @ts-ignore | ||
body: new URLSearchParams(body), | ||
mode: "no-cors", | ||
}); | ||
if (response.status !== 200) { | ||
throw new Error((await response.text()) || response.statusText); | ||
} | ||
return await response.json(); | ||
}; | ||
|
||
function uuid() { | ||
let e = new Date().getTime(); | ||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (a) => { | ||
const t = ((e + 16 * Math.random()) % 16) | 0; | ||
// biome-ignore lint/style/noCommaOperator: <explanation> | ||
return (e = Math.floor(e / 16)), ("x" === a ? t : (3 & t) | 8).toString(16); | ||
}); | ||
} | ||
|
||
function authorization(url: string, uuid: string) { | ||
const t = uuid; | ||
const n = new Date().getTime(); | ||
return { | ||
Authorization: `PPG ${t}:${HmacMD5( | ||
`${t}\n${url.split("?")[0]}\n${n}`, | ||
"v1.8.7_0c4b33bdb0", | ||
).toString(Base64)}`, | ||
Timestamp: n.toString(), | ||
}; | ||
} | ||
|
||
interface Meaning { | ||
meaning: string; | ||
examples: string[]; | ||
originalMeaning: string; | ||
} | ||
|
||
interface Po { | ||
type: string; | ||
meanings: Meaning[]; | ||
} | ||
|
||
interface Item { | ||
entry: string; | ||
subEntry?: string | null; | ||
matchType: string; | ||
hanjaEntry: string | null; | ||
phoneticSigns: PhoneticSign[]; | ||
pos: Po[]; | ||
source: string; | ||
url: string; | ||
mUrl: string; | ||
expDicTypeForm: string; | ||
locale: string; | ||
gdid: string; | ||
expEntrySuperscript: string; | ||
} | ||
|
||
interface Dict { | ||
items?: Item[]; | ||
examples: string[]; | ||
isWordType: boolean; | ||
} | ||
|
||
interface PhoneticSign { | ||
type: null; | ||
sign: string; | ||
} | ||
|
||
interface Meaning { | ||
meaning: string; | ||
examples: string[]; | ||
originalMeaning: string; | ||
} | ||
|
||
interface Po { | ||
type: string; | ||
meanings: Meaning[]; | ||
} | ||
|
||
interface Item { | ||
entry: string; | ||
subEntry?: string | null; | ||
matchType: string; | ||
hanjaEntry: string | null; | ||
phoneticSigns: PhoneticSign[]; | ||
pos: Po[]; | ||
source: string; | ||
url: string; | ||
mUrl: string; | ||
expDicTypeForm: string; | ||
locale: string; | ||
gdid: string; | ||
expEntrySuperscript: string; | ||
} | ||
|
||
interface TarDict { | ||
items: Item[]; | ||
examples: string[]; | ||
isWordType: boolean; | ||
} | ||
|
||
interface TlitResult { | ||
token: string; | ||
phoneme: string; | ||
} | ||
|
||
interface Message { | ||
tlitResult: TlitResult[]; | ||
} | ||
|
||
interface TlitSrc { | ||
message: Message; | ||
} | ||
|
||
interface TlitResult { | ||
token: string; | ||
phoneme: string; | ||
} | ||
|
||
interface Message { | ||
tlitResult: TlitResult[]; | ||
} | ||
|
||
interface Tlit { | ||
message: Message; | ||
} | ||
|
||
interface Nbest { | ||
lang: string; | ||
prob: number; | ||
} | ||
|
||
interface LangDetection { | ||
nbests: Nbest[]; | ||
} | ||
|
||
export interface TranslateResult { | ||
dict: Dict; | ||
tarDict: TarDict; | ||
delay: number; | ||
delaySmt: number; | ||
srcLangType: string; | ||
tarLangType: string; | ||
translatedText: string; | ||
engineType: string; | ||
tlitSrc: TlitSrc; | ||
tlit: Tlit; | ||
langDetection: LangDetection; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.