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

export class Easynews extends BaseWrapper {
  constructor(
    configString: string | null,
    overrideUrl: string | null,
    addonName: string = 'Easynews',
    addonId: string,
    userConfig: Config,
    indexerTimeout?: number
  ) {
    let url = overrideUrl
      ? overrideUrl
      : Settings.EASYNEWS_URL + (configString ? configString + '/' : '');

    super(
      addonName,
      url,
      addonId,
      userConfig,
      indexerTimeout || Settings.DEFAULT_EASYNEWS_TIMEOUT,
      Settings.DEFAULT_EASYNEWS_USER_AGENT
        ? { 'User-Agent': Settings.DEFAULT_EASYNEWS_USER_AGENT }
        : undefined
    );
  }

  protected parseStream(stream: Stream): ParseResult {
    const parseResult = super.parseStream(stream);
    if (parseResult.type !== 'error') {
      parseResult.result.type = 'usenet';
    }
    return parseResult;
  }
}

const getEasynewsConfigString = (username: string, password: string) => {
  return `%7B%22username%22%3A%22${encodeURIComponent(username)}%22%2C%22password%22%3A%22${encodeURIComponent(password)}%22%7D`;
};

export async function getEasynewsStreams(
  config: Config,
  easynewsOptions: {
    overrideName?: string;
    overrideUrl?: string;
    indexerTimeout?: string;
  },
  streamRequest: StreamRequest,
  addonId: string
): Promise<{
  addonStreams: ParsedStream[];
  addonErrors: string[];
}> {
  // look for the 'easynews' id in the services array and destructure the username and password
  // if we cant find it, throw an error
  const easynewsService = serviceDetails.find(
    (service) => service.id === 'easynews'
  );
  if (!easynewsService) {
    throw new Error('Easynews service not found');
  }

  // check for the presence of the username and password in teh easynewsService.credentials object
  // if not found, throw an error
  const credentails = config.services.find(
    (service) => service.id === 'easynews'
  )?.credentials;
  if (!credentails || !credentails.username || !credentails.password) {
    throw new Error('Easynews credentials not found');
  }

  const easynewsConfigString = getEasynewsConfigString(
    credentails.username,
    credentails.password
  );

  const easynews = new Easynews(
    easynewsConfigString,
    easynewsOptions.overrideUrl ?? null,
    easynewsOptions.overrideName,
    addonId,
    config,
    easynewsOptions.indexerTimeout
      ? parseInt(easynewsOptions.indexerTimeout)
      : undefined
  );

  return await easynews.getParsedStreams(streamRequest);
}