-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
getTopClips.ts
40 lines (35 loc) · 1.33 KB
/
getTopClips.ts
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
import { DAY } from '@sogebot/ui-helpers/constants';
import { HelixClip } from '../../../../node_modules/@twurple/api/lib';
import client from '../api/client';
import { getGameNameFromId } from './getGameNameFromId';
import { streamStatusChangeSince } from '~/helpers/api';
import { error } from '~/helpers/log';
import { variables } from '~/watchers';
export async function getTopClips (opts: any) {
const channelId = variables.get('services.twitch.channelId') as string;
try {
const period = {
startDate: opts.period === 'stream'
? (new Date(streamStatusChangeSince.value)).toISOString()
: new Date(Date.now() - opts.days * DAY).toISOString(),
endDate: (new Date()).toISOString(),
};
const clientBot = await client('bot');
const getClipsForBroadcaster = await clientBot.clips.getClipsForBroadcasterPaginated(channelId, { ...period }).getAll();
// get mp4 from thumbnail
const clips: (Partial<HelixClip> & { mp4: string; game: string | null })[] = [];
for (const c of getClipsForBroadcaster) {
clips.push({
...c,
mp4: c.thumbnailUrl.replace('-preview-480x272.jpg', '.mp4'),
game: await getGameNameFromId(Number(c.gameId)),
});
}
return clips;
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
}
}
return [];
}