-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathApiService.ts
More file actions
379 lines (316 loc) · 12.3 KB
/
ApiService.ts
File metadata and controls
379 lines (316 loc) · 12.3 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { isValid, parseISO } from 'date-fns';
import { injectable } from 'inversify';
import { getMediaStatusFromEventState } from '../utils/liveEvent';
import { createURL } from '../utils/urlFormatting';
import { getDataOrThrow } from '../utils/api';
import { filterMediaOffers } from '../utils/entitlements';
import { useConfigStore as ConfigStore } from '../stores/ConfigStore';
import type { GetPlaylistParams, Playlist, PlaylistItem } from '../../types/playlist';
import type { ContentList, GetContentSearchParams } from '../../types/content-list';
import type { AdSchedule } from '../../types/ad-schedule';
import type { EpisodeInSeries, EpisodesRes, EpisodesWithPagination, GetSeriesParams, Series } from '../../types/series';
import env from '../env';
import { logError } from '../logger';
// change the values below to change the property used to look up the alternate image
enum ImageProperty {
CARD = 'card',
BACKGROUND = 'background',
CHANNEL_LOGO = 'channel_logo',
}
const PAGE_LIMIT = 20;
@injectable()
export default class ApiService {
/**
* We use playlistLabel prop to define the label used for all media items inside.
* That way we can change the behavior of the same media items being in different playlists
*/
protected generateAlternateImageURL = ({ mediaId, label, playlistLabel }: { mediaId: string; label: string; playlistLabel?: string }) => {
const pathname = `/v2/media/${mediaId}/images/${playlistLabel || label}.webp`;
const url = createURL(`${env.APP_API_BASE_URL}${pathname}`, { poster_fallback: 1, fallback: playlistLabel ? label : null });
return url;
};
protected parseDate = (item: PlaylistItem, prop: string) => {
const date = item[prop] as string | undefined;
if (date && !isValid(new Date(date))) {
logError('ApiService', `Invalid "${prop}" date provided for the "${item.title}" media item`, { error: new Error('Invalid date') });
return undefined;
}
return date ? parseISO(date) : undefined;
};
protected getTranslatedFields = (item: PlaylistItem, language?: string) => {
if (!language) {
return item;
}
const defaultLanguage = env.APP_DEFAULT_LANGUAGE;
const transformedItem = { ...item };
if (language !== defaultLanguage) {
for (const [key, _] of Object.entries(transformedItem)) {
if (item[`${key}-${language}`]) {
transformedItem[key] = item[`${key}-${language}`];
}
}
}
return transformedItem;
};
/**
* Transform incoming content lists
*/
protected transformContentList = (contentList: ContentList, language: string): Playlist => {
const { list, id, ...rest } = contentList;
const playlist: Playlist = { ...rest, feedid: id, playlist: [] };
playlist.playlist = list.map((item) => {
const { custom_params, media_id, description, tags, ...rest } = item;
const playlistItem: PlaylistItem = {
feedid: contentList.id,
mediaid: media_id,
tags: tags.join(','),
description: description || '',
sources: [],
images: [],
image: '',
link: '',
pubdate: 0,
...rest,
...custom_params,
};
return this.transformMediaItem({ item: playlistItem, playlist, language });
});
return playlist;
};
/**
* Transform incoming playlists
*/
protected transformPlaylist = (playlist: Playlist, relatedMediaId?: string, language?: string) => {
playlist.playlist = playlist.playlist.map((item) => this.transformMediaItem({ item, playlist, language }));
// remove the related media item (when this is a recommendations playlist)
if (relatedMediaId) {
playlist.playlist = playlist.playlist.filter((item) => item.mediaid !== relatedMediaId);
}
return playlist;
};
/**
* Transform incoming media items
* - Parses productId into MediaOffer[] for all cleeng offers
*/
transformMediaItem = ({ item, playlist, language }: { item: PlaylistItem; playlist?: Playlist; language?: string }) => {
const config = ConfigStore.getState().config;
const offerKeys = Object.keys(config?.integrations)[0];
const playlistLabel = playlist?.imageLabel;
const mediaId = item.mediaid;
const translatedFields = this.getTranslatedFields(item, language);
const transformedMediaItem = {
...item,
...translatedFields,
cardImage: this.generateAlternateImageURL({ mediaId, label: ImageProperty.CARD, playlistLabel }),
channelLogoImage: this.generateAlternateImageURL({ mediaId, label: ImageProperty.CHANNEL_LOGO, playlistLabel }),
backgroundImage: this.generateAlternateImageURL({ mediaId, label: ImageProperty.BACKGROUND }),
mediaOffers: item.productIds ? filterMediaOffers(offerKeys, item.productIds) : undefined,
scheduledStart: this.parseDate(item, 'VCH.ScheduledStart'),
scheduledEnd: this.parseDate(item, 'VCH.ScheduledEnd'),
};
// add the media status to the media item after the transformation because the live media status depends on the scheduledStart and scheduledEnd
transformedMediaItem.mediaStatus = getMediaStatusFromEventState(transformedMediaItem);
return transformedMediaItem;
};
private transformEpisodes = (episodesRes: EpisodesRes, language?: string, seasonNumber?: number) => {
const { episodes, page, page_limit, total } = episodesRes;
// Adding images and keys for media items
return {
episodes: episodes
.filter((el) => el.media_item)
.map((el) => ({
...this.transformMediaItem({ item: el.media_item as PlaylistItem, language }),
seasonNumber: seasonNumber?.toString() || el.season_number?.toString() || '',
episodeNumber: String(el.episode_number),
})),
pagination: { page, page_limit, total },
};
};
/**
* Get watchlist by playlistId
*/
getMediaByWatchlist = async ({
playlistId,
mediaIds,
token,
language,
}: {
playlistId: string;
mediaIds: string[];
token?: string;
language?: string;
}): Promise<PlaylistItem[] | undefined> => {
if (!mediaIds?.length) {
return [];
}
const pathname = `/apps/watchlists/${playlistId}`;
const url = createURL(`${env.APP_API_BASE_URL}${pathname}`, { token, media_ids: mediaIds });
const response = await fetch(url);
const data = (await getDataOrThrow(response)) as Playlist;
if (!data) throw new Error(`The data was not found using the watchlist ${playlistId}`);
return (data.playlist || []).map((item) => this.transformMediaItem({ item, language }));
};
/**
* Get media by id
*/
getMediaById = async ({
id,
token,
drmPolicyId,
language,
}: {
id: string;
token?: string;
drmPolicyId?: string;
language?: string;
}): Promise<PlaylistItem | undefined> => {
const pathname = drmPolicyId ? `/v2/media/${id}/drm/${drmPolicyId}` : `/v2/media/${id}`;
const url = createURL(`${env.APP_API_BASE_URL}${pathname}`, { token });
const response = await fetch(url);
const data = (await getDataOrThrow(response)) as Playlist;
const mediaItem = data.playlist[0];
if (!mediaItem) throw new Error('MediaItem not found');
return this.transformMediaItem({ item: mediaItem, language });
};
/**
* Get media by id with passport
*/
getMediaByIdWithPassport = async ({
id,
siteId,
planId,
passport,
language,
}: {
id: string;
siteId: string;
planId: string;
passport: string;
language?: string;
}): Promise<PlaylistItem | undefined> => {
const pathname = `/v2/sites/${siteId}/media/${id}/playback.json`;
const url = createURL(`${env.APP_API_BASE_URL}${pathname}`, { passport, plan_id: planId });
const response = await fetch(url);
const data = (await getDataOrThrow(response)) as Playlist;
const mediaItem = data.playlist[0];
if (!mediaItem) throw new Error('MediaItem not found');
return this.transformMediaItem({ item: mediaItem, language });
};
/**
* Get series by id
* @param {string} id
* @param params
*/
getSeries = async (id: string, params: GetSeriesParams = {}): Promise<Series | undefined> => {
if (!id) {
throw new Error('Series ID is required');
}
const pathname = `/apps/series/${id}`;
const url = createURL(`${env.APP_API_BASE_URL}${pathname}`, params);
const response = await fetch(url);
return (await getDataOrThrow(response)) as Series;
};
/**
* Get all series for the given media_ids
*/
getSeriesByMediaIds = async (mediaIds: string[]): Promise<Record<string, EpisodeInSeries[]> | undefined> => {
const pathname = `/apps/series`;
const url = `${env.APP_API_BASE_URL}${pathname}?media_ids=${mediaIds.join(',')}`;
const response = await fetch(url);
return (await getDataOrThrow(response)) as Record<string, EpisodeInSeries[]>;
};
/**
* Get all episodes of the selected series (when no particular season is selected or when episodes are attached to series)
*/
getEpisodes = async ({
seriesId,
pageOffset,
pageLimit = PAGE_LIMIT,
afterId,
language,
}: {
seriesId: string | undefined;
pageOffset?: number;
pageLimit?: number;
afterId?: string;
language?: string;
}): Promise<EpisodesWithPagination> => {
if (!seriesId) {
throw new Error('Series ID is required');
}
const pathname = `/apps/series/${seriesId}/episodes`;
const url = createURL(`${env.APP_API_BASE_URL}${pathname}`, {
page_offset: pageOffset,
page_limit: pageLimit,
after_id: afterId,
});
const response = await fetch(url);
const episodesResponse = (await getDataOrThrow(response)) as EpisodesRes;
return this.transformEpisodes(episodesResponse, language);
};
/**
* Get season of the selected series
*/
getSeasonWithEpisodes = async ({
seriesId,
seasonNumber,
pageOffset,
pageLimit = PAGE_LIMIT,
language,
}: {
seriesId: string | undefined;
language: string;
seasonNumber: number;
pageOffset?: number;
pageLimit?: number;
}): Promise<EpisodesWithPagination> => {
if (!seriesId) {
throw new Error('Series ID is required');
}
const pathname = `/apps/series/${seriesId}/seasons/${seasonNumber}/episodes`;
const url = createURL(`${env.APP_API_BASE_URL}${pathname}`, { page_offset: pageOffset, page_limit: pageLimit });
const response = await fetch(url);
const episodesRes = (await getDataOrThrow(response)) as EpisodesRes;
return this.transformEpisodes(episodesRes, language, seasonNumber);
};
getAdSchedule = async (id: string | undefined | null): Promise<AdSchedule | undefined> => {
if (!id) {
throw new Error('Ad Schedule ID is required');
}
const url = env.APP_API_BASE_URL + `/v2/advertising/schedules/${id}.json`;
const response = await fetch(url, { credentials: 'omit' });
return (await getDataOrThrow(response)) as AdSchedule;
};
/**
* Get playlist by id
*/
getPlaylistById = async (id?: string, params: GetPlaylistParams = {}, language: string = env.APP_DEFAULT_LANGUAGE): Promise<Playlist | undefined> => {
if (!id) {
return undefined;
}
const pathname = `/v2/playlists/${id}`;
const url = createURL(`${env.APP_API_BASE_URL}${pathname}`, params);
const response = await fetch(url);
const data = (await getDataOrThrow(response)) as Playlist;
return this.transformPlaylist(data, params.related_media_id, language);
};
getContentList = async ({ id, siteId, language }: { id: string | undefined; siteId: string; language: string }): Promise<Playlist | undefined> => {
if (!id || !siteId) {
throw new Error('List ID and Site ID are required');
}
const pathname = `/v2/sites/${siteId}/content_lists/${id}`;
const url = createURL(`${env.APP_API_BASE_URL}${pathname}`, {});
const response = await fetch(url);
const data = (await getDataOrThrow(response)) as ContentList;
return this.transformContentList(data, language);
};
getContentSearch = async ({ siteId, params }: { siteId: string; params: GetContentSearchParams }) => {
const pathname = `/v2/sites/${siteId}/app_content/media/search`;
const url = createURL(`${env.APP_API_BASE_URL}${pathname}`, {
search_query: params.searchTerm,
});
const response = await fetch(url);
const data = (await getDataOrThrow(response)) as Playlist;
return this.transformPlaylist(data);
};
}