Skip to content

Commit 3a7de59

Browse files
committed
fix: handle 'Ask HN' or any post without url and has story text
1 parent 813f016 commit 3a7de59

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/lib/hn.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type StoryOutput = {
1111
content: string
1212
comments: SlimComment[]
1313
title: string
14-
url: string
14+
url?: string
1515
storyId: number
1616
/** Sitename, byline, or readable hostname */
1717
source: string
@@ -73,7 +73,8 @@ export type Hit = {
7373
story_id: number
7474
title: string
7575
updated_at: string
76-
url: string
76+
url?: string
77+
story_text?: string
7778
}
7879

7980
export type ProcessingTimingsMS = {

0 commit comments

Comments
 (0)