2023-02-01 07:45:18 +00:00
|
|
|
import {
|
|
|
|
|
WALLABAG_CLIENT_ID,
|
|
|
|
|
WALLABAG_CLIENT_SECRET,
|
|
|
|
|
WALLABAG_USERNAME,
|
|
|
|
|
WALLABAG_PASSWORD,
|
|
|
|
|
WALLABAG_URL
|
|
|
|
|
} from '$env/static/private';
|
2023-02-03 06:40:02 +00:00
|
|
|
import type { Article } from '$root/lib/types/article';
|
|
|
|
|
import type { PageQuery } from '$root/lib/types/pageQuery';
|
2023-02-01 07:45:18 +00:00
|
|
|
import { URLSearchParams } from 'url';
|
|
|
|
|
|
|
|
|
|
const base: string = WALLABAG_URL;
|
|
|
|
|
|
|
|
|
|
export async function fetchArticlesApi(
|
|
|
|
|
method: string,
|
|
|
|
|
resource: string,
|
|
|
|
|
queryParams: Record<string, string>,
|
|
|
|
|
data?: Record<string, unknown>
|
|
|
|
|
) {
|
2023-02-03 06:40:02 +00:00
|
|
|
let lastFetched: Date | null = null;
|
2023-02-01 07:45:18 +00:00
|
|
|
|
|
|
|
|
const authBody = {
|
|
|
|
|
grant_type: 'password',
|
|
|
|
|
client_id: WALLABAG_CLIENT_ID,
|
|
|
|
|
client_secret: WALLABAG_CLIENT_SECRET,
|
|
|
|
|
username: WALLABAG_USERNAME,
|
|
|
|
|
password: WALLABAG_PASSWORD
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const authResponse = await fetch(`${base}/oauth/v2/token`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
|
body: new URLSearchParams(authBody)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const auth = await authResponse.json();
|
|
|
|
|
|
2023-02-03 06:40:02 +00:00
|
|
|
const pageQuery: PageQuery = {
|
2023-02-01 07:45:18 +00:00
|
|
|
sort: 'updated',
|
2023-02-03 06:40:02 +00:00
|
|
|
perPage: 6,
|
|
|
|
|
since: 0
|
2023-02-01 07:45:18 +00:00
|
|
|
};
|
2023-02-03 06:40:02 +00:00
|
|
|
const entriesQueryParams = new URLSearchParams(pageQuery);
|
|
|
|
|
console.log(`Entries params: ${entriesQueryParams}`);
|
2023-02-01 07:45:18 +00:00
|
|
|
|
|
|
|
|
if (lastFetched) {
|
|
|
|
|
pageQuery.since = Math.round(lastFetched / 1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastFetched = new Date();
|
|
|
|
|
|
2023-02-03 06:40:02 +00:00
|
|
|
const nbEntries = 0;
|
|
|
|
|
const pageResponse = await fetch(`${WALLABAG_URL}/api/entries.json?${entriesQueryParams}`, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${auth.access_token}`
|
2023-02-01 07:45:18 +00:00
|
|
|
}
|
2023-02-03 06:40:02 +00:00
|
|
|
});
|
2023-02-01 07:45:18 +00:00
|
|
|
|
|
|
|
|
if (!pageResponse.ok) {
|
|
|
|
|
throw new Error(pageResponse.statusText);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 06:40:02 +00:00
|
|
|
const entries = await pageResponse.json();
|
|
|
|
|
const articles: Article[] = [];
|
2023-02-01 07:45:18 +00:00
|
|
|
|
2023-02-03 06:40:02 +00:00
|
|
|
// do {
|
|
|
|
|
// nbEntries += entries._embedded.items.length;
|
|
|
|
|
console.log(`number of articles fetched: ${entries._embedded.items.length}`);
|
|
|
|
|
entries._embedded.items.forEach((article: Article) => {
|
|
|
|
|
article.created_at = new Date(article.created_at);
|
|
|
|
|
article.updated_at = new Date(article.updated_at);
|
|
|
|
|
article.archived_at = article.archived_at ? new Date(article.archived_at) : null;
|
|
|
|
|
articles.push(article);
|
|
|
|
|
});
|
2023-02-01 07:45:18 +00:00
|
|
|
|
2023-02-03 06:40:02 +00:00
|
|
|
// if (!entries._links.next) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// console.log(`Links next ${JSON.stringify(entries._links.next)}`);
|
|
|
|
|
// const response = await fetch(entries._links.next.href, {
|
|
|
|
|
// method: 'GET',
|
|
|
|
|
// headers: {
|
|
|
|
|
// Authorization: `Bearer ${auth.access_token}`
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// entries = await response.json();
|
|
|
|
|
// } while (entries._links.next);
|
2023-02-01 07:45:18 +00:00
|
|
|
|
|
|
|
|
return { articles };
|
|
|
|
|
}
|