11import type { dbYouTube } from "../../types/database" ;
22
33import { pool } from "../database" ;
4+ import getSinglePlaylistAndReturnVideoId , {
5+ PlaylistType ,
6+ } from "../youtube/getSinglePlaylistAndReturnVideoId" ;
47
58export async function dbYouTubeGetAllChannelsToTrack ( ) : Promise <
69 dbYouTube [ ] | [ ]
@@ -20,3 +23,60 @@ export async function dbYouTubeGetAllChannelsToTrack(): Promise<
2023 return [ ] ;
2124 }
2225}
26+
27+ // These two functions are for checking/adding a new channel to the youtube table
28+ export async function checkIfChannelIsAlreadyTracked (
29+ channelId : string ,
30+ ) : Promise < boolean > {
31+ const query = `SELECT * FROM youtube WHERE youtube_channel_id = ?` ;
32+
33+ try {
34+ const client = await pool . connect ( ) ;
35+ const result = await client . query ( query , [ channelId ] ) ;
36+
37+ return result . rows . length > 0 ;
38+ } catch ( err ) {
39+ console . error ( "Error checking if channel is already tracked:" , err ) ;
40+
41+ return false ;
42+ }
43+ }
44+
45+ // Before adding a new channel, we need to get the latest video, short and stream ID
46+ export async function addNewChannelToTrack (
47+ channelId : string ,
48+ ) : Promise < boolean > {
49+ console . log ( "Adding channel to track:" , channelId ) ;
50+
51+ const longId = await getSinglePlaylistAndReturnVideoId (
52+ channelId ,
53+ PlaylistType . Video ,
54+ ) ;
55+ const shortId = await getSinglePlaylistAndReturnVideoId (
56+ channelId ,
57+ PlaylistType . Short ,
58+ ) ;
59+ const liveId = await getSinglePlaylistAndReturnVideoId (
60+ channelId ,
61+ PlaylistType . Stream ,
62+ ) ;
63+
64+ const query = `INSERT INTO youtube (youtube_channel_id, latest_video_id, latest_short_id, latest_stream_id) VALUES (?, ?, ?, ?)` ;
65+
66+ try {
67+ const client = await pool . connect ( ) ;
68+
69+ await client . query ( query , [
70+ channelId ,
71+ longId || null ,
72+ shortId || null ,
73+ liveId || null ,
74+ ] ) ;
75+
76+ return true ;
77+ } catch ( err ) {
78+ console . error ( "Error adding channel to track:" , err ) ;
79+
80+ return false ;
81+ }
82+ }
0 commit comments