Skip to content

Commit

Permalink
polling new content api to get highlighted content
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed May 11, 2024
1 parent 0190b1c commit 0d6712d
Showing 1 changed file with 75 additions and 1 deletion.
76 changes: 75 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Item, ItemFormat, Omnivore } from '@omnivore-app/api'
import { requestUrl } from 'obsidian'

export enum HighlightColors {
Yellow = 'yellow',
Expand All @@ -7,6 +8,50 @@ export enum HighlightColors {
Blue = 'blue',
}

interface GetContentResponse {
data: {
libraryItemId: string
downloadUrl: string
error?: string
}[]
}

const getContent = async (
endpoint: string,
apiKey: string,
libraryItemIds: string[],
): Promise<GetContentResponse> => {
const response = await requestUrl({
url: `${endpoint}/api/content`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: apiKey,
},
body: JSON.stringify({ libraryItemIds, format: 'highlightedMarkdown' }),
})

return response.json
}

const downloadFromUrl = async (url: string): Promise<string> => {
try {
// polling until download is ready or failed
const response = await requestUrl({
url,
})
return response.text
} catch (error) {
// retry after 1 second if download returns 404
if (error.status === 404) {
await sleep(1000)
return downloadFromUrl(url)
}

throw error
}
}

export const getItems = async (
endpoint: string,
apiKey: string,
Expand All @@ -27,11 +72,40 @@ export const getItems = async (
after,
first,
query: `${updatedAt ? 'updated:' + updatedAt : ''} sort:saved-asc ${query}`,
includeContent,
includeContent: false,
format,
})

const articles = response.edges.map((e) => e.node)
if (includeContent && articles.length > 0) {
try {
const content = await getContent(
endpoint,
apiKey,
articles.map((a) => a.id),
)

await Promise.allSettled(
content.data.map(async (c, index) => {
if (c.error) {
console.error('Error fetching content', c.error)
return
}

// timeout if download takes too long
articles[index].content = await Promise.race([
downloadFromUrl(c.downloadUrl),
new Promise<string>(
(_, reject) => setTimeout(() => reject('Timeout'), 10000), // 10 seconds
),
])
}),
)
} catch (error) {
console.error('Error fetching content', error)
}
}

return [articles, response.pageInfo.hasNextPage]
}

Expand Down

0 comments on commit 0d6712d

Please sign in to comment.