1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-12-22 13:07:00 +00:00
cloudtube/utils/youtube.js

52 lines
1.6 KiB
JavaScript
Raw Normal View History

const {request} = require("./request")
2020-08-30 13:54:59 +00:00
const db = require("./db")
async function fetchChannel(path, ucid, instance) {
2022-01-10 01:18:45 +00:00
function updateGoodData(channel) {
const bestIcon = channel.authorThumbnails.slice(-1)[0]
const iconURL = bestIcon ? bestIcon.url : null
db.prepare("REPLACE INTO Channels (ucid, name, icon_url, missing, missing_reason) VALUES (?, ?, ?, 0, NULL)").run(channel.authorId, channel.author, iconURL)
}
function updateBadData(channel) {
if (channel.identifier === "NOT_FOUND" || channel.identifier === "ACCOUNT_TERMINATED") {
db.prepare("UPDATE Channels SET missing = 1, missing_reason = ? WHERE ucid = ?").run(channel.error, channel.authorId)
return {
missing: true,
message: channel.error
}
} else {
return {
missing: false,
message: channel.error
}
}
}
2020-08-31 13:40:34 +00:00
if (!instance) throw new Error("No instance parameter provided")
2022-01-10 01:18:45 +00:00
const row = db.prepare("SELECT * FROM Channels WHERE ucid = ?").get(ucid)
// can branch on row.missing if needed, but account termination is not permanent,
// so we need to fetch new data from the web either way...
2022-01-10 01:18:45 +00:00
/** @type {any} */
const channel = await request(`${instance}/api/v1/channels/${ucid}?second__path=${path}`).then(res => res.json())
2022-01-10 01:18:45 +00:00
// handle the case where the just-fetched channel has an error
2022-01-10 01:18:45 +00:00
if (channel.error) {
const missingData = updateBadData(channel)
return {
error: true,
ucid,
row,
...missingData
}
}
// handle the case where the just-fetched channel does not have an error
2022-01-10 01:18:45 +00:00
updateGoodData(channel)
2020-08-30 13:54:59 +00:00
return channel
}
module.exports.fetchChannel = fetchChannel