File size: 2,992 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
import { parseFilename } from '@aiostreams/parser';
import {
  ParsedStream,
  Stream,
  Config,
  AddonDetail,
  ParsedNameData,
  StreamRequest,
  ParseResult,
} from '@aiostreams/types';
import { BaseWrapper } from './base';
import { addonDetails, Settings } from '@aiostreams/utils';
import { emojiToLanguage } from '@aiostreams/formatters';

export class DMMCast extends BaseWrapper {
  constructor(
    installationUrl: string,
    addonId: string,
    userConfig: Config,
    addonName: string = 'DMM Cast',
    indexerTimeout?: number
  ) {
    super(
      addonName,
      installationUrl,
      addonId,
      userConfig,
      indexerTimeout || Settings.DEFAULT_DMM_CAST_TIMEOUT,
      Settings.DEFAULT_DMM_CAST_USER_AGENT
        ? { 'User-Agent': Settings.DEFAULT_DMM_CAST_USER_AGENT }
        : undefined
    );
  }

  protected parseStream(stream: Stream): ParseResult {
    // the streams for DMM cast can be one of the following
    // 1:Cast - Cast a file inside a torrent
    // 2:Stream - Stream the latest link you casted
    // DMM Other - Filename can be split across multiple lines with πŸ“¦ {size} at last line
    // DMM Yours - Filename can be split across multiple lines with πŸ“¦ {size} at last line
    let message = '';
    let filename = stream.title
      ? stream.title
          .split('\n')
          .map((line) => line.replace(/-$/, ''))
          .filter((line) => !line.includes('πŸ“¦'))
          .join('')
      : stream.behaviorHints?.filename?.trim();
    if (!stream.title?.includes('πŸ“¦')) {
      filename = undefined;
      message = stream.title || '';
    }

    const parsedFilename: ParsedNameData = parseFilename(filename || '');
    const sizeInBytes = stream.title?.split('\n').pop()?.includes('πŸ“¦')
      ? this.extractSizeInBytes(stream.title.split('\n').pop()!, 1024)
      : 0;

    const parseResult: ParseResult = this.createParsedResult({
      parsedInfo: parsedFilename,
      stream,
      filename,
      size: sizeInBytes,
    });
    if (parseResult.type === 'stream') {
      parseResult.result.message = message;
    }
    return parseResult;
  }
}

export async function getDMMCastStreams(
  config: Config,
  dmmCastOptions: {
    installationUrl?: string;
    indexerTimeout?: string;
    overrideName?: string;
  },
  streamRequest: StreamRequest,
  addonId: string
): Promise<{
  addonStreams: ParsedStream[];
  addonErrors: string[];
}> {
  if (!dmmCastOptions.installationUrl) {
    throw new Error('DMM Cast installation URL is missing');
  } else if (
    dmmCastOptions.installationUrl.match(
      /\/api\/stremio\/.*\/manifest\.json$/
    ) === null
  ) {
    throw new Error('Invalid DMM Cast installation URL');
  }
  const dmmCast = new DMMCast(
    dmmCastOptions.installationUrl,
    addonId,
    config,
    dmmCastOptions.overrideName,
    dmmCastOptions.indexerTimeout
      ? parseInt(dmmCastOptions.indexerTimeout)
      : undefined
  );
  return await dmmCast.getParsedStreams(streamRequest);
}