mirror of
https://github.com/thedaviddelta/lingva-scraper.git
synced 2025-10-06 00:02:43 +02:00
Remove translation from info scraping
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "lingva-scraper",
|
"name": "lingva-scraper",
|
||||||
"version": "0.1.2",
|
"version": "0.1.3",
|
||||||
"description": "Google Translate scraper for Lingva Translate",
|
"description": "Google Translate scraper for Lingva Translate",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
export * from "./info";
|
export * from "./info";
|
||||||
export * from "./simple";
|
export * from "./text";
|
||||||
export * from "./audio";
|
export * from "./audio";
|
||||||
export * from "./utils/language";
|
export * from "./utils/language";
|
||||||
export * from "./utils/interfaces";
|
export * from "./utils/interfaces";
|
||||||
|
@@ -5,7 +5,7 @@ import { Boilerplate, Data } from "./utils/types";
|
|||||||
import { TranslationInfo } from "./utils/interfaces";
|
import { TranslationInfo } from "./utils/interfaces";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the full translation information, including the translated text and, optionally, other relevant data
|
* Retrieves the full translation information given a pair of languages and a query
|
||||||
* @param source - The code of the language to translate from
|
* @param source - The code of the language to translate from
|
||||||
* @param target - The code of the language to translate to
|
* @param target - The code of the language to translate to
|
||||||
* @param query - The text to be translated
|
* @param query - The text to be translated
|
||||||
@@ -31,12 +31,7 @@ export const getTranslationInfo = async (
|
|||||||
if (!resData)
|
if (!resData)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const translation = parse.translation(resData);
|
|
||||||
if (!translation)
|
|
||||||
return;
|
|
||||||
|
|
||||||
return parse.undefinedFields({
|
return parse.undefinedFields({
|
||||||
translation,
|
|
||||||
detectedSource: source === "auto"
|
detectedSource: source === "auto"
|
||||||
? parse.detected(resData)
|
? parse.detected(resData)
|
||||||
: undefined,
|
: undefined,
|
||||||
|
@@ -3,13 +3,13 @@ import { mapGoogleCode, LangCode } from "./utils/language";
|
|||||||
import request, { Endpoint } from "./utils/request";
|
import request, { Endpoint } from "./utils/request";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the translation text using a legacy implementation
|
* Retrieves the translation given a pair of languages and a query
|
||||||
* @param source - The code of the language to translate from
|
* @param source - The code of the language to translate from
|
||||||
* @param target - The code of the language to translate to
|
* @param target - The code of the language to translate to
|
||||||
* @param query - The text to be translated
|
* @param query - The text to be translated
|
||||||
* @returns A single {@link string} with the translated text
|
* @returns A single {@link string} with the translated text
|
||||||
*/
|
*/
|
||||||
export const getSimpleTranslation = async (
|
export const getTranslationText = async (
|
||||||
source: LangCode<"source">,
|
source: LangCode<"source">,
|
||||||
target: LangCode<"target">,
|
target: LangCode<"target">,
|
||||||
query: string
|
query: string
|
||||||
@@ -22,7 +22,7 @@ export const getSimpleTranslation = async (
|
|||||||
if (encodedQuery.length > 7500)
|
if (encodedQuery.length > 7500)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return request(Endpoint.SIMPLE)
|
return request(Endpoint.TEXT)
|
||||||
.with({ source: parsedSource, target: parsedTarget, query: encodedQuery })
|
.with({ source: parsedSource, target: parsedTarget, query: encodedQuery })
|
||||||
.doing(({ data }) => {
|
.doing(({ data }) => {
|
||||||
if (!data)
|
if (!data)
|
@@ -21,7 +21,6 @@ interface ExtraTranslationsGroup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface TranslationInfo {
|
export interface TranslationInfo {
|
||||||
translation: string,
|
|
||||||
detectedSource?: LangCode<"source">,
|
detectedSource?: LangCode<"source">,
|
||||||
typo?: string,
|
typo?: string,
|
||||||
pronunciation: {
|
pronunciation: {
|
||||||
|
@@ -7,10 +7,6 @@ export const detected = ([source, target, detected, extra]: Data): TranslationIn
|
|||||||
return code ? mapLingvaCode<"source">(code) : undefined;
|
return code ? mapLingvaCode<"source">(code) : undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const translation = ([, target]: Data): TranslationInfo["translation"] | undefined => (
|
|
||||||
target?.[0]?.[0]?.[5]?.[0]?.[0] ?? target?.[0]?.[0]?.[5]?.[0]?.[4]?.[0]?.[0]
|
|
||||||
);
|
|
||||||
|
|
||||||
export const typo = ([source]: Data): TranslationInfo["typo"] => (
|
export const typo = ([source]: Data): TranslationInfo["typo"] => (
|
||||||
source?.[1]?.[0]?.[4] ?? undefined
|
source?.[1]?.[0]?.[4] ?? undefined
|
||||||
);
|
);
|
||||||
|
@@ -4,7 +4,7 @@ import { LangCodeGoogle } from "./language";
|
|||||||
|
|
||||||
export const Endpoint = {
|
export const Endpoint = {
|
||||||
INFO: "info",
|
INFO: "info",
|
||||||
SIMPLE: "simple",
|
TEXT: "text",
|
||||||
AUDIO: "audio"
|
AUDIO: "audio"
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ type Params = {
|
|||||||
[Endpoint.INFO]: {
|
[Endpoint.INFO]: {
|
||||||
body: string
|
body: string
|
||||||
},
|
},
|
||||||
[Endpoint.SIMPLE]: {
|
[Endpoint.TEXT]: {
|
||||||
source: LangCodeGoogle<"source">,
|
source: LangCodeGoogle<"source">,
|
||||||
target: LangCodeGoogle<"target">,
|
target: LangCodeGoogle<"target">,
|
||||||
query: string
|
query: string
|
||||||
@@ -71,8 +71,8 @@ const retrieve = <T extends EndpointType>(endpoint: T, params: Params[T]) => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (endpoint === Endpoint.SIMPLE) {
|
if (endpoint === Endpoint.TEXT) {
|
||||||
const { source, target, query } = params as Params[typeof Endpoint.SIMPLE];
|
const { source, target, query } = params as Params[typeof Endpoint.TEXT];
|
||||||
return axios.get(
|
return axios.get(
|
||||||
`https://translate.google.com/m?sl=${source}&tl=${target}&q=${query}`,
|
`https://translate.google.com/m?sl=${source}&tl=${target}&q=${query}`,
|
||||||
{
|
{
|
||||||
|
@@ -87,17 +87,15 @@ type DataTarget = [ // target
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
[
|
[
|
||||||
[
|
string, // text
|
||||||
string, // text
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
[ // more
|
||||||
[ // more
|
string,
|
||||||
string,
|
number[]
|
||||||
number[]
|
][]
|
||||||
][]
|
][]
|
||||||
]
|
|
||||||
]
|
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
LangCodeGoogle<"target">, // lang
|
LangCodeGoogle<"target">, // lang
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { getSimpleTranslation, LangCode } from "../src";
|
import { getTranslationText, LangCode } from "../src";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { expectFromEntries, expectFromWrong, sampleText } from "./testUtils";
|
import { expectFromEntries, expectFromWrong, sampleText } from "./testUtils";
|
||||||
|
|
||||||
@@ -15,16 +15,16 @@ const entries: Entry[] = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
it("returns translated text correctly", () => (
|
it("returns translated text correctly", () => (
|
||||||
expectFromEntries(entries, getSimpleTranslation, trans => {
|
expectFromEntries(entries, getTranslationText, trans => {
|
||||||
expect(trans).not.toBeNull();
|
expect(trans).not.toBeNull();
|
||||||
})
|
})
|
||||||
));
|
));
|
||||||
|
|
||||||
it("returns null on wrong params", () => (
|
it("returns null on wrong params", () => (
|
||||||
expectFromWrong(getSimpleTranslation)
|
expectFromWrong(getTranslationText)
|
||||||
));
|
));
|
||||||
|
|
||||||
it("returns null on huge text", () => (
|
it("returns null on huge text", () => (
|
||||||
getSimpleTranslation("ca", "es", sampleText.huge)
|
getTranslationText("ca", "es", sampleText.huge)
|
||||||
.then(trans => expect(trans).toBeNull())
|
.then(trans => expect(trans).toBeNull())
|
||||||
));
|
));
|
Reference in New Issue
Block a user