File size: 4,740 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
161
162
163
164
165
166
167
import { Addon, Option, UserData, Resource, ParsedStream } from '../db';
import { baseOptions, Preset } from './preset';
import { Env } from '../utils';
import { constants, ServiceId } from '../utils';
import { StreamParser } from '../parser';
import { Stream } from '../db';

class TorboxStreamParser extends StreamParser {
  override getSeeders(
    stream: Stream,
    currentParsedStream: ParsedStream
  ): number | undefined {
    return (stream as any).seeders && (stream as any).seeders >= 0
      ? (stream as any).seeders
      : undefined;
  }
  override get ageRegex() {
    return /\|\sAge:\s([0-9]+[dmyh])/i;
  }
  override get indexerRegex() {
    return /Source:\s*([^\n]+)/;
  }
  override getInfoHash(
    stream: Stream,
    currentParsedStream: ParsedStream
  ): string | undefined {
    return (stream as any).hash;
  }
  override getInLibrary(
    stream: Stream,
    currentParsedStream: ParsedStream
  ): boolean {
    return (stream as any).is_your_media || stream.name?.includes('Your Media');
  }
  protected override getService(
    stream: Stream,
    currentParsedStream: ParsedStream
  ): ParsedStream['service'] | undefined {
    return {
      id: constants.TORBOX_SERVICE,
      cached: (stream as any).is_cached ?? true,
    };
  }

  protected override getMessage(
    stream: Stream,
    currentParsedStream: ParsedStream
  ): string | undefined {
    if (stream.description?.includes('Click play to start')) {
      currentParsedStream.filename = undefined;
      return 'Click play to start streaming your media';
    }
    return undefined;
  }

  protected override getStreamType(
    stream: Stream,
    service: ParsedStream['service'],
    currentParsedStream: ParsedStream
  ): ParsedStream['type'] {
    if ((stream as any).type === 'usenet') {
      return constants.USENET_STREAM_TYPE;
    }
    const type = stream.description?.match(/Type:\s*([^\n\s]+)/)?.[1];
    if (type) {
      if (type.includes('Torrent')) {
        return constants.DEBRID_STREAM_TYPE;
      } else if (type.includes('Usenet')) {
        return constants.USENET_STREAM_TYPE;
      }
    }
    return super.getStreamType(stream, service, currentParsedStream);
  }
}

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

  static override get METADATA() {
    const supportedServices: ServiceId[] = [constants.TORBOX_SERVICE];

    const supportedResources = [
      constants.STREAM_RESOURCE,
      constants.META_RESOURCE,
      constants.CATALOG_RESOURCE,
    ];

    const options: Option[] = [
      ...baseOptions('TorBox', supportedResources, Env.DEFAULT_TORBOX_TIMEOUT),
      {
        id: 'socials',
        name: '',
        description: '',
        type: 'socials',
        socials: [{ id: 'website', url: 'https://torbox.app' }],
      },
    ];

    return {
      ID: 'torbox',
      NAME: 'TorBox',
      LOGO: 'https://torbox.app/android-chrome-512x512.png',
      URL: Env.TORBOX_STREMIO_URL,
      TIMEOUT: Env.DEFAULT_TORBOX_TIMEOUT || Env.DEFAULT_TIMEOUT,
      USER_AGENT: Env.DEFAULT_TORBOX_USER_AGENT || Env.DEFAULT_USER_AGENT,
      SUPPORTED_SERVICES: supportedServices,
      DESCRIPTION:
        'Provides torrent and usenet streams for users of TorBox.app',
      OPTIONS: options,
      SUPPORTED_STREAM_TYPES: [
        constants.DEBRID_STREAM_TYPE,
        constants.USENET_STREAM_TYPE,
      ],
      SUPPORTED_RESOURCES: supportedResources,
    };
  }

  static async generateAddons(
    userData: UserData,
    options: Record<string, any>
  ): Promise<Addon[]> {
    return [this.generateAddon(userData, options)];
  }

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

  private static generateManifestUrl(
    userData: UserData,
    options: Record<string, any>
  ) {
    let url = options.url || this.METADATA.URL;
    if (url.endsWith('/manifest.json')) {
      return url;
    }
    url = url.replace(/\/$/, '');
    const torboxApiKey = this.getServiceCredential(
      constants.TORBOX_SERVICE,
      userData
    );
    if (!torboxApiKey) {
      throw new Error(
        `${this.METADATA.NAME} requires the Torbox service to be enabled.`
      );
    }

    return `${url}/${torboxApiKey}/manifest.json`;
  }
}