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