From 7ffb1a20fba8608693aa6a53c25593d6b57bf6ff Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 13 Sep 2023 10:49:38 +0700 Subject: [PATCH 1/2] feat: replace `googlethis` with `google-sr` --- dist/rank.mjs | 10 ++- dist/rank.mjs.map | 2 +- package.json | 2 +- src/rank.mts | 9 +- yarn.lock | 213 +++++++++++++++++++++++++--------------------- 5 files changed, 130 insertions(+), 106 deletions(-) diff --git a/dist/rank.mjs b/dist/rank.mjs index b57f7de5..d7ceacb7 100644 --- a/dist/rank.mjs +++ b/dist/rank.mjs @@ -1,4 +1,4 @@ -import google from "googlethis"; +import { search, ResultTypes } from "google-sr"; /** * Retrieves a list of websites from Google search results based on the provided keyword. * @param keyword - The keyword to search for. @@ -6,11 +6,13 @@ import google from "googlethis"; * @returns A promise that resolves to an array of website URLs. */ async function listWebsites(keyword, page) { - const res = await google.search(keyword, { page, parse_ads: false }); + const res = await search({ query: keyword, page: page }); const websites = []; let prevWebsite = ""; - for (const result of res.results) { - const website = new URL(result.url).hostname; + for (const result of res) { + if (result.type != ResultTypes.SearchResult) + continue; + const website = new URL(result.link).hostname; if (website !== prevWebsite) websites.push(website); prevWebsite = website; diff --git a/dist/rank.mjs.map b/dist/rank.mjs.map index 0df3aca7..7a21f6b2 100644 --- a/dist/rank.mjs.map +++ b/dist/rank.mjs.map @@ -1 +1 @@ -{"version":3,"file":"rank.mjs","sourceRoot":"","sources":["../src/rank.mts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,YAAY,CAAC;AAEhC;;;;;GAKG;AACH,KAAK,UAAU,YAAY,CAAC,OAAe,EAAE,IAAY;IACvD,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;IACrE,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,IAAI,WAAW,GAAG,EAAE,CAAC;IACrB,KAAK,MAAM,M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C;IACrB,KAAK,MAAM,MAAM,IAAI,GAAG,EAAE;QACxB,IAAI,MAAM,CAAC,IAAI,IAAI,WAAW,CAAC,YAAY;YAAE,SAAS;QACtD,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC;QAC9C,IAAI,OAAO,KAAK,WAAW;YAAE,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACpD,WAAW,GAAG,OAAO,CAAC;KACvB;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAaD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAAe,EACf,OAAe,EACf,IAA2B;IAE3B,MAAM,OAAO,GAAG,IAAI,EAAE,OAAO,IAAI,CAAC,CAAC;IACnC,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,OAAO,EAAE,EAAE,IAAI,EAAE;QACzC,MAAM,QAAQ,GAAG,MAAM,YAAY,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QACnD,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,QAAQ,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE;YACjD,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;gBACpC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;aACvB;SACF;KACF;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"} \ No newline at end of file diff --git a/package.json b/package.json index 0432239e..7f179c06 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "dependencies": { "chalk": "^4.1.2", "commander": "^11.0.0", - "googlethis": "^1.7.1", + "google-sr": "^3.2.1", "ora": "^7.0.1" }, "devDependencies": { diff --git a/src/rank.mts b/src/rank.mts index e0948dcd..33dc8d90 100644 --- a/src/rank.mts +++ b/src/rank.mts @@ -1,4 +1,4 @@ -import google from "googlethis"; +import { search, ResultTypes } from "google-sr"; /** * Retrieves a list of websites from Google search results based on the provided keyword. @@ -7,11 +7,12 @@ import google from "googlethis"; * @returns A promise that resolves to an array of website URLs. */ async function listWebsites(keyword: string, page: number): Promise { - const res = await google.search(keyword, { page, parse_ads: false }); + const res = await search({ query: keyword, page: page }); const websites: string[] = []; let prevWebsite = ""; - for (const result of res.results) { - const website = new URL(result.url).hostname; + for (const result of res) { + if (result.type != ResultTypes.SearchResult) continue; + const website = new URL(result.link).hostname; if (website !== prevWebsite) websites.push(website); prevWebsite = website; } diff --git a/yarn.lock b/yarn.lock index fb919b28..586c151c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -627,15 +627,6 @@ __metadata: languageName: node linkType: hard -"axios@npm:^0.21.1": - version: 0.21.4 - resolution: "axios@npm:0.21.4" - dependencies: - follow-redirects: ^1.14.0 - checksum: 44245f24ac971e7458f3120c92f9d66d1fc695e8b97019139de5b0cc65d9b8104647db01e5f46917728edfc0cfd88eb30fc4c55e6053eef4ace76768ce95ff3c - languageName: node - linkType: hard - "axios@npm:^1.0.0": version: 1.4.0 resolution: "axios@npm:1.4.0" @@ -647,6 +638,17 @@ __metadata: languageName: node linkType: hard +"axios@npm:^1.4.0": + version: 1.5.0 + resolution: "axios@npm:1.5.0" + dependencies: + follow-redirects: ^1.15.0 + form-data: ^4.0.0 + proxy-from-env: ^1.1.0 + checksum: e7405a5dbbea97760d0e6cd58fecba311b0401ddb4a8efbc4108f5537da9b3f278bde566deb777935a960beec4fa18e7b8353881f2f465e4f2c0e949fead35be + languageName: node + linkType: hard + "balanced-match@npm:^1.0.0": version: 1.0.2 resolution: "balanced-match@npm:1.0.2" @@ -858,31 +860,32 @@ __metadata: languageName: node linkType: hard -"cheerio-select@npm:^1.5.0": - version: 1.6.0 - resolution: "cheerio-select@npm:1.6.0" +"cheerio-select@npm:^2.1.0": + version: 2.1.0 + resolution: "cheerio-select@npm:2.1.0" dependencies: - css-select: ^4.3.0 - css-what: ^6.0.1 - domelementtype: ^2.2.0 - domhandler: ^4.3.1 - domutils: ^2.8.0 - checksum: c64cccea5ba3af091cf876d07a8bbf81fbd616c821495d194b73829f026777a8edd17a0f760ddd5be4a213c4411c60b03d2b1f8da4a77a46c81ed596a9860b20 + boolbase: ^1.0.0 + css-select: ^5.1.0 + css-what: ^6.1.0 + domelementtype: ^2.3.0 + domhandler: ^5.0.3 + domutils: ^3.0.1 + checksum: 843d6d479922f28a6c5342c935aff1347491156814de63c585a6eb73baf7bb4185c1b4383a1195dca0f12e3946d737c7763bcef0b9544c515d905c5c44c5308b languageName: node linkType: hard -"cheerio@npm:1.0.0-rc.10": - version: 1.0.0-rc.10 - resolution: "cheerio@npm:1.0.0-rc.10" +"cheerio@npm:1.0.0-rc.12": + version: 1.0.0-rc.12 + resolution: "cheerio@npm:1.0.0-rc.12" dependencies: - cheerio-select: ^1.5.0 - dom-serializer: ^1.3.2 - domhandler: ^4.2.0 - htmlparser2: ^6.1.0 - parse5: ^6.0.1 - parse5-htmlparser2-tree-adapter: ^6.0.1 - tslib: ^2.2.0 - checksum: ace2f9c5809737534b1320d11d48762013694fa905b4deacac81a634edac178c1b0534f79d7b1896a88ce489db6cb539f222317996b21c8b6923ce413dcc1a2f + cheerio-select: ^2.1.0 + dom-serializer: ^2.0.0 + domhandler: ^5.0.3 + domutils: ^3.0.1 + htmlparser2: ^8.0.1 + parse5: ^7.0.0 + parse5-htmlparser2-tree-adapter: ^7.0.0 + checksum: 5d4c1b7a53cf22d3a2eddc0aff70cf23cbb30d01a4c79013e703a012475c02461aa1fcd99127e8d83a02216386ed6942b2c8103845fd0812300dd199e6e7e054 languageName: node linkType: hard @@ -1046,20 +1049,20 @@ __metadata: languageName: node linkType: hard -"css-select@npm:^4.3.0": - version: 4.3.0 - resolution: "css-select@npm:4.3.0" +"css-select@npm:^5.1.0": + version: 5.1.0 + resolution: "css-select@npm:5.1.0" dependencies: boolbase: ^1.0.0 - css-what: ^6.0.1 - domhandler: ^4.3.1 - domutils: ^2.8.0 + css-what: ^6.1.0 + domhandler: ^5.0.2 + domutils: ^3.0.1 nth-check: ^2.0.1 - checksum: d6202736839194dd7f910320032e7cfc40372f025e4bf21ca5bf6eb0a33264f322f50ba9c0adc35dadd342d3d6fae5ca244779a4873afbfa76561e343f2058e0 + checksum: 2772c049b188d3b8a8159907192e926e11824aea525b8282981f72ba3f349cf9ecd523fdf7734875ee2cb772246c22117fc062da105b6d59afe8dcd5c99c9bda languageName: node linkType: hard -"css-what@npm:^6.0.1": +"css-what@npm:^6.1.0": version: 6.1.0 resolution: "css-what@npm:6.1.0" checksum: b975e547e1e90b79625918f84e67db5d33d896e6de846c9b584094e529f0c63e2ab85ee33b9daffd05bff3a146a1916bec664e18bb76dd5f66cbff9fc13b2bbe @@ -1101,6 +1104,13 @@ __metadata: languageName: node linkType: hard +"deepmerge-ts@npm:^5.1.0": + version: 5.1.0 + resolution: "deepmerge-ts@npm:5.1.0" + checksum: 6b57db93c2985e4a35f24b2451db31715050d143988b7d6346f4049c9aec21a6c289514b88d3ee3d6e0697e72ef5d96ff0bbb7cb75422d56fee55ee85c7168e7 + languageName: node + linkType: hard + "define-lazy-prop@npm:^2.0.0": version: 2.0.0 resolution: "define-lazy-prop@npm:2.0.0" @@ -1168,41 +1178,41 @@ __metadata: languageName: node linkType: hard -"dom-serializer@npm:^1.0.1, dom-serializer@npm:^1.3.2": - version: 1.4.1 - resolution: "dom-serializer@npm:1.4.1" +"dom-serializer@npm:^2.0.0": + version: 2.0.0 + resolution: "dom-serializer@npm:2.0.0" dependencies: - domelementtype: ^2.0.1 - domhandler: ^4.2.0 - entities: ^2.0.0 - checksum: fbb0b01f87a8a2d18e6e5a388ad0f7ec4a5c05c06d219377da1abc7bb0f674d804f4a8a94e3f71ff15f6cb7dcfc75704a54b261db672b9b3ab03da6b758b0b22 + domelementtype: ^2.3.0 + domhandler: ^5.0.2 + entities: ^4.2.0 + checksum: cd1810544fd8cdfbd51fa2c0c1128ec3a13ba92f14e61b7650b5de421b88205fd2e3f0cc6ace82f13334114addb90ed1c2f23074a51770a8e9c1273acbc7f3e6 languageName: node linkType: hard -"domelementtype@npm:^2.0.1, domelementtype@npm:^2.2.0": +"domelementtype@npm:^2.3.0": version: 2.3.0 resolution: "domelementtype@npm:2.3.0" checksum: ee837a318ff702622f383409d1f5b25dd1024b692ef64d3096ff702e26339f8e345820f29a68bcdcea8cfee3531776b3382651232fbeae95612d6f0a75efb4f6 languageName: node linkType: hard -"domhandler@npm:^4.0.0, domhandler@npm:^4.2.0, domhandler@npm:^4.3.1": - version: 4.3.1 - resolution: "domhandler@npm:4.3.1" +"domhandler@npm:^5.0.2, domhandler@npm:^5.0.3": + version: 5.0.3 + resolution: "domhandler@npm:5.0.3" dependencies: - domelementtype: ^2.2.0 - checksum: 4c665ceed016e1911bf7d1dadc09dc888090b64dee7851cccd2fcf5442747ec39c647bb1cb8c8919f8bbdd0f0c625a6bafeeed4b2d656bbecdbae893f43ffaaa + domelementtype: ^2.3.0 + checksum: 0f58f4a6af63e6f3a4320aa446d28b5790a009018707bce2859dcb1d21144c7876482b5188395a188dfa974238c019e0a1e610d2fc269a12b2c192ea2b0b131c languageName: node linkType: hard -"domutils@npm:^2.5.2, domutils@npm:^2.8.0": - version: 2.8.0 - resolution: "domutils@npm:2.8.0" +"domutils@npm:^3.0.1": + version: 3.1.0 + resolution: "domutils@npm:3.1.0" dependencies: - dom-serializer: ^1.0.1 - domelementtype: ^2.2.0 - domhandler: ^4.2.0 - checksum: abf7434315283e9aadc2a24bac0e00eab07ae4313b40cc239f89d84d7315ebdfd2fb1b5bf750a96bc1b4403d7237c7b2ebf60459be394d625ead4ca89b934391 + dom-serializer: ^2.0.0 + domelementtype: ^2.3.0 + domhandler: ^5.0.3 + checksum: e5757456ddd173caa411cfc02c2bb64133c65546d2c4081381a3bafc8a57411a41eed70494551aa58030be9e58574fcc489828bebd673863d39924fb4878f416 languageName: node linkType: hard @@ -1275,10 +1285,10 @@ __metadata: languageName: node linkType: hard -"entities@npm:^2.0.0": - version: 2.2.0 - resolution: "entities@npm:2.2.0" - checksum: 19010dacaf0912c895ea262b4f6128574f9ccf8d4b3b65c7e8334ad0079b3706376360e28d8843ff50a78aabcb8f08f0a32dbfacdc77e47ed77ca08b713669b3 +"entities@npm:^4.2.0, entities@npm:^4.4.0": + version: 4.5.0 + resolution: "entities@npm:4.5.0" + checksum: 853f8ebd5b425d350bffa97dd6958143179a5938352ccae092c62d1267c4e392a039be1bae7d51b6e4ffad25f51f9617531fedf5237f15df302ccfb452cbf2d7 languageName: node linkType: hard @@ -1574,7 +1584,7 @@ __metadata: languageName: node linkType: hard -"follow-redirects@npm:^1.14.0, follow-redirects@npm:^1.15.0": +"follow-redirects@npm:^1.15.0": version: 1.15.2 resolution: "follow-redirects@npm:1.15.2" peerDependenciesMeta: @@ -1846,7 +1856,7 @@ __metadata: commander: ^11.0.0 eslint: ^8.48.0 esmock: ^2.3.8 - googlethis: ^1.7.1 + google-sr: ^3.2.1 mocha: ^10.2.0 nx: ^16.7.4 ora: ^7.0.1 @@ -1859,15 +1869,23 @@ __metadata: languageName: unknown linkType: soft -"googlethis@npm:^1.7.1": - version: 1.7.1 - resolution: "googlethis@npm:1.7.1" +"google-sr-selectors@npm:^0.0.2": + version: 0.0.2 + resolution: "google-sr-selectors@npm:0.0.2" + checksum: a9f6a8190575ec950f4fdd1343f9034d34a504d03d2b4eab5db70d9b0f5f9ca900975d925be58846ddb6b029919c7ab20f0da5c96dbf0d0636d877ff3e61b733 + languageName: node + linkType: hard + +"google-sr@npm:^3.2.1": + version: 3.2.1 + resolution: "google-sr@npm:3.2.1" dependencies: - axios: ^0.21.1 - cheerio: 1.0.0-rc.10 - form-data: ^4.0.0 - unraw: ^2.0.1 - checksum: 688faa0068865d735ea619647a94386f7cd1dc6a73a874c8247f0811cdb8bb26f5a2ff18953018d452b3200e447fe31db0d37ad52836f1e38e9424fe6c5ee213 + axios: ^1.4.0 + cheerio: 1.0.0-rc.12 + deepmerge-ts: ^5.1.0 + google-sr-selectors: ^0.0.2 + tslib: ^2.6.1 + checksum: 1eb28549509d5656d38fb110117ac36bd9601eeb8aef3b0ce24a19e6d46785fb448833dbfb79e493d0e821523b7e66855b444953e083125ba7ea1084028905b1 languageName: node linkType: hard @@ -1915,15 +1933,15 @@ __metadata: languageName: node linkType: hard -"htmlparser2@npm:^6.1.0": - version: 6.1.0 - resolution: "htmlparser2@npm:6.1.0" +"htmlparser2@npm:^8.0.1": + version: 8.0.2 + resolution: "htmlparser2@npm:8.0.2" dependencies: - domelementtype: ^2.0.1 - domhandler: ^4.0.0 - domutils: ^2.5.2 - entities: ^2.0.0 - checksum: 81a7b3d9c3bb9acb568a02fc9b1b81ffbfa55eae7f1c41ae0bf840006d1dbf54cb3aa245b2553e2c94db674840a9f0fdad7027c9a9d01a062065314039058c4e + domelementtype: ^2.3.0 + domhandler: ^5.0.3 + domutils: ^3.0.1 + entities: ^4.4.0 + checksum: 29167a0f9282f181da8a6d0311b76820c8a59bc9e3c87009e21968264c2987d2723d6fde5a964d4b7b6cba663fca96ffb373c06d8223a85f52a6089ced942700 languageName: node linkType: hard @@ -2893,19 +2911,22 @@ __metadata: languageName: node linkType: hard -"parse5-htmlparser2-tree-adapter@npm:^6.0.1": - version: 6.0.1 - resolution: "parse5-htmlparser2-tree-adapter@npm:6.0.1" +"parse5-htmlparser2-tree-adapter@npm:^7.0.0": + version: 7.0.0 + resolution: "parse5-htmlparser2-tree-adapter@npm:7.0.0" dependencies: - parse5: ^6.0.1 - checksum: 1848378b355d027915645c13f13f982e60502d201f53bc2067a508bf2dba4aac08219fc781dcd160167f5f50f0c73f58d20fa4fb3d90ee46762c20234fa90a6d + domhandler: ^5.0.2 + parse5: ^7.0.0 + checksum: fc5d01e07733142a1baf81de5c2a9c41426c04b7ab29dd218acb80cd34a63177c90aff4a4aee66cf9f1d0aeecff1389adb7452ad6f8af0a5888e3e9ad6ef733d languageName: node linkType: hard -"parse5@npm:^6.0.1": - version: 6.0.1 - resolution: "parse5@npm:6.0.1" - checksum: 7d569a176c5460897f7c8f3377eff640d54132b9be51ae8a8fa4979af940830b2b0c296ce75e5bd8f4041520aadde13170dbdec44889975f906098ea0002f4bd +"parse5@npm:^7.0.0": + version: 7.1.2 + resolution: "parse5@npm:7.1.2" + dependencies: + entities: ^4.4.0 + checksum: 59465dd05eb4c5ec87b76173d1c596e152a10e290b7abcda1aecf0f33be49646ea74840c69af975d7887543ea45564801736356c568d6b5e71792fd0f4055713 languageName: node linkType: hard @@ -3484,13 +3505,20 @@ __metadata: languageName: node linkType: hard -"tslib@npm:^2.2.0, tslib@npm:^2.3.0, tslib@npm:^2.4.0": +"tslib@npm:^2.3.0, tslib@npm:^2.4.0": version: 2.6.1 resolution: "tslib@npm:2.6.1" checksum: b0d176d176487905b66ae4d5856647df50e37beea7571c53b8d10ba9222c074b81f1410fb91da13debaf2cbc970663609068bdebafa844ea9d69b146527c38fe languageName: node linkType: hard +"tslib@npm:^2.6.1": + version: 2.6.2 + resolution: "tslib@npm:2.6.2" + checksum: 329ea56123005922f39642318e3d1f0f8265d1e7fcb92c633e0809521da75eeaca28d2cf96d7248229deb40e5c19adf408259f4b9640afd20d13aecc1430f3ad + languageName: node + linkType: hard + "type-check@npm:^0.4.0, type-check@npm:~0.4.0": version: 0.4.0 resolution: "type-check@npm:0.4.0" @@ -3559,13 +3587,6 @@ __metadata: languageName: node linkType: hard -"unraw@npm:^2.0.1": - version: 2.0.1 - resolution: "unraw@npm:2.0.1" - checksum: af9a9d2f6e420cb4f52fe2f1f5982e6b0be95da640d6ae8d6d9ff631d864771793cb9fe7e2a16ef1ce631b94065f4438e7bd7f1701076fc69296edc4e704d42f - languageName: node - linkType: hard - "uri-js@npm:^4.2.2": version: 4.4.1 resolution: "uri-js@npm:4.4.1" From 208e6c79bebeeff1a8c23d0359d276291eb6203f Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 13 Sep 2023 11:00:13 +0700 Subject: [PATCH 2/2] fix: use `searchWithPages` to allow searching on multiple pages correctly --- dist/rank.mjs | 36 +++++++++++++----------------------- dist/rank.mjs.map | 2 +- src/rank.mts | 37 +++++++++++++++---------------------- 3 files changed, 29 insertions(+), 46 deletions(-) diff --git a/dist/rank.mjs b/dist/rank.mjs index d7ceacb7..613edc7d 100644 --- a/dist/rank.mjs +++ b/dist/rank.mjs @@ -1,24 +1,4 @@ -import { search, ResultTypes } from "google-sr"; -/** - * Retrieves a list of websites from Google search results based on the provided keyword. - * @param keyword - The keyword to search for. - * @param page - The page number to list the website. - * @returns A promise that resolves to an array of website URLs. - */ -async function listWebsites(keyword, page) { - const res = await search({ query: keyword, page: page }); - const websites = []; - let prevWebsite = ""; - for (const result of res) { - if (result.type != ResultTypes.SearchResult) - continue; - const website = new URL(result.link).hostname; - if (website !== prevWebsite) - websites.push(website); - prevWebsite = website; - } - return websites; -} +import { ResultTypes, searchWithPages } from "google-sr"; /** * Retrieves the rank of a website in Google search results for a specific keyword. * @param website - The website URL to check the rank for. @@ -28,8 +8,18 @@ async function listWebsites(keyword, page) { */ export async function getWebsiteRank(website, keyword, opts) { const maxPage = opts?.maxPage ?? 1; - for (let page = 0; page < maxPage; ++page) { - const websites = await listWebsites(keyword, page); + const res = await searchWithPages({ query: keyword, pages: maxPage }); + for (let page = 0; page < res.length; ++page) { + const websites = []; + let prevWebsite = ""; + for (const result of res[page]) { + if (result.type == ResultTypes.SearchResult) { + const website = new URL(result.link).hostname; + if (website !== prevWebsite) + websites.push(website); + prevWebsite = website; + } + } for (let rank = 0; rank < websites.length; ++rank) { if (websites[rank].includes(website)) { return { page, rank }; diff --git a/dist/rank.mjs.map b/dist/rank.mjs.map index 7a21f6b2..5d7f8f92 100644 --- a/dist/rank.mjs.map +++ b/dist/rank.mjs.map @@ -1 +1 @@ -{"version":3,"file":"rank.mjs","sourceRoot":"","sources":["../src/rank.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAEhD;;;;;GAKG;AACH,KA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O,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAazD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAAe,EACf,OAAe,EACf,IAA2B;IAE3B,MAAM,OAAO,GAAG,IAAI,EAAE,OAAO,IAAI,CAAC,CAAC;IAEnC,MAAM,GAAG,GAAG,MAAM,eAAe,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;IAEtE,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,GAAG,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE;QAC5C,MAAM,QAAQ,GAAa,EAAE,CAAC;QAC9B,IAAI,WAAW,GAAG,EAAE,CAAC;QACrB,KAAK,MAAM,MAAM,IAAI,GAAG,CAAC,IAAI,CAAC,EAAE;YAC9B,IAAI,MAAM,CAAC,IAAI,IAAI,WAAW,CAAC,YAAY,EAAE;gBAC3C,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC;gBAC9C,IAAI,OAAO,KAAK,WAAW;oBAAE,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBACpD,WAAW,GAAG,OAAO,CAAC;aACvB;SACF;QAED,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,QAAQ,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE;YACjD,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;gBACpC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;aACvB;SACF;KACF;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"} \ No newline at end of file diff --git a/src/rank.mts b/src/rank.mts index 33dc8d90..60506fc2 100644 --- a/src/rank.mts +++ b/src/rank.mts @@ -1,23 +1,4 @@ -import { search, ResultTypes } from "google-sr"; - -/** - * Retrieves a list of websites from Google search results based on the provided keyword. - * @param keyword - The keyword to search for. - * @param page - The page number to list the website. - * @returns A promise that resolves to an array of website URLs. - */ -async function listWebsites(keyword: string, page: number): Promise { - const res = await search({ query: keyword, page: page }); - const websites: string[] = []; - let prevWebsite = ""; - for (const result of res) { - if (result.type != ResultTypes.SearchResult) continue; - const website = new URL(result.link).hostname; - if (website !== prevWebsite) websites.push(website); - prevWebsite = website; - } - return websites; -} +import { ResultTypes, searchWithPages } from "google-sr"; /** * Represents the ranking of a website in Google Search. @@ -43,8 +24,20 @@ export async function getWebsiteRank( opts?: { maxPage?: number }, ): Promise { const maxPage = opts?.maxPage ?? 1; - for (let page = 0; page < maxPage; ++page) { - const websites = await listWebsites(keyword, page); + + const res = await searchWithPages({ query: keyword, pages: maxPage }); + + for (let page = 0; page < res.length; ++page) { + const websites: string[] = []; + let prevWebsite = ""; + for (const result of res[page]) { + if (result.type == ResultTypes.SearchResult) { + const website = new URL(result.link).hostname; + if (website !== prevWebsite) websites.push(website); + prevWebsite = website; + } + } + for (let rank = 0; rank < websites.length; ++rank) { if (websites[rank].includes(website)) { return { page, rank };