Skip to content

Commit

Permalink
feat: Make youtubeHls configurable per-instance
Browse files Browse the repository at this point in the history
  • Loading branch information
otomir23 committed Nov 26, 2024
1 parent 7fe0f4d commit 3363697
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/core/data/cobalt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,34 @@ const cobaltErrors = new Map([
["content.post.age", "error-media-unavailable"],
].map(([k, v]) => [`error.api.${k}`, v]))

export async function fetchMedia({ url, lang, apiBaseUrl, downloadMode = "auto", auth }: {
const stackHeaders = (...headers: ([string, string] | null | false | undefined)[]) => headers.filter((h): h is [string, string] => !!h)

const stackObjects = <T>(...objs: (T | null | false | undefined)[]) => objs.reduce(
(p, c) => c ? ({ ...p, ...c }) : p,
{},
)

export async function fetchMedia({ url, lang, apiBaseUrl, downloadMode = "auto", auth, youtubeHls }: {
url: string,
lang?: string,
downloadMode?: string,
apiBaseUrl: string,
auth?: string,
youtubeHls?: boolean,
}): Promise<Result<SuccessfulCobaltMediaResponse, Text>> {
const res = await fetch(`${apiBaseUrl}`, {
method: "POST",
headers: [
headers: stackHeaders(
["Accept", "application/json"],
["Content-Type", "application/json"],
["User-Agent", "cobold (+https://github.com/tskau/cobold)"],
...auth ? [["Authorization", auth] satisfies [string, string]] : [],
...lang ? [["Accept-Language", lang] satisfies [string, string]] : [],
],
body: JSON.stringify({ url, downloadMode, filenameStyle: "basic", youtubeHLS: true }),
!!auth && ["Authorization", auth],
!!lang && ["Accept-Language", lang],
),
body: JSON.stringify(stackObjects(
{ url, downloadMode, filenameStyle: "basic" },
youtubeHls && { youtubeHLS: true },
)),
}).catch(() => null)

if (!res)
Expand Down
3 changes: 3 additions & 0 deletions src/core/data/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ export const apiServerSchema = z.object({
name: z.string().optional(),
url: z.string().url(),
auth: z.string().optional(),
youtubeHls: z.boolean().optional(),
}).or(
z.string().url().transform(data => ({
name: undefined,
url: data,
auth: undefined,
youtubeHls: undefined,
})),
).transform(data => ({
...data,
Expand Down Expand Up @@ -120,6 +122,7 @@ async function tryDownload(outputType: string, request: MediaRequest, apiPool: A
lang,
apiBaseUrl: currentApi.url,
auth: currentApi.auth,
youtubeHls: currentApi.youtubeHls,
})

if (!res.success) {
Expand Down

0 comments on commit 3363697

Please sign in to comment.