feat: add peertube service

pull/476/head
Dario Vladović 2021-01-10 17:16:11 +00:00 zatwierdzone przez GitHub
rodzic cdaefa14ae
commit 41c8376b35
2 zmienionych plików z 101 dodań i 0 usunięć

99
api/peertube.ts 100644
Wyświetl plik

@ -0,0 +1,99 @@
import got from '../libs/got'
import { millify } from '../libs/utils'
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
const BRAND_COLOR = 'F16805'
export default createBadgenHandler({
title: 'PeerTube',
examples: {
'/peertube/framatube.org/comments/9c9de5e8-0a1e-484a-b099-e80766180a6d': 'comments',
'/peertube/framatube.org/votes/9c9de5e8-0a1e-484a-b099-e80766180a6d': 'votes (combined)',
'/peertube/framatube.org/votes/9c9de5e8-0a1e-484a-b099-e80766180a6d/likes': 'votes (likes)',
'/peertube/framatube.org/votes/9c9de5e8-0a1e-484a-b099-e80766180a6d/dislikes': 'votes (dislikes)',
'/peertube/framatube.org/views/9c9de5e8-0a1e-484a-b099-e80766180a6d': 'views',
'/peertube/framatube.org/followers/framasoft': 'followers (account)',
'/peertube/framatube.org/followers/framasoft/framablog.audio': 'followers (channel)',
},
handlers: {
'/peertube/:instance/:topic<comments|views>/:video-id': handler,
'/peertube/:instance/:topic<votes>/:video-id/:format?<likes|dislikes>': votesHandler,
'/peertube/:instance/:topic<followers>/:account/:channel?': followersHandler
}
})
async function handler ({ instance, topic, 'video-id': videoId }: PathArgs) {
const client = createClient(instance)
switch (topic) {
case 'comments': {
const { total } = await client.get(`/videos/${videoId}/comment-threads`).json()
return {
subject: 'comments',
status: millify(total),
color: BRAND_COLOR
}
}
case 'views': {
const { views } = await client.get(`/videos/${videoId}`).json()
return {
subject: 'views',
status: millify(views),
color: BRAND_COLOR
}
}
}
}
async function votesHandler ({ instance, 'video-id': videoId, format }: PathArgs) {
const client = createClient(instance)
const { likes, dislikes } = await client.get(`/videos/${videoId}`).json()
switch (format) {
case 'likes': {
return {
subject: 'likes',
status: millify(likes),
color: BRAND_COLOR
}
}
case 'dislikes': {
return {
subject: 'dislikes',
status: millify(dislikes),
color: BRAND_COLOR
}
}
}
return {
subject: 'votes',
status: `${millify(likes)} 👍 ${millify(dislikes)} 👎`,
color: BRAND_COLOR
}
}
async function followersHandler ({ instance, account, channel }: PathArgs) {
const client = createClient(instance)
if (channel) {
const { followersCount } = await client.get(`/video-channels/${channel}`).json()
return {
subject: 'followers',
status: millify(followersCount),
color: BRAND_COLOR
}
}
const { followersCount } = await client.get(`/accounts/${account}`).json()
return {
subject: 'followers',
status: millify(followersCount),
color: BRAND_COLOR
}
}
function createClient (instance: string) {
const prefixUrl = `https://${instance}/api/v1`
return got.extend({ prefixUrl })
}

Wyświetl plik

@ -55,6 +55,8 @@ export const liveBadgeList = [
'xo',
'badgesize',
'jsdelivr',
// social
'peertube',
// utilities
'opencollective',
'keybase',