From 7387169a4017796cbf4e73610d922933bb7311a8 Mon Sep 17 00:00:00 2001 From: tharoosha Date: Wed, 1 Nov 2023 03:25:35 +0530 Subject: [PATCH] youtube ser --- .../recommanded_system/youtube_search.py | 9 +- frontend/src/pages/YT_RecommendationView.js | 130 +++++++++--------- 2 files changed, 71 insertions(+), 68 deletions(-) diff --git a/backend/ml_models/recommanded_system/youtube_search.py b/backend/ml_models/recommanded_system/youtube_search.py index afd6c9d..4079e57 100644 --- a/backend/ml_models/recommanded_system/youtube_search.py +++ b/backend/ml_models/recommanded_system/youtube_search.py @@ -76,7 +76,9 @@ def search_youtube_videos(video_types_probabilities): - api_key = os.getenv("YOUTUBE_API_KEY") + # api_key = os.getenv("YOUTUBE_API_KEY") + api_key = "AIzaSyBLplcZSVmjupMn-6drtByHPuFFuQui4MI" + max_videos = sum(video_types_probabilities.values()) videos_info = [] @@ -137,14 +139,15 @@ def get_youtube_videos_from_preferences(api_key, categories, max_results=5): def youtube_lists(input): try: # Parse the JSON data + api_key = "AIzaSyBLplcZSVmjupMn-6drtByHPuFFuQui4MI" category_list = input["categories"] # print(category_list) # print(category_list) # categories = ["Gaming"] - videos = get_youtube_videos_from_preferences(os.getenv("YOUTUBE_API_KEY"), category_list) - + videos = get_youtube_videos_from_preferences(api_key, category_list) + # videos = ["FLDAuYJy4Tw", "XGdYhRxHNAQ", "MhZhezlx8ZU", "BejOWDwJYYM", "lg-oBTPvlgU"] # videos = get_youtube_videos_from_preferences(API_KEY, categories) # print(videos) diff --git a/frontend/src/pages/YT_RecommendationView.js b/frontend/src/pages/YT_RecommendationView.js index 754674e..894f79f 100644 --- a/frontend/src/pages/YT_RecommendationView.js +++ b/frontend/src/pages/YT_RecommendationView.js @@ -43,69 +43,69 @@ const YT_RecommendationView = () => { localStorage.setItem('rating', rating); }, [rating]); - // useEffect(() => { - // const requestOptions = { - // method: 'POST', - // headers: { 'Content-Type': 'application/json' }, - // body: storedData, - // }; - // axios - // .post(`${process.env.REACT_APP_SERVER_ENDPOINT}/api/youtube_list`, storedData) - // .then((response) => { - // console.log(response.data.result); - // // const parsedArray = JSON.parse(response.data.replace(/\n/g, '')); - // // const videoIdsArray = response.data.split('\n').map(line => line.replace(/[[\]']/g, '')).filter(id => id !== 'undefined'); - // // videoIdsArray = videoIdsArray.split(', '); - // // var videoUrls = JSON.parse(response.data); - // // console.log(videoIdsArray) - // setVideoIds(JSON.parse(response.data.result)); - // }) - // .catch((error) => { - // console.log(error); - // }); - // }, [storedData]); - - - async function getYoutubeVideosFromPreferences(apiKey, categories, maxResults = 5) { + useEffect(() => { + const requestOptions = { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: storedData, + }; + axios + .post(`${process.env.REACT_APP_SERVER_ENDPOINT}/api/youtube_list`, storedData) + .then((response) => { + console.log(response); + // const parsedArray = JSON.parse(response.data.replace(/\n/g, '')); + // const videoIdsArray = response.data.split('\n').map(line => line.replace(/[[\]']/g, '')).filter(id => id !== 'undefined'); + // videoIdsArray = videoIdsArray.split(', '); + // var videoUrls = JSON.parse(response.data); + // console.log(videoIdsArray) + setVideoIds((response.data.result)); + }) + .catch((error) => { + console.log(error); + }); + }, [storedData]); + + + // async function getYoutubeVideosFromPreferences(apiKey, categories, maxResults = 5) { - const baseUrl = 'https://www.googleapis.com/youtube/v3/search'; - const videoLinks = []; - - for (const category of categories) { - const params = { - part: 'snippet', - q: category, - type: 'video', - maxResults: maxResults, - key: apiKey, - }; - - try { - const response = await axios.get(baseUrl, { params }); - const data = response.data; - - if (!data.items) { - console.error(`Error fetching videos for category '${category}':`); - continue; - } - - for (const item of data.items) { - const videoId = item.id.videoId; - // const link = `https://www.youtube.com/watch?v=${videoId}`; - const link = videoId; - videoLinks.push(link); - } - } catch (error) { - console.error(`Error fetching videos for category '${category}':`, error); - } - } - - return videoLinks; - } - - let videoLinks = getYoutubeVideosFromPreferences(process.env.YOUTUBE_API_KEY, storedData) - - console.log(videoLinks); + // const baseUrl = 'https://www.googleapis.com/youtube/v3/search'; + // const videoLinks = []; + + // for (const category of categories) { + // const params = { + // part: 'snippet', + // q: category, + // type: 'video', + // maxResults: maxResults, + // key: apiKey, + // }; + + // try { + // const response = await axios.get(baseUrl, { params }); + // const data = response.data; + + // if (!data.items) { + // console.error(`Error fetching videos for category '${category}':`); + // continue; + // } + + // for (const item of data.items) { + // const videoId = item.id.videoId; + // // const link = `https://www.youtube.com/watch?v=${videoId}`; + // const link = videoId; + // videoLinks.push(link); + // } + // } catch (error) { + // console.error(`Error fetching videos for category '${category}':`, error); + // } + // } + + // return videoLinks; + // } + + // let videoLinks = getYoutubeVideosFromPreferences(process.env.YOUTUBE_API_KEY, storedData) + + // console.log(videoLinks); // Convert the list to a JSON array // videoIds = JSON.parse(videoIds); // videoIds = JSON.parse(videoIds) @@ -113,7 +113,7 @@ const YT_RecommendationView = () => { // const listData = JSON.parse(videoIds.replace(/'/g, '"')); // console.log(listData); - // console.log(videoIds) + console.log({videoIds}) // console.log('Loaded Ids ', typeof videoUrls); return ( @@ -129,7 +129,7 @@ const YT_RecommendationView = () => {
- {/* {videoIds.map((videoId) => { + {videoIds.map((videoId) => { return ( <>
@@ -151,7 +151,7 @@ const YT_RecommendationView = () => {
); - })} */} + })}