From ca7e1ab73e94046ec6a2498cc0aa34368a5ff442 Mon Sep 17 00:00:00 2001 From: Davi Nomoeh Dani Date: Fri, 24 Feb 2023 02:49:19 +0700 Subject: [PATCH] fix vqd (#21) * fix wrong regex pattern * try to collect all posibility sites instead of first site only * update package version v2.4.5 --- .eslintrc.json | 4 ++-- package.json | 2 +- src/lib/duckduckgo.ts | 24 ++++++++++-------------- src/lib/songlyrics.ts | 42 ++++++++++++++++++++++++++---------------- yarn.lock | 17 +++++++++++++++++ 5 files changed, 56 insertions(+), 33 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 4b17f5c..be6c304 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -28,7 +28,7 @@ "linebreak-style": ["error", "unix"], "quotes": ["error", "single"], "eqeqeq": ["error", "always"], - "no-await-in-loop": "error", + "no-await-in-loop": "off", "array-callback-return": "error", "no-compare-neg-zero": "error", "no-dupe-else-if": "error", @@ -45,7 +45,7 @@ "no-undef": "off", "accessor-pairs": "error", "arrow-body-style": "error", - "complexity": ["error", 4], + "complexity": ["error", 7], "consistent-return": "off", "consistent-this": ["error", "self"], "comma-dangle": ["error", "always-multiline"], diff --git a/package.json b/package.json index 2000795..54f67d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "songlyrics", - "version": "2.4.4", + "version": "2.4.5", "description": "Find a lyrics just enter the song title", "keywords": [ "lyrics", diff --git a/src/lib/duckduckgo.ts b/src/lib/duckduckgo.ts index fa270aa..e7b5db3 100644 --- a/src/lib/duckduckgo.ts +++ b/src/lib/duckduckgo.ts @@ -1,6 +1,6 @@ import { makeRequest } from './request' -const VQD_REGEX = /vqd='(\d+-\d+-\d+)'/ +const VQD_REGEX = /vqd='([\d-]+)';/ const SEARCH_REGEX = /DDG\.pageLayout\.load\('d',(\[.+\])\);DDG\.duckbar\.load\('images'/ @@ -8,18 +8,14 @@ const queryString = (query: Record) => new URLSearchParams(query).toString() const getVQD = async (query: string) => { - try { - const vqdRequestUrl = new URL( - `https://duckduckgo.com/?${queryString({ - q: query, - ia: 'web', - })}`, - ) - const html = await makeRequest(vqdRequestUrl) - return VQD_REGEX.exec(html)?.at(1) - } catch (err) { - throw new Error(`Failed to get the VQD for query "${query}".`) - } + const vqdRequestUrl = new URL( + `https://duckduckgo.com/?${queryString({ + q: query, + ia: 'web', + })}`, + ) + const html = await makeRequest(vqdRequestUrl) + return VQD_REGEX.exec(html)?.at(1) } // eslint-disable-next-line complexity @@ -55,7 +51,7 @@ export const webSearch = async ( if (/DDG.deep.is506/.test(responseString)) { throw new Error('A server error occurred!') } - const raw = SEARCH_REGEX.exec(responseString)?.at(1)?.replace(/\t/g, ' ') + const raw = SEARCH_REGEX.exec(responseString)?.at(1)?.replace(/\\t/g, ' ') if (!raw) { throw new Error('No results found!') } diff --git a/src/lib/songlyrics.ts b/src/lib/songlyrics.ts index 355bd41..d55beaa 100644 --- a/src/lib/songlyrics.ts +++ b/src/lib/songlyrics.ts @@ -20,23 +20,33 @@ export const songlyrics = async ( const clean = cleanTitle(title).toLowerCase() const query = `${clean} inurl:lyrics` const ddgResults = await webSearch(query) - const ddgResult = ddgResults?.shift() - const sourceName = ddgResult?.i.replace(/(www.|.com)/g, '').toLowerCase() - const sources = makeSources() - if (ddgResult && sourceName && sources.has(sourceName)) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const source = sources.get(sourceName)! - const html = await makeRequest(new URL(ddgResult.c)) - const lyrics = await source.parse(htmlParser(html)) - return { - title: ddgResult.t.replace(/\|.+/g, '').trim(), - lyrics, - source: { - name: source.name, - url: ddgResult.i, - link: ddgResult.c, - }, + const possibleSites = + ddgResults + ?.map(r => { + r.i = r.i?.replace(/(www\.)?(.*)\.\w+$/g, '$2').toLowerCase() + return r + }) + .filter(r => [...sources.keys()].includes(r.i)) || [] + for (const site of possibleSites) { + const siteName = site?.i + if (siteName) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const source = sources.get(siteName)! + const html = await makeRequest(new URL(site.c)) + const lyrics = await source.parse(htmlParser(html)) + if (!lyrics) { + continue + } + return { + title: site.t.replace(/\|.+/g, '').trim(), + lyrics, + source: { + name: source.name, + url: site.i, + link: site.c, + }, + } } } return diff --git a/yarn.lock b/yarn.lock index ab3ce8a..092023e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -52,11 +52,23 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" +"@types/follow-redirects@^1.14.1": + version "1.14.1" + resolved "https://registry.yarnpkg.com/@types/follow-redirects/-/follow-redirects-1.14.1.tgz#c08b173be7517ddc53725d0faf9648d4dc7a9cdb" + integrity sha512-THBEFwqsLuU/K62B5JRwab9NW97cFmL4Iy34NTMX0bMycQVzq2q7PKOkhfivIwxdpa/J72RppgC42vCHfwKJ0Q== + dependencies: + "@types/node" "*" + "@types/json-schema@^7.0.9": version "7.0.11" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3" integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ== +"@types/node@*": + version "18.14.1" + resolved "https://registry.yarnpkg.com/@types/node/-/node-18.14.1.tgz#90dad8476f1e42797c49d6f8b69aaf9f876fc69f" + integrity sha512-QH+37Qds3E0eDlReeboBxfHbX9omAcBCXEzswCu6jySP642jiM3cYSIkU/REqwhCUqXdonHFuBfJDiAJxMNhaQ== + "@types/node@^16.10.2": version "16.11.36" resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.36.tgz#9ab9f8276987132ed2b225cace2218ba794fc751" @@ -687,6 +699,11 @@ flatted@^3.1.0: resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.5.tgz#76c8584f4fc843db64702a6bd04ab7a8bd666da3" integrity sha512-WIWGi2L3DyTUvUrwRKgGi9TwxQMUEqPOPQBVi71R96jZXJdFskXEmf54BoZaS1kknGODoIGASGEzBUYdyMCBJg== +follow-redirects@^1.15.1: + version "1.15.2" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13" + integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA== + fs.realpath@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"