Skip to content

Commit

Permalink
fix vqd (#21)
Browse files Browse the repository at this point in the history
* fix wrong regex pattern

* try to collect all posibility sites instead of first site only

* update package version v2.4.5
  • Loading branch information
mr687 authored Feb 23, 2023
1 parent 1e88d51 commit ca7e1ab
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
24 changes: 10 additions & 14 deletions src/lib/duckduckgo.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
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'/

const queryString = (query: Record<string, string>) =>
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
Expand Down Expand Up @@ -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!')
}
Expand Down
42 changes: 26 additions & 16 deletions src/lib/songlyrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit ca7e1ab

Please sign in to comment.