Skip to content

Commit

Permalink
youtube ser
Browse files Browse the repository at this point in the history
  • Loading branch information
tharoosha committed Oct 31, 2023
1 parent 0a011f1 commit 7387169
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 68 deletions.
9 changes: 6 additions & 3 deletions backend/ml_models/recommanded_system/youtube_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down Expand Up @@ -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)
Expand Down
130 changes: 65 additions & 65 deletions frontend/src/pages/YT_RecommendationView.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,77 +43,77 @@ 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)
// console.log(typeof videoIds)
// const listData = JSON.parse(videoIds.replace(/'/g, '"'));

// console.log(listData);
// console.log(videoIds)
console.log({videoIds})
// console.log('Loaded Ids ', typeof videoUrls);

return (
Expand All @@ -129,7 +129,7 @@ const YT_RecommendationView = () => {

<div className="">
<div className="yt-container">
{/* {videoIds.map((videoId) => {
{videoIds.map((videoId) => {
return (
<>
<div className="yt-recommendation-item">
Expand All @@ -151,7 +151,7 @@ const YT_RecommendationView = () => {
</div>
</>
);
})} */}
})}
</div>
<div className="popup-inner__button"></div>
</div>
Expand Down

0 comments on commit 7387169

Please sign in to comment.