sepia-search-motore-di-rice.../server/lib/peertube-instance.ts

61 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-02-19 15:39:35 +01:00
import { IndexableVideo } from '../types/video.model'
import { doRequest } from '../helpers/requests'
import { ResultList, Video, VideoChannel, VideoDetails } from '@shared/models'
import { IndexableChannel } from '../types/channel.model'
import { INDEXER_COUNT } from '../initializers/constants'
import { IndexableDoc } from '../types/elastic-search.model'
async function getVideo (host: string, uuid: string): Promise<IndexableVideo> {
const url = 'https://' + host + '/api/v1/videos/' + uuid
const res = await doRequest<VideoDetails>({
uri: url,
json: true
})
return prepareVideoForDB(res.body, host)
}
async function getChannel (host: string, name: string): Promise<IndexableChannel> {
const url = 'https://' + host + '/api/v1/video-channels/' + name
const res = await doRequest<VideoChannel>({
uri: url,
json: true
})
return prepareChannelForDB(res.body, host)
}
async function getVideos (host: string, start: number): Promise<IndexableVideo[]> {
const url = 'https://' + host + '/api/v1/videos'
const res = await doRequest<ResultList<Video>>({
uri: url,
qs: {
start,
filter: 'local',
skipCount: true,
count: INDEXER_COUNT.VIDEOS
},
json: true
})
return res.body.data.map(v => prepareVideoForDB(v, host))
}
function prepareVideoForDB <T extends Video> (video: T, host: string): T & IndexableDoc {
return Object.assign(video, { elasticSearchId: host + video.id, host })
}
function prepareChannelForDB <T extends VideoChannel> (channel: T, host: string): T & IndexableDoc {
return Object.assign(channel, { elasticSearchId: host + channel.id, host })
}
export {
getVideo,
getChannel,
getVideos,
prepareChannelForDB
}