-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: move all deno scripts to .ts files, allow their discovery, prep …
…for fixing this error: https://github.com/bevry/dorothy/actions/runs/10841571157/job/30085812259#step:2:774
- Loading branch information
Showing
5 changed files
with
113 additions
and
116 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
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// deno-lint-ignore-file no-explicit-any | ||
const [apikey, username, collection] = Deno.args | ||
if (!apikey || !username || !collection) { | ||
throw new Error('Missing arguments: apikey, username, collection') | ||
} | ||
const url = new URL( | ||
`https://wallhaven.cc/api/v1/collections/${username}/${collection}?apikey=${apikey}`, | ||
) | ||
function stderr(text: string) { | ||
return Deno.stderr.write(new TextEncoder().encode(text + '\n')) | ||
} | ||
function stdout(text: string) { | ||
return Deno.stdout.write(new TextEncoder().encode(text + '\n')) | ||
} | ||
function wait(ms: number, callback: () => void) { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(function () { | ||
try { | ||
resolve(callback()) | ||
} catch (error) { | ||
reject(error) | ||
} | ||
}, ms) | ||
}) | ||
} | ||
async function fetchNextPage() { | ||
await stderr('Fetching: ' + url.toString()) | ||
const req = await fetch(url) | ||
if (req.status === 429) { | ||
await stderr('Waiting 45 seconds for rate limiting.') | ||
return await wait(45 * 1000, fetchNextPage) | ||
} | ||
const text = await req.text() | ||
const json: any = await Promise.resolve().then(function () { | ||
try { | ||
return JSON.parse(text) | ||
} catch (err) { | ||
console.error('Failed to parse JSON:', err, text) | ||
return Promise.reject('Failed to download ' + url) | ||
} | ||
}) | ||
const urls = json.data.map((item: any) => item.path) | ||
await stdout(urls.join('\n')) | ||
if (json.meta.last_page !== json.meta.current_page) { | ||
url.searchParams.set('page', String(json.meta.current_page + 1)) | ||
fetchNextPage() | ||
} | ||
} | ||
await fetchNextPage() |
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// deno-lint-ignore-file no-explicit-any | ||
|
||
// parse stdin | ||
const json = await new Response(Deno.stdin.readable).json() | ||
|
||
// prepare result | ||
const names = new Set<string>() | ||
|
||
// add casks if not filtered to only formula | ||
if (Deno.args.includes('--formula') === false) { | ||
// all casks are by request, so no need for special --requested handling for casks | ||
for (const cask of json.casks.filter((i: any) => i.installed)) { | ||
names.add(cask.full_token) | ||
} | ||
} | ||
|
||
// add formulas if not filtered to only casks | ||
if (Deno.args.includes('--cask') === false) { | ||
if (Deno.args.includes('--requested')) { | ||
// only requested formula | ||
for (const formula of json.formulae) { | ||
if (formula.installed.find((i: any) => i.installed_on_request)) { | ||
names.add(formula.full_name) | ||
} | ||
} | ||
} else { | ||
// all formula | ||
for (const formula of json.formulae.filter((i: any) => i.installed)) { | ||
names.add(formula.full_name) | ||
} | ||
} | ||
} | ||
|
||
// output result | ||
if (names.size) { | ||
await Deno.stdout.write( | ||
new TextEncoder().encode(Array.from(names).sort().concat('').join('\n')), | ||
) | ||
} |
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