| 1 | 'use strict'; |
| 2 | |
| 3 | const fetch = require('node-fetch'); |
| 4 | const {writeFileSync} = require('fs'); |
| 5 | const stories = 50; |
| 6 | |
| 7 | async function getStory(id) { |
| 8 | const storyRes = await fetch( |
| 9 | `https://hacker-news.firebaseio.com/v0/item/${id}.json` |
| 10 | ); |
| 11 | return await storyRes.json(); |
| 12 | } |
| 13 | |
| 14 | async function getTopStories() { |
| 15 | const topStoriesRes = await fetch( |
| 16 | 'https://hacker-news.firebaseio.com/v0/topstories.js' |
| 17 | ); |
| 18 | const topStoriesIds = await topStoriesRes.json(); |
| 19 | |
| 20 | const topStories = []; |
| 21 | for (let i = 0; i < stories; i++) { |
| 22 | const topStoriesId = topStoriesIds[i]; |
| 23 | |
| 24 | topStories.push(await getStory(topStoriesId)); |
| 25 | } |
| 26 | |
| 27 | writeFileSync( |
| 28 | 'top-stories.json', |
| 29 | `window.stories = ${JSON.stringify(topStories)}` |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | getTopStories(); |