File size: 3,364 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
import { BaseWrapper } from './base';
import {
  Config,
  ParsedNameData,
  ParsedStream,
  ParseResult,
  Stream,
  StreamRequest,
} from '@aiostreams/types';
import { parseFilename } from '@aiostreams/parser';
import { createLogger, Settings } from '@aiostreams/utils';

const logger = createLogger('wrappers');

interface TorboxStream extends Stream {
  name: string;
  url: string;
  description: string;
  hash?: string;
  is_cached?: boolean;
  size?: number;
  magnet?: string;
  nzb?: string;
  seeders?: number;
  peers?: number;
  quality?: string;
  resolution?: string;
  language?: string;
  type?: string;
  adult?: boolean;
  user_search?: boolean;
}

export class Torbox extends BaseWrapper {
  constructor(
    apiKey: string,
    addonName: string = 'TorBox',
    addonId: string,
    userConfig: Config,
    indexerTimeout?: number
  ) {
    super(
      addonName,
      Settings.TORBOX_STREMIO_URL + apiKey + '/',
      addonId,
      userConfig,
      indexerTimeout || Settings.DEFAULT_TORBOX_TIMEOUT,
      Settings.DEFAULT_TORBOX_USER_AGENT
        ? { 'User-Agent': Settings.DEFAULT_TORBOX_USER_AGENT }
        : undefined
    );
  }

  protected parseStream(stream: TorboxStream): ParseResult {
    const filename =
      stream.behaviorHints?.filename ||
      stream.description.match(/Name:\s*([^\n]+)/)?.[1];
    let message = undefined;
    if (stream.description.includes('Click play to start streaming')) {
      message = stream.description;
    }
    const parsedFilename: ParsedNameData = parseFilename(
      filename || stream.description
    );

    const size = stream.behaviorHints?.videoSize || stream.size;
    const seeders =
      stream.seeders && stream.seeders !== -1 ? stream.seeders : undefined;
    const age =
      stream.description.match(/\|\sAge:\s([0-9]+[dmyh])/)?.[1] || undefined;
    const source =
      stream.description.match(/Source:\s*([^\n]+)/)?.[1] || undefined;
    const infoHash =
      stream.hash ||
      stream.magnet?.match(/btih:([0-9a-fA-F]{40,})/)?.[1] ||
      undefined;
    const personal = stream.name.includes('Your Media');
    const provider = {
      id: 'torbox',
      cached: stream.is_cached !== undefined ? stream.is_cached : true,
    };

    const parsedStream: ParseResult = this.createParsedResult({
      parsedInfo: parsedFilename,
      stream,
      filename,
      size,
      provider,
      seeders,
      usenetAge: age,
      indexer: source,
      personal,
      infoHash,
      message,
    });

    return parsedStream;
  }
}

export async function getTorboxStreams(
  config: Config,
  torboxOptions: {
    indexerTimeout?: string;
    overrideName?: string;
  },
  streamRequest: StreamRequest,
  addonId: string
): Promise<{ addonStreams: ParsedStream[]; addonErrors: string[] }> {
  const torboxService = config.services.find(
    (service) => service.id === 'torbox'
  );
  if (!torboxService) {
    throw new Error('Torbox service not found');
  }

  const torboxApiKey = torboxService.credentials.apiKey;
  if (!torboxApiKey) {
    throw new Error('Torbox API key not found');
  }

  const torbox = new Torbox(
    torboxApiKey,
    torboxOptions.overrideName,
    addonId,
    config,
    torboxOptions.indexerTimeout
      ? parseInt(torboxOptions.indexerTimeout)
      : undefined
  );
  return await torbox.getParsedStreams(streamRequest);
}