Skip to content

Commit

Permalink
feat: add support to set max page and show page ranking
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Jul 17, 2023
1 parent f8db940 commit d1750f9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ To retrieve the rank of a website for a specific keyword, run the `google-rank`
$ google-rank wikipedia.org krakatoa
Ranks for wikipedia.org website:
1 krakatoa
page 1 rank 1 krakatoa
```

Multiple keywords can also be specified:

```
$ google-rank wikipedia.org krakatoa mit 'social media'
$ google-rank wikipedia.org krakatoa facebook 'social media'
Ranks for wikipedia.org website:
1 krakatoa
2 mit
1 social media
page 1 rank 1 krakatoa
page 2 rank 2 facebook
page 1 rank 1 social media
```

If the website is not found for the specified keywords, it will output the rank as `?`:
Expand All @@ -44,7 +44,7 @@ If the website is not found for the specified keywords, it will output the rank
$ google-rank wikipedia.org 'best city to travel'
Ranks for wikipedia.org website:
? best city to travel
page ? rank ? best city to travel
```

## License
Expand Down
14 changes: 11 additions & 3 deletions lib/google-rank.js

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

2 changes: 1 addition & 1 deletion lib/google-rank.js.map

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

14 changes: 11 additions & 3 deletions src/google-rank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,30 @@ async function run() {
program
.argument("<website>", "website name")
.arguments("<keywords...>")
.option("--max-page <number>", "maximum page to search for", "3")
.parse();

const website = program.args[0];
const keywords = program.args.slice(1);

const opts = program.opts();
const maxPage = Number.parseInt(opts.maxPage);

const rankByKeywords: [string, Promise<WebsiteRank | undefined>][] = [];
for (const keyword of keywords) {
const prom = getWebsiteRank(website, keyword);
const prom = getWebsiteRank(website, keyword, { maxPage });
rankByKeywords.push([keyword, prom]);
}

process.stdout.write(`Ranks for ${website} website:\n`);
for (const [keyword, prom] of rankByKeywords) {
const rank = await prom;
const rankStr = rank !== undefined ? `${rank.rank}` : "?";
process.stdout.write(`${rankStr} ${keyword}\n`);
if (rank === undefined) {
process.stdout.write(`page ? rank ?`);
} else {
process.stdout.write(`page ${rank.page + 1} rank ${rank.rank + 1}`);
}
process.stdout.write(` ${keyword}\n`);
}
}

Expand Down

0 comments on commit d1750f9

Please sign in to comment.