Skip to content

Commit

Permalink
feat(animegarden): support pass baseURL
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jan 16, 2025
1 parent c5a2411 commit a5d0145
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/animegarden/src/download/aria2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class Aria2Client extends DownloadClient {
if (status.errorMessage && status.errorMessage.indexOf('[METADATA]') === -1) {
const REs = [
/File (.*) exists, but a control file\(\*.aria2\) does not exist/,
/ (.*) \(\*.aria2\) /,
/ (.*) \(\*.aria2\) /
];
for (const RE of REs) {
if (RE.test(status.errorMessage)) {
Expand Down
23 changes: 10 additions & 13 deletions packages/animegarden/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@ import { memo } from 'memofunc';
import { fetchResourceDetail } from 'animegarden';
import { bold, dim, lightBlue, lightCyan, lightRed, link } from '@breadc/color';

import {
type AnimeSystem,
onDeath,
type Plugin,
type PluginEntry,
StringArray,
ufetch
} from '@animespace/core';
import { type AnimeSystem, type Plugin, type PluginEntry, onDeath, ufetch } from '@animespace/core';

import './plan.d';

import { registerCli } from './cli';
import { ANIMEGARDEN, DOT } from './constant';
import { DownloadProviders, makeClient } from './download';
import { generateDownloadTask, runDownloadTask } from './task';
import { clearAnimeResourcesCache, fetchAnimeResources, useResourcesCache } from './resources';
import { formatAnimeGardenSearchURL, printFansubs, printKeywords } from './format';
import { clearAnimeResourcesCache, fetchAnimeResources, useResourcesCache } from './resources';

export interface AnimeGardenOptions extends PluginEntry {
api?: string;

provider?: DownloadProviders;
}

Expand All @@ -42,6 +37,7 @@ const memoClient = memo(

export function AnimeGarden(options: AnimeGardenOptions): Plugin {
const provider = options.provider ?? 'webtorrent';
const config = { baseURL: options.api };
const getClient = (sys: AnimeSystem) => memoClient(provider, sys, options);

let shouldClearCache = false;
Expand Down Expand Up @@ -76,7 +72,8 @@ export function AnimeGarden(options: AnimeGardenOptions): Plugin {
const resource = await fetchResourceDetail(
ufetch,
'dmhy',
video.source.magnet.split('/').at(-1)!
video.source.magnet.split('/').at(-1)!,
{ baseURL: options.api }
);

try {
Expand Down Expand Up @@ -122,13 +119,13 @@ export function AnimeGarden(options: AnimeGardenOptions): Plugin {
},
refresh: {
async pre(system, options) {
const cache = await useResourcesCache(system);
const cache = await useResourcesCache(system, config);
if (options.filter !== undefined) {
cache.disable();
}
},
async post(system) {
const cache = await useResourcesCache(system);
const cache = await useResourcesCache(system, config);
cache.finalize();
useResourcesCache.clear();
},
Expand All @@ -146,7 +143,7 @@ export function AnimeGarden(options: AnimeGardenOptions): Plugin {
printFansubs(anime, logger);

const animegardenURL = formatAnimeGardenSearchURL(anime);
const resources = await fetchAnimeResources(system, anime).catch(() => undefined);
const resources = await fetchAnimeResources(system, anime, config).catch(() => undefined);
if (resources === undefined) {
logger.log(
`${lightRed('Found resources')} ${dim('from')} ${link(
Expand Down
26 changes: 19 additions & 7 deletions packages/animegarden/src/resources/cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Path } from 'breadfs';
import { memoAsync } from 'memofunc';
import { fetchResources, makeResourcesFilter, type Resource } from 'animegarden';
import {
fetchResources,
FetchResourcesOptions,
makeResourcesFilter,
type Resource
} from 'animegarden';

import { Anime, AnimeSystem, ufetch } from '@animespace/core';

Expand All @@ -17,6 +22,8 @@ type AnimeCacheSchema = Required<
export class ResourcesCache {
private readonly system: AnimeSystem;

private readonly options: FetchResourcesOptions;

private readonly root: Path;

private readonly animeRoot: Path;
Expand All @@ -31,8 +38,9 @@ export class ResourcesCache {

private recentResponse: Awaited<ReturnType<typeof fetchResources>> | undefined = undefined;

constructor(system: AnimeSystem) {
constructor(system: AnimeSystem, options: Pick<FetchResourcesOptions, 'baseURL'> = {}) {
this.system = system;
this.options = options;
this.root = system.space.storage.cache.join('animegarden');
this.animeRoot = this.root.join('anime');
this.resourcesRoot = this.root.join('resources');
Expand Down Expand Up @@ -85,6 +93,7 @@ export class ResourcesCache {

const ac = new AbortController();
const resp = await fetchResources(ufetch, {
baseURL: this.options.baseURL,
type: '動畫',
retry: 10,
count: -1,
Expand Down Expand Up @@ -203,6 +212,7 @@ export class ResourcesCache {
try {
const ac = new AbortController();
const resp = await fetchResources(ufetch, {
baseURL: this.options.baseURL,
type: '動畫',
after: anime.plan.date,
include: anime.plan.keywords.include,
Expand Down Expand Up @@ -233,8 +243,10 @@ export async function clearAnimeResourcesCache(system: AnimeSystem, anime: Anime
await cache.clearAnimeResources(anime);
}

export const useResourcesCache = memoAsync(async (system: AnimeSystem) => {
const cache = new ResourcesCache(system);
await cache.initialize();
return cache;
});
export const useResourcesCache = memoAsync(
async (system: AnimeSystem, options?: Pick<FetchResourcesOptions, 'baseURL'>) => {
const cache = new ResourcesCache(system, options);
await cache.initialize();
return cache;
}
);
8 changes: 7 additions & 1 deletion packages/animegarden/src/resources/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import type { FetchResourcesOptions } from 'animegarden';

import { Anime, AnimeSystem } from '@animespace/core';

import { useResourcesCache } from './cache';

export { clearAnimeResourcesCache, useResourcesCache } from './cache';

export async function fetchAnimeResources(system: AnimeSystem, anime: Anime) {
export async function fetchAnimeResources(
system: AnimeSystem,
anime: Anime,
options?: Pick<FetchResourcesOptions, 'baseURL'>
) {
const cache = await useResourcesCache(system);
try {
return await cache.load(anime);
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/anime/episode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export function parseEpisode<T extends Partial<FormatOptions> = Partial<FormatOp
if (!info) return undefined;

const metadata =
options?.metadata instanceof Function ? options.metadata(info) : options.metadata ?? undefined;
options?.metadata instanceof Function
? options.metadata(info)
: (options.metadata ?? undefined);

if (anime.plan.type === '番剧') {
const resolvedEpisode = anime.resolveEpisode(info.episode.number, metadata?.fansub);
Expand Down

0 comments on commit a5d0145

Please sign in to comment.