64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { AbstractScheduler } from './abstract-scheduler'
|
|
import { INDEXER_COUNT, SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
|
|
import { doRequest } from '../../helpers/requests'
|
|
import { logger } from '../../helpers/logger'
|
|
import { ResultList } from '../../../PeerTube/shared/models/result-list.model'
|
|
import { Video } from '../../../PeerTube/shared/models/videos/video.model'
|
|
import { indexVideos, refreshVideosIndex } from '../../helpers/elastic-search'
|
|
import { IndexableVideo } from '../../types/video.model'
|
|
import { inspect } from 'util'
|
|
|
|
export class VideosIndexer extends AbstractScheduler {
|
|
|
|
private static instance: AbstractScheduler
|
|
|
|
protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.videosIndexer
|
|
|
|
private constructor () {
|
|
super()
|
|
}
|
|
|
|
protected async internalExecute () {
|
|
return this.indexVideos()
|
|
}
|
|
|
|
private async indexVideos () {
|
|
const instances = [ 'peertube.cpy.re' ]
|
|
|
|
for (const instance of instances) {
|
|
try {
|
|
const videos = await this.getVideos(instance)
|
|
|
|
await indexVideos(videos)
|
|
|
|
logger.info('Added video data from %s.', instance)
|
|
} catch (err) {
|
|
console.error(inspect(err, { depth: 10 }))
|
|
logger.warn('Cannot index videos from %s.', instance, { err })
|
|
}
|
|
}
|
|
|
|
await refreshVideosIndex()
|
|
}
|
|
|
|
private async getVideos (host: string): Promise<IndexableVideo[]> {
|
|
const url = 'https://' + host + '/api/v1/videos'
|
|
|
|
const res = await doRequest<ResultList<Video>>({
|
|
uri: url,
|
|
qs: {
|
|
start: 0,
|
|
filter: 'local',
|
|
count: INDEXER_COUNT.VIDEOS
|
|
},
|
|
json: true
|
|
})
|
|
|
|
return res.body.data.map(v => Object.assign(v, { elasticSearchId: host + v.id, host }))
|
|
}
|
|
|
|
static get Instance () {
|
|
return this.instance || (this.instance = new this())
|
|
}
|
|
}
|