File size: 4,979 Bytes
cc2caf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

class LoadBalancerAPI {
  constructor(baseURL) {
    this.baseURL = baseURL;
    this.cache = {
      filmStore: null,
      tvStore: null,
      allMovies: null,
      allSeries: null,
      movieMetadata: new Map(),
      seriesMetadata: new Map(),
    };
  }

  async getInstances() {
    return await this._get('/api/get/instances');
  }

  async getInstancesHealth() {
    return await this._get('/api/get/instances/health');
  }

  async getMovieByTitle(title) {
    return await this._get(`/api/get/movie/${encodeURIComponent(title)}`);
  }

  async getSeriesEpisode(title, season, episode) {
    return await this._get(`/api/get/series/${encodeURIComponent(title)}/${season}/${episode}`);
  }

  async getSeriesStore() {
    if (!this.cache.tvStore) {
      this.cache.tvStore = await this._get('/api/get/series/store');
    }
    return this.cache.tvStore || {};
  }

  async getMovieStore() {
    if (!this.cache.filmStore) {
      this.cache.filmStore = await this._get('/api/get/movie/store');
    }
    return this.cache.filmStore || {};
  }

  async getMovieMetadataByTitle(title) {
    if (!this.cache.movieMetadata.has(title)) {
      const metadata = await this._get(`/api/get/movie/metadata/${encodeURIComponent(title)}`);
      this.cache.movieMetadata.set(title, metadata);
    }
    return this.cache.movieMetadata.get(title);
  }

  async getMovieCard(title) {
    return await this._get(`/api/get/movie/card/${encodeURIComponent(title)}`);
  }

  async getSeriesMetadataByTitle(title) {
    if (!this.cache.seriesMetadata.has(title)) {
      const metadata = await this._get(`/api/get/series/metadata/${encodeURIComponent(title)}`);
      this.cache.seriesMetadata.set(title, metadata);
    }
    return this.cache.seriesMetadata.get(title);
  }

  async getSeriesCard(title) {
    return await this._get(`/api/get/series/card/${encodeURIComponent(title)}`);
  }

  async getSeasonMetadataByTitleAndSeason(title, season) {
    return await this._get(`/api/get/series/metadata/${encodeURIComponent(title)}/${encodeURIComponent(season)}`);
  }

  async getSeasonMetadataBySeriesId(series_id, season) {
    return await this._get(`/api/get/series/metadata/${series_id}/${season}`);
  }

  async getAllMovies() {
    if (!this.cache.allMovies) {
      this.cache.allMovies = await this._get('/api/get/movie/all');
    }
    return this.cache.allMovies;
  }

  async getAllSeriesShows() {
    if (!this.cache.allSeries) {
      this.cache.allSeries = await this._get('/api/get/series/all');
    }
    return this.cache.allSeries;
  }

  async getRecent(limit = 10) {
    return await this._get(`/api/get/recent?limit=${limit}`);
  }

  async getGenreCategories(mediaType) {
    const url = mediaType
      ? `/api/get/genre_categories?media_type=${encodeURIComponent(mediaType)}`
      : '/api/get/genre_categories';
    return await this._get(url);
  }
  
  async getGenreItems(genres, mediaType, limit = 5, page = 1) {
    if (!Array.isArray(genres)) {
      throw new Error("The 'genres' parameter must be an array.");
    }
    const params = new URLSearchParams();
    genres.forEach(genre => params.append('genre', genre));
    params.append('limit', limit);
    params.append('page', page);
    if (mediaType) {
      params.append('media_type', mediaType);
    }
    try {
      const response = await this._get(`/api/get/genre?${params.toString()}`);
      console.debug(response);
      return response;
    } catch (error) {
      console.debug("Error fetching genre items:", error);
      throw error;
    }
  }

  async getDownloadProgress(url) {
    return await this._getNoBase(url);
  }

  async _get(endpoint) {
    return await this._request(`${this.baseURL}${endpoint}`, { method: 'GET' });
  }

  async _getNoBase(url) {
    return await this._request(url, { method: 'GET' });
  }

  async _post(endpoint, body) {
    return await this._request(`${this.baseURL}${endpoint}`, {
      method: 'POST',
      body: JSON.stringify(body)
    });
  }

  async _request(url, options) {
    try {
      const response = await fetch(url, {
        headers: { 'Content-Type': 'application/json' },
        ...options,
      });
      console.log(`API Request: ${url} with options: ${JSON.stringify(options)}`);
      return await this._handleResponse(response);
    } catch (error) {
      console.debug(`Request error for ${url}:`, error);
      throw error;
    }
  }

  async _handleResponse(response) {
    if (!response.ok) {
      const errorDetails = await response.text();
      throw new Error(`HTTP Error ${response.status}: ${errorDetails}`);
    }
    try {
      return await response.json();
    } catch (error) {
      console.debug('Error parsing JSON response:', error);
      throw error;
    }
  }

  clearCache() {
    this.cache = {
      filmStore: null,
      tvStore: null,
      allMovies: null,
      allSeries: null,
      movieMetadata: new Map(),
      seriesMetadata: new Map(),
    };
  }
}

export { LoadBalancerAPI };