@@ -18,11 +18,14 @@ export async function fetchTopStories(count: number = 10): Promise<StoryOutput[]
1818 const data = ( await response . json ( ) ) as ResponseData
1919
2020 // Extract only the data we need
21- const slim = data . hits . map ( s => ( {
22- title : s . title ,
23- url : s . url ,
24- storyId : s . story_id ,
25- } ) )
21+ const slim = data . hits . map ( s => {
22+ return {
23+ title : s . title ,
24+ url : s . url ,
25+ storyId : s . story_id ,
26+ story_text : s . story_text ,
27+ }
28+ } )
2629
2730 // Check if we have already covered the story
2831 const covered = await readFromCache ( 'covered-stories' )
@@ -58,10 +61,33 @@ export async function fetchTopStories(count: number = 10): Promise<StoryOutput[]
5861 // Fetch the content and comments for each story
5962 const output : StoryOutput [ ] = [ ]
6063 for ( const story of filtered ) {
64+ const comments = await fetchStoryDataById ( story . storyId )
6165 logger . info ( `Fetching [${ story . storyId } ] - ${ story . title } - ${ story . url } ` )
6266 const cacheKey = 'story-' + story . storyId . toString ( )
6367
6468 let htmlString = await readFromCache ( cacheKey )
69+
70+ const baseStoryOutput : Pick < StoryOutput , 'comments' | 'hnUrl' | 'storyId' | 'title' > = {
71+ title : story . title ,
72+ storyId : story . storyId ,
73+ comments,
74+ hnUrl : `https://news.ycombinator.com/item?id=${ story . storyId } ` ,
75+ }
76+
77+ // Ask HN posts don't have a url, but have a story_text
78+ if ( ! story . url && story . story_text ) {
79+ output . push ( {
80+ content : story . story_text ,
81+ source : 'Hacker News' ,
82+ ...baseStoryOutput ,
83+ } )
84+ }
85+
86+ if ( ! story . url ) {
87+ logger . error ( `No url or story text found for story ${ story . storyId } ` )
88+ continue
89+ }
90+
6591 if ( ! htmlString ) {
6692 htmlString = await fetch ( story . url ) . then ( res => res . text ( ) )
6793 if ( ! htmlString ) {
@@ -87,16 +113,12 @@ export async function fetchTopStories(count: number = 10): Promise<StoryOutput[]
87113
88114 logger . debug ( { byline, excerpt, siteName } )
89115
90- const comments = await fetchStoryDataById ( story . storyId )
91116 output . push ( {
92117 content : textContent ,
93- comments,
94- title : story . title ,
95118 url : story . url ,
96- storyId : story . storyId ,
97119 // strip http(s), parse just the domain from the url
98120 source : siteName ?? byline ?? new URL ( story . url ) . hostname . replace ( 'www.' , '' ) ,
99- hnUrl : `https://news.ycombinator.com/item?id= ${ story . storyId } ` ,
121+ ... baseStoryOutput ,
100122 } )
101123 }
102124
0 commit comments