Skip to content

Instantly share code, notes, and snippets.

@VehpuS
Last active November 30, 2022 13:27
Show Gist options
  • Save VehpuS/d70dc3669d96da953c7a4f9f6665e83d to your computer and use it in GitHub Desktop.
Save VehpuS/d70dc3669d96da953c7a4f9f6665e83d to your computer and use it in GitHub Desktop.
Scraping personal YCombinator hackernews upvotes from the developer console after login (running in dev console adds the relevant cookie info). Can easily be adapted to an independent scraper / work for more than just the URL
/**
* Get a list of all upvotes urls for a user from a single page
* @param {string} user - The logged in username (you won't be able to get a response for another user)
* @param {number} pageNum - The page of upvotes to query
* @returns {Promise<string[]>} a list of upvoted URLs from the page
*/
const queryUserUpvotesByPage = async (user, pageNum) => {
const req = await fetch(`https://news.ycombinator.com/upvoted?id=${user}&p=${pageNum}`);
const text = await test.text();
return Array.from(text.matchAll(/titleline.*?a href\="(.*?)"/g)).map(([t, url]) => url);
}
/**
* Get a list of all upvotes urls for a user
* @param {string} user - The logged in username (you won't be able to get a response for another user)
* @param {number} delay - Delay between requests (to avoid rate limiting)
* @param {number} maxPages - Max pages to check for (to avoid infinite loops)
* @returns {Promise<string[]>} a list of upvoted URLs by the user
*/
const getAllUserUpvotes = async (user, delay=1000, maxPages=9999) => {
const allUrls = [];
for (let pageNum = 0; pageNum += 1; pageNum < maxPages) {
let newPageVotes = [];
try {
newPageVotes = await queryUserUpvotesByPage(user, pageNum);
} catch(err) {
break;
}
if (newPageVotes.length === 0) {
break;
}
allUrls.push(...newPageVotes);
console.log(`Got ${newPageVotes.length} urls from page ${pageNum} - total ${allUrls.length}`);
await new Promise(resolve => setTimeout(resolve, delay));
}
return allUrls;
}
console.log(await getAllUserUpvotes("test"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment