Skip to content

Commit

Permalink
feat!: add options to enable api return tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jul 25, 2024
1 parent 49192a2 commit 8a00cbc
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 85 deletions.
21 changes: 4 additions & 17 deletions packages/animegarden/src/garden/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Resource, ResourceDetail } from '../types';
import type { Resource, ResourceDetail, ResourceWithId } from '../types';

import { version } from '../../package.json';

Expand All @@ -24,14 +24,6 @@ export { AllFansubs, findFansub } from './constant';

export const DefaultBaseURL = 'https://garden.breadio.wiki/api/';

type ResourceWithId<T extends FetchResourcesOptions> = Resource & {
id: number;
} & {
magnet: T['magnet'] extends true ? string : string | null | undefined;
magnet2: T['magnet2'] extends true ? string : string | null | undefined;
magnetUser: T['magnetUser'] extends true ? string : string | null | undefined;
};

interface FetchResourcesResult<T extends FetchResourcesOptions> {
ok: boolean;
resources: ResourceWithId<T>[];
Expand All @@ -51,14 +43,9 @@ export async function fetchResources<T extends FetchResourcesOptions = FetchReso

const url = stringifySearchURL(baseURL, options);

if (options.magnet) {
url.searchParams.set('magnet', 'true');
}
if (options.magnet2) {
url.searchParams.set('magnet2', 'true');
}
if (options.magnetUser) {
url.searchParams.set('magnetUser', 'true');
// Enable tracker
if (options.tracker) {
url.searchParams.set('tracker', 'true');
}

if (options.count !== undefined && options.count !== null) {
Expand Down
18 changes: 2 additions & 16 deletions packages/animegarden/src/garden/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,11 @@ export interface FetchResourcesOptions extends FilterOptions {
signal?: AbortSignal;

/**
* Should return magnet href
* Should return tracker href
*
* @default false
*/
magnet?: boolean;

/**
* Should return magnet href2
*
* @default false
*/
magnet2?: boolean;

/**
* Should return magnet user
*
* @default false
*/
magnetUser?: boolean;
tracker?: boolean;

/**
* Request headers
Expand Down
24 changes: 13 additions & 11 deletions packages/animegarden/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { FetchResourcesOptions } from './garden/types';

export type ResourceType =
| '動畫'
| '季度全集'
Expand All @@ -19,13 +21,7 @@ export type ResourceType =
| '特攝'
| '其他';

export interface MagnetOptions {
magnet?: string;
magnet2?: string;
magnetUser?: string;
}

export interface Resource<T extends MagnetOptions = MagnetOptions> {
export interface Resource<T extends FetchResourcesOptions = FetchResourcesOptions> {
id?: number;

provider: string;
Expand All @@ -38,11 +34,13 @@ export interface Resource<T extends MagnetOptions = MagnetOptions> {

type: ResourceType;

magnet: T['magnet'] extends string ? string : string | null | undefined;
magnet: string;

magnet2: T['magnet2'] extends string ? string : string | null | undefined;

magnetUser: T['magnetUser'] extends string ? string : string | null | undefined;
tracker: T['tracker'] extends true
? string
: T['tracker'] extends false
? null | undefined
: string | null | undefined;

size: string;

Expand All @@ -63,6 +61,10 @@ export interface Resource<T extends MagnetOptions = MagnetOptions> {
fetchedAt: string;
}

export type ResourceWithId<T extends FetchResourcesOptions> = Resource<T> & {
id: number;
};

export type FetchedResource = Omit<Resource, 'fetchedAt'>;

export interface ResourceDetail {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export async function fetchResources(
) {
return await rawFetchResources(options.fetch ?? ofetch, {
baseURL,
tracker: true,
signal: options.signal,
magnet: true,
...filter
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/pages/feed.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const GET: APIRoute = async (context) => {
filter.data.map((f) =>
fetchResources(wfetch(locals?.worker), {
...f,
magnet: true,
tracker: true,
baseURL,
page: 1,
pageSize: 100
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/pages/resources/[page].astro
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
import { type ResourceType, findFansub, parseSearchURL } from 'animegarden';
import { parseSearchURL } from 'animegarden';
import Layout from '../../layouts/Layout.astro';
import FilterCard from '../../components/Filter.astro';
import Pagination from '../../components/Pagination.astro';
import ResourceTable from '../../components/ResourceTable.astro';
import ErrorNotification from '../../components/Error.astro';
import FilterCard from '../../components/Filter.astro';
import { generateFeed } from '../../logic/feed';
import { getRuntimeEnv } from '../../utils';
Expand Down
3 changes: 3 additions & 0 deletions packages/database/drizzle/0006_simple_dragon_man.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE "resources" ADD COLUMN "tracker" text;--> statement-breakpoint
ALTER TABLE "resources" DROP COLUMN IF EXISTS "magnet2";--> statement-breakpoint
ALTER TABLE "resources" DROP COLUMN IF EXISTS "magnet_user";
Loading

0 comments on commit 8a00cbc

Please sign in to comment.