Created
July 4, 2024 21:41
-
-
Save bobbywilson0/49e4728e539c726e921c79f614254ec6 to your computer and use it in GitHub Desktop.
fetch who's hiring posts from hn api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetch from "node-fetch"; | |
import * as fs from "node:fs/promises"; | |
const BASE_URL = "https://hacker-news.firebaseio.com/v0"; | |
const fetchData = async () => { | |
try { | |
const response = await fetch(`${BASE_URL}/user/whoishiring.json`); | |
const data = await response.json(); | |
for (const n of data.submitted) { | |
const response = await fetch(`${BASE_URL}/item/${n}.json`); | |
const topLevelPosts = await response.json(); | |
if (topLevelPosts.kids === undefined) { | |
continue; | |
} else if ( | |
topLevelPosts.title && | |
topLevelPosts.title.match(/Ask HN: Who is hiring\?/) != null | |
) { | |
for (const m of topLevelPosts.kids) { | |
const response = await fetch(`${BASE_URL}/item/${m}.json`); | |
const data = await response.json(); | |
const fileName = `${m}.json`; | |
const filePath = `./raw/${fileName}`; | |
try { | |
if (data.deleted != true && data.text != undefined) { | |
await fs.writeFile(filePath, JSON.stringify(data)); | |
console.log(`Data written to ${filePath}`); | |
} | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
} | |
} | |
} catch (error) { | |
console.error(error); | |
} | |
}; | |
fetchData(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment