2020-02-19 15:39:35 +01:00
|
|
|
import { ResultList, Video, VideoChannel, VideoDetails } from '@shared/models'
|
2020-09-02 15:22:10 +02:00
|
|
|
import { doRequestWithRetries } from '../helpers/requests'
|
2020-03-04 15:32:39 +01:00
|
|
|
import { INDEXER_COUNT, REQUESTS } from '../initializers/constants'
|
2020-09-02 15:22:10 +02:00
|
|
|
import { IndexableChannel } from '../types/channel.model'
|
2020-02-19 15:39:35 +01:00
|
|
|
import { IndexableDoc } from '../types/elastic-search.model'
|
2020-09-02 15:22:10 +02:00
|
|
|
import { IndexableVideo } from '../types/video.model'
|
2020-02-19 15:39:35 +01:00
|
|
|
|
|
|
|
async function getVideo (host: string, uuid: string): Promise<IndexableVideo> {
|
|
|
|
const url = 'https://' + host + '/api/v1/videos/' + uuid
|
|
|
|
|
2020-03-04 15:32:39 +01:00
|
|
|
const res = await doRequestWithRetries<VideoDetails>({
|
2020-02-19 15:39:35 +01:00
|
|
|
uri: url,
|
|
|
|
json: true
|
2020-03-04 15:32:39 +01:00
|
|
|
}, REQUESTS.MAX_RETRIES, REQUESTS.WAIT)
|
2020-02-19 15:39:35 +01:00
|
|
|
|
|
|
|
return prepareVideoForDB(res.body, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getChannel (host: string, name: string): Promise<IndexableChannel> {
|
|
|
|
const url = 'https://' + host + '/api/v1/video-channels/' + name
|
|
|
|
|
2020-03-04 15:32:39 +01:00
|
|
|
const res = await doRequestWithRetries<VideoChannel>({
|
2020-02-19 15:39:35 +01:00
|
|
|
uri: url,
|
|
|
|
json: true
|
2020-03-04 15:32:39 +01:00
|
|
|
}, REQUESTS.MAX_RETRIES, REQUESTS.WAIT)
|
2020-02-19 15:39:35 +01:00
|
|
|
|
|
|
|
return prepareChannelForDB(res.body, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getVideos (host: string, start: number): Promise<IndexableVideo[]> {
|
|
|
|
const url = 'https://' + host + '/api/v1/videos'
|
|
|
|
|
2020-03-04 15:32:39 +01:00
|
|
|
const res = await doRequestWithRetries<ResultList<Video>>({
|
2020-02-19 15:39:35 +01:00
|
|
|
uri: url,
|
|
|
|
qs: {
|
|
|
|
start,
|
|
|
|
filter: 'local',
|
2020-06-12 16:08:43 +02:00
|
|
|
nsfw: 'both',
|
2020-02-19 15:39:35 +01:00
|
|
|
skipCount: true,
|
|
|
|
count: INDEXER_COUNT.VIDEOS
|
|
|
|
},
|
|
|
|
json: true
|
2020-03-04 15:32:39 +01:00
|
|
|
}, REQUESTS.MAX_RETRIES, REQUESTS.WAIT)
|
2020-02-19 15:39:35 +01:00
|
|
|
|
2020-06-08 15:50:20 +02:00
|
|
|
if (!res.body || !Array.isArray(res.body.data)) {
|
|
|
|
throw new Error('Invalid video data from ' + url)
|
|
|
|
}
|
|
|
|
|
2020-02-19 15:39:35 +01:00
|
|
|
return res.body.data.map(v => prepareVideoForDB(v, host))
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareVideoForDB <T extends Video> (video: T, host: string): T & IndexableDoc {
|
2020-06-05 14:37:39 +02:00
|
|
|
return Object.assign(video, {
|
|
|
|
elasticSearchId: host + video.id,
|
|
|
|
host,
|
|
|
|
url: 'https://' + host + '/videos/watch/' + video.uuid
|
|
|
|
})
|
2020-02-19 15:39:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function prepareChannelForDB <T extends VideoChannel> (channel: T, host: string): T & IndexableDoc {
|
2020-06-05 14:37:39 +02:00
|
|
|
return Object.assign(channel, {
|
|
|
|
elasticSearchId: host + channel.id,
|
|
|
|
host,
|
|
|
|
url: 'https://' + host + '/video-channels/' + channel.name
|
|
|
|
})
|
2020-02-19 15:39:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
getVideo,
|
|
|
|
getChannel,
|
|
|
|
getVideos,
|
|
|
|
prepareChannelForDB
|
|
|
|
}
|