-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
279 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,167 +1,56 @@ | ||
import { Omnivore } from '@omnivore-app/api' | ||
import { Item, ItemFormat, Omnivore } from '@omnivore-app/api' | ||
|
||
export interface SearchResponse { | ||
data: { | ||
search: { | ||
edges: { node: Article }[] | ||
pageInfo: { | ||
hasNextPage: boolean | ||
} | ||
} | ||
} | ||
} | ||
|
||
export enum UpdateReason { | ||
CREATED = 'CREATED', | ||
UPDATED = 'UPDATED', | ||
DELETED = 'DELETED', | ||
} | ||
|
||
export interface UpdatesSinceResponse { | ||
data: { | ||
updatesSince: { | ||
edges: { updateReason: UpdateReason; node: Article }[] | ||
pageInfo: { | ||
hasNextPage: boolean | ||
} | ||
} | ||
} | ||
} | ||
|
||
export type PageType = | ||
| 'ARTICLE' | ||
| 'BOOK' | ||
| 'FILE' | ||
| 'PROFILE' | ||
| 'UNKNOWN' | ||
| 'WEBSITE' | ||
| 'TWEET' | ||
| 'VIDEO' | ||
| 'IMAGE' | ||
| 'HIGHLIGHTS' | ||
|
||
export interface Article { | ||
id: string | ||
title: string | ||
siteName?: string | null | ||
originalArticleUrl: string | null | ||
author?: string | null | ||
description?: string | null | ||
slug: string | ||
labels: Label[] | null | ||
highlights: Highlight[] | null | ||
updatedAt: string | null | ||
savedAt: string | ||
pageType: PageType | ||
content?: string | null | ||
publishedAt?: string | null | ||
readAt?: string | null | ||
readingProgressPercent: number | ||
isArchived: boolean | ||
wordsCount?: number | null | ||
archivedAt?: string | null | ||
} | ||
|
||
export interface Label { | ||
name: string | ||
} | ||
|
||
export type HighlightType = 'HIGHLIGHT' | 'NOTE' | 'REDACTION' | ||
|
||
export interface Highlight { | ||
id: string | ||
quote: string | null | ||
annotation: string | null | ||
patch: string | null | ||
updatedAt: string | null | ||
labels?: Label[] | null | ||
type: HighlightType | ||
highlightPositionPercent: number | null | ||
color?: string | null | ||
highlightPositionAnchorIndex: number | null | ||
} | ||
|
||
const ENDPOINT = 'https://api-prod.omnivore.app/api/graphql' | ||
const requestHeaders = (apiKey: string) => ({ | ||
'Content-Type': 'application/json', | ||
authorization: apiKey, | ||
'X-OmnivoreClient': 'logseq-plugin', | ||
}) | ||
|
||
export const getOmnivoreArticles = async ( | ||
export const getOmnivoreItems = async ( | ||
apiKey: string, | ||
after = 0, | ||
first = 10, | ||
updatedAt = '', | ||
query = '', | ||
includeContent = false, | ||
format = 'html', | ||
endpoint = ENDPOINT | ||
): Promise<[Article[], boolean]> => { | ||
format: ItemFormat = 'html', | ||
baseUrl?: string | ||
): Promise<[Item[], boolean]> => { | ||
const omnivore = new Omnivore({ | ||
authToken: apiKey, | ||
baseUrl: endpoint, | ||
apiKey, | ||
baseUrl, | ||
timeoutMs: 10000, | ||
}) | ||
|
||
const result = await omnivore.items.search({ | ||
after: after.toString(), | ||
after, | ||
first, | ||
query: `${updatedAt ? 'updated:' + updatedAt : ''} sort:saved-asc ${query}`, | ||
includeContent, | ||
format: format as 'html' | 'markdown', | ||
format, | ||
}) | ||
|
||
const items = result.edges.map((e) => e.node) | ||
|
||
return [items, result.pageInfo.hasNextPage] | ||
} | ||
|
||
export const getDeletedOmnivoreArticles = async ( | ||
export const getDeletedOmnivoreItems = async ( | ||
apiKey: string, | ||
after = 0, | ||
first = 10, | ||
updatedAt = '', | ||
endpoint = ENDPOINT | ||
): Promise<[Article[], boolean]> => { | ||
const res = await fetch(endpoint, { | ||
headers: requestHeaders(apiKey), | ||
body: JSON.stringify({ | ||
query: ` | ||
query UpdatesSince($after: String, $first: Int, $since: Date!) { | ||
updatesSince(first: $first, after: $after, since: $since) { | ||
... on UpdatesSinceSuccess { | ||
edges { | ||
updateReason | ||
node { | ||
id | ||
slug | ||
title | ||
savedAt | ||
} | ||
} | ||
pageInfo { | ||
hasNextPage | ||
} | ||
} | ||
... on UpdatesSinceError { | ||
errorCodes | ||
} | ||
} | ||
}`, | ||
variables: { | ||
after: `${after}`, | ||
first, | ||
since: updatedAt || '2021-01-01', | ||
}, | ||
}), | ||
method: 'POST', | ||
baseUrl: string | ||
): Promise<[Item[], boolean]> => { | ||
const omnivore = new Omnivore({ | ||
apiKey, | ||
baseUrl, | ||
timeoutMs: 10000, | ||
}) | ||
|
||
const result = await omnivore.items.updates({ | ||
after, | ||
first, | ||
since: updatedAt || '2021-01-01', | ||
}) | ||
|
||
const jsonRes = (await res.json()) as UpdatesSinceResponse | ||
const deletedArticles = jsonRes.data.updatesSince.edges | ||
.filter((edge) => edge.updateReason === UpdateReason.DELETED) | ||
.map((edge) => edge.node) | ||
const deletedItems = result.edges | ||
.filter((edge) => edge.updateReason === 'DELETED' && edge.node) | ||
.map((edge) => edge.node) as Item[] | ||
|
||
return [deletedArticles, jsonRes.data.updatesSince.pageInfo.hasNextPage] | ||
return [deletedItems, result.pageInfo.hasNextPage] | ||
} |
Oops, something went wrong.