1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-11-24 08:27:30 +00:00
cloudtube/utils/youtube.js

61 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)
// handle the case where the channel has a known error
if (row && row.missing_reason) {
return {
error: true,
ucid,
row,
missing: true,
message: row.missing_reason
}
}
/** @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 channel has a newly discovered error
if (channel.error) {
const missingData = updateBadData(channel)
return {
error: true,
ucid,
row,
...missingData
}
}
// handle the case where the channel returns good data (this is the only remaining scenario)
updateGoodData(channel)
2020-08-30 13:54:59 +00:00
return channel
}
module.exports.fetchChannel = fetchChannel