1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-09-20 03:07:28 +00:00
cloudtube/api/utils/youtube.js
2020-09-01 01:40:34 +12:00

27 lines
939 B
JavaScript

const fetch = require("node-fetch")
const db = require("./db")
async function fetchChannel(ucid, instance) {
if (!instance) throw new Error("No instance parameter provided")
// fetch
const channel = await fetch(`${instance}/api/v1/channels/${ucid}`).then(res => res.json())
// update database
const bestIcon = channel.authorThumbnails.slice(-1)[0]
const iconURL = bestIcon ? bestIcon.url : null
db.prepare("REPLACE INTO Channels (ucid, name, icon_url) VALUES (?, ?, ?)").run([channel.authorId, channel.author, iconURL])
// return
return channel
}
function fetchChannelLatest(ucid) {
return fetch(`http://localhost:3000/api/v1/channels/${ucid}/latest`).then(res => res.json()).then(root => {
root.forEach(video => {
video.descriptionHtml = video.descriptionHtml.replace(/<a /g, '<a tabindex="-1" ')
})
return root
})
}
module.exports.fetchChannel = fetchChannel
module.exports.fetchChannelLatest = fetchChannelLatest