import { Hono } from 'npm:hono';
import sheet from 'npm:@yawnxyz/sheetlog@0.0.15';
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
import { Sema } from 'npm:async-sema';
import { modelProvider } from 'https://esm.town/v/yawnxyz/ai';
sheet.setup({
sheet: "links",
sheetUrl: "https://script.google.com/macros/s/AKfycbzavst1u2rbRAw-bkIrhoQT07507yvSHRFSkMKkDRQvsHAAe9lQ78s8c1TIfJJb7F50/exec"
})
const sema = new Sema(5);
export async function addLinksToSheet(links) {
let counter = 0;
for (const link of links) {
await sema.acquire();
(async () => {
try {
console.log(`Adding link ${counter}:`, link.title, link.url);
counter++;
await sheet.log({
title: link.title,
url: link.url,
postedAt: new Date(link.postedAt).toLocaleString('en-US', { timeZone: 'UTC' }),
data: JSON.stringify(link),
});
} finally {
sema.release();
}
})();
}
}
export async function getSidebar(limit = 1, start = 0) {
const gql = {
"operationName": "posts",
"variables": {
"input": {
"limit": limit,
"offset": start,
"enableTotal": true,
"enableCache": true,
"filter": {
"status": {
"_eq": 2
}
},
"sort": {
"postedAt": "desc"
}
}
},
"query": `query posts($input: MultiPostInput) {
posts(input: $input) {
results {
...PostFragment
__typename
}
totalCount
__typename
}
}
fragment PostFragment on Post {
_id
title
shortTitle
url
credit
twitterName
postedAt
createdAt
pageUrl
body
userId
categories {
...CategoryFragment
}
}
fragment CategoryFragment on Category {
_id
name
slug
pagePath
}`
};
let results = await fetchJSON(`https://archive.sidebar.io/graphql`, {