Add videosCount to channels

This commit is contained in:
Chocobozzz 2021-11-24 11:36:42 +01:00
parent c19de268fd
commit 4f8f324438
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 28 additions and 2 deletions

View File

@ -155,6 +155,7 @@ function formatChannelForDB (c: IndexableChannel): DBChannel {
description: c.description,
support: c.support,
videosCount: c.videosCount,
handle: `${c.name}@${c.host}`,
@ -182,6 +183,10 @@ function buildChannelsMapping () {
const base = buildChannelOrAccountCommonMapping()
Object.assign(base, {
videosCount: {
type: 'long'
},
support: {
type: 'keyword'
},

View File

@ -25,7 +25,27 @@ async function getChannel (host: string, name: string): Promise<IndexableChannel
json: true
}, REQUESTS.MAX_RETRIES, REQUESTS.WAIT)
return prepareChannelForDB(res.body, host)
const videosCount = await getChannelVideosCount(host, name)
return prepareChannelForDB(res.body, host, videosCount)
}
async function getChannelVideosCount (host: string, name: string): Promise<number> {
const url = 'https://' + host + '/api/v1/video-channels/' + name + '/videos'
const res = await doRequestWithRetries<ResultList<Video>>({
uri: url,
qs: {
start: 0,
count: 0
},
json: true
}, REQUESTS.MAX_RETRIES, REQUESTS.WAIT)
const total = res.body?.total
if (!total || typeof total !== 'number') return 0
return total
}
async function getVideos (host: string, start: number): Promise<IndexableVideo[]> {
@ -78,10 +98,11 @@ function prepareVideoForDB <T extends Video> (video: T, host: string): T & Index
})
}
function prepareChannelForDB (channel: VideoChannel, host: string): IndexableChannel {
function prepareChannelForDB (channel: VideoChannel, host: string, videosCount: number): IndexableChannel {
return Object.assign(channel, {
elasticSearchId: host + channel.id,
host,
videosCount,
url: 'https://' + host + '/video-channels/' + channel.name
})
}