File size: 5,123 Bytes
0bfe2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import {
  Addon,
  Option,
  UserData,
  ParsedStream,
  Stream,
  AIOStream,
} from '../db';
import { Preset, baseOptions } from './preset';
import { Env, formatZodError, RESOURCES } from '../utils';
import { StreamParser } from '../parser';
import { createLogger } from '../utils';

const logger = createLogger('parser');

class AIOStreamsStreamParser extends StreamParser {
  override parse(stream: Stream): ParsedStream {
    const aioStream = stream as AIOStream;
    const parsed = AIOStream.safeParse(aioStream);
    if (!parsed.success) {
      logger.error(
        `Stream from AIOStream was not detected as a valid stream: ${formatZodError(parsed.error)}`
      );
      throw new Error('Invalid stream');
    }
    const addonName = this.addon?.name?.trim();
    return {
      id: this.getRandomId(),
      addon: {
        ...this.addon,
        name: addonName
          ? `${addonName} | ${aioStream.streamData?.addon ?? ''}`
          : (aioStream.streamData?.addon ?? ''),
      },
      error: aioStream.streamData?.error,
      type: aioStream.streamData?.type ?? 'http',
      url: aioStream.url ?? undefined,
      externalUrl: aioStream.externalUrl ?? undefined,
      ytId: aioStream.ytId ?? undefined,
      requestHeaders: aioStream.behaviorHints?.proxyHeaders?.request,
      responseHeaders: aioStream.behaviorHints?.proxyHeaders?.response,
      notWebReady: aioStream.behaviorHints?.notWebReady ?? undefined,
      videoHash: aioStream.behaviorHints?.videoHash ?? undefined,
      filename: aioStream.streamData?.filename,
      folderName: aioStream.streamData?.folderName,
      size: aioStream.streamData?.size,
      folderSize: aioStream.streamData?.folderSize,
      indexer: aioStream.streamData?.indexer,
      service: aioStream.streamData?.service,
      duration: aioStream.streamData?.duration,
      library: aioStream.streamData?.library ?? false,
      age: aioStream.streamData?.age,
      message: aioStream.streamData?.message,
      torrent: aioStream.streamData?.torrent,
      parsedFile: aioStream.streamData?.parsedFile,
      keywordMatched: aioStream.streamData?.keywordMatched,
      streamExpressionMatched: aioStream.streamData?.streamExpressionMatched,
      regexMatched: aioStream.streamData?.regexMatched,
      originalName: aioStream.name ?? undefined,
      originalDescription: (aioStream.description || stream.title) ?? undefined,
    };
  }
}

export class AIOStreamsPreset extends Preset {
  static override getParser(): typeof StreamParser {
    return AIOStreamsStreamParser;
  }

  static override get METADATA() {
    const options: Option[] = [
      {
        id: 'name',
        name: 'Name',
        description:
          "What to call this addon. Leave empty if you don't want to include the name of this addon in the stream results.",
        type: 'string',
        required: true,
        default: 'AIOStreams',
      },
      {
        id: 'manifestUrl',
        name: 'Manifest URL',
        description: 'Provide the Manifest URL for this AIOStreams addon.',
        type: 'url',
        required: true,
      },
      {
        id: 'timeout',
        name: 'Timeout',
        description: 'The timeout for this addon',
        type: 'number',
        default: Env.DEFAULT_TIMEOUT,
        constraints: {
          min: Env.MIN_TIMEOUT,
          max: Env.MAX_TIMEOUT,
        },
      },
      {
        id: 'resources',
        name: 'Resources',
        description:
          'Optionally override the resources that are fetched from this addon ',
        type: 'multi-select',
        required: false,
        default: undefined,
        options: RESOURCES.map((resource) => ({
          label: resource,
          value: resource,
        })),
      },
    ];

    return {
      ID: 'aiostreams',
      NAME: 'AIOStreams',
      LOGO: 'https://raw.githubusercontent.com/Viren070/AIOStreams/refs/heads/main/packages/frontend/public/assets/logo.png',
      URL: '',
      TIMEOUT: Env.DEFAULT_TIMEOUT,
      USER_AGENT: Env.DEFAULT_USER_AGENT,
      SUPPORTED_SERVICES: [],
      DESCRIPTION: 'Wrap AIOStreams within AIOStreams!',
      OPTIONS: options,
      SUPPORTED_STREAM_TYPES: [],
      SUPPORTED_RESOURCES: [],
    };
  }

  static async generateAddons(
    userData: UserData,
    options: Record<string, any>
  ): Promise<Addon[]> {
    if (!options.manifestUrl.endsWith('/manifest.json')) {
      throw new Error(
        `${options.name} has an invalid Manifest URL. It must be a valid link to a manifest.json`
      );
    }
    return [this.generateAddon(userData, options)];
  }

  private static generateAddon(
    userData: UserData,
    options: Record<string, any>
  ): Addon {
    return {
      name: options.name || this.METADATA.NAME,
      manifestUrl: options.manifestUrl.replace('stremio://', 'https://'),
      enabled: true,
      library: false,
      resources: options.resources || undefined,
      timeout: options.timeout || this.METADATA.TIMEOUT,
      presetType: this.METADATA.ID,
      presetInstanceId: '',
      headers: {
        'User-Agent': this.METADATA.USER_AGENT,
      },
    };
  }
}