Skip to content

Commit

Permalink
Remove translation from info scraping
Browse files Browse the repository at this point in the history
  • Loading branch information
thedaviddelta committed Jun 14, 2022
1 parent cd00405 commit 8602ce8
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./info";
export * from "./simple";
export * from "./text";
export * from "./audio";
export * from "./utils/language";
export * from "./utils/interfaces";
7 changes: 1 addition & 6 deletions src/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/simple.ts → src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ interface ExtraTranslationsGroup {
}

export interface TranslationInfo {
translation: string,
detectedSource?: LangCode<"source">,
typo?: string,
pronunciation: {
Expand Down
4 changes: 0 additions & 4 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
8 changes: 4 additions & 4 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LangCodeGoogle } from "./language";

export const Endpoint = {
INFO: "info",
SIMPLE: "simple",
TEXT: "text",
AUDIO: "audio"
} as const;

Expand All @@ -14,7 +14,7 @@ type Params = {
[Endpoint.INFO]: {
body: string
},
[Endpoint.SIMPLE]: {
[Endpoint.TEXT]: {
source: LangCodeGoogle<"source">,
target: LangCodeGoogle<"target">,
query: string
Expand Down Expand Up @@ -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}`,
{
Expand Down
20 changes: 9 additions & 11 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/simple.test.ts → tests/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSimpleTranslation, LangCode } from "../src";
import { getTranslationText, LangCode } from "../src";
// @ts-ignore
import { expectFromEntries, expectFromWrong, sampleText } from "./testUtils";

Expand All @@ -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())
));

0 comments on commit 8602ce8

Please sign in to comment.