Skip to content

Commit

Permalink
chore: merge pull request #79 from threeal/use-google-sr
Browse files Browse the repository at this point in the history
Use google-sr
  • Loading branch information
threeal authored Sep 13, 2023
2 parents 5af970d + 208e6c7 commit dfe2cc3
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 140 deletions.
34 changes: 13 additions & 21 deletions dist/rank.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
import google from "googlethis";
/**
* 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 google.search(keyword, { page, parse_ads: false });
const websites = [];
let prevWebsite = "";
for (const result of res.results) {
const website = new URL(result.url).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.
Expand All @@ -26,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 };
Expand Down
2 changes: 1 addition & 1 deletion dist/rank.mjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
36 changes: 15 additions & 21 deletions src/rank.mts
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
import google from "googlethis";

/**
* 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<string[]> {
const res = await google.search(keyword, { page, parse_ads: false });
const websites: string[] = [];
let prevWebsite = "";
for (const result of res.results) {
const website = new URL(result.url).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.
Expand All @@ -42,8 +24,20 @@ export async function getWebsiteRank(
opts?: { maxPage?: number },
): Promise<WebsiteRank | undefined> {
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 };
Expand Down
Loading

0 comments on commit dfe2cc3

Please # to comment.