Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/tharoosha/Depresio into you…
Browse files Browse the repository at this point in the history
…tube-nadil
  • Loading branch information
Nadil-K committed Oct 31, 2023
2 parents 4af543a + 0a011f1 commit 8acf7db
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 32 deletions.
1 change: 0 additions & 1 deletion backend/.cache

This file was deleted.

9 changes: 7 additions & 2 deletions backend/controllers/mlController.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,14 @@ export async function youtube_lists(req, res) {
// const process = spawn("/usr/src/app/venv/bin/python3", ["/usr/src/app/ml_models/recommanded_system/youtube_search.py", categories,]);
const process = spawn("python3", ["../backend/ml_models/recommanded_system/youtube_search.py", categories,]);

let youtube_list = []
// let youtube_list = []
let youtube_list = "";


process.stdout.on("data", (data) => {
youtube_list = data;
// youtube_list = data;
youtube_list += data.toString();

});

process.on("close", (code) => {
Expand Down
16 changes: 14 additions & 2 deletions backend/ml_models/recommanded_system/youtube_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,31 @@ def youtube_lists(input):
# Parse the JSON data

category_list = input["categories"]
print(category_list)
# 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, categories)
# print(videos)
# # for video in videos:
# # print(video)

output = {"result": videos}
# # output = {"result": ["lTxn2BuqyzU", "MU0HhH4dIHs", "i5gbWhRW_ZA", "FUqttxtk8y0", "8PD-Smkxsj0", "nqf8-vB_PWE", "TQgWzalijAw", "JbHk0ZjYM04", "GkIA4ldWRis", "puy5WYx1Wok", "DwuJeGYlYyw", "4GvI7M8PyKs", "n_NfxUQCoXE", "Fjp2TdlTTIU", "SCyFBeIgaDc"]}

output_json = json.dumps(output)
# # sys.stdout.flush()
# # print(result)
print(output_json)
# json_string = json.dumps(videos)
# json_string = json.dumps(videos)
# print(json_string)

# videos = videos.split(',')
print(videos)
# print(videos)
# return videos

except Exception as e:
Expand All @@ -170,8 +180,10 @@ def youtube_lists(input):
# data = json.loads(input)
# youtube_lists(input)
input = {"categories" : [input]}
# input = ["Film & Animation","Music","Shows"]
youtube_lists(input)
# print(input)
# print(input)



Expand Down
100 changes: 73 additions & 27 deletions frontend/src/pages/YT_RecommendationView.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import axios from 'axios';

const YT_RecommendationView = () => {
const [videoIds, setVideoIds] = useState([]);
// const [videoTrack, setVideoTrack] = useState([]);
const [isReviewDialogVisible, setIsReviewDialogVisible] = useState(false);
const [rating, setRating] = useState(0);

Expand All @@ -42,33 +43,78 @@ 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);
// 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(videoIdsArray);
})
.catch((error) => {
console.log(error);
});
}, [storedData]);

console.log(videoIds);
// 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) {

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);

console.log('Loaded Ids ', typeof videoUrls);
// videoIds = JSON.parse(videoIds)
// console.log(typeof videoIds)
// const listData = JSON.parse(videoIds.replace(/'/g, '"'));

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

return (
<>
Expand All @@ -83,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 @@ -105,7 +151,7 @@ const YT_RecommendationView = () => {
</div>
</>
);
})}
})} */}
</div>
<div className="popup-inner__button"></div>
</div>
Expand Down

0 comments on commit 8acf7db

Please sign in to comment.