Skip to content

Commit

Permalink
Increased api fetching speed
Browse files Browse the repository at this point in the history
  • Loading branch information
kmr-ankitt committed Sep 16, 2024
1 parent d13abdf commit d7d8e41
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions backend/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ app.use(cors());

async function getVideoDuration(videoIds) {
try {
let duration = 0;
for (const videoID of videoIds) {
const requests = videoIds.map(videoID => {
const url = `https://www.googleapis.com/youtube/v3/videos`;
const response = await axios.get(url, {
return axios.get(url, {
params: {
part: "contentDetails",
id: videoID,
// eslint-disable-next-line no-undef
key: process.env.YOUTUBE_API_KEY,
},
});
});

const responses = await Promise.all(requests);
const totalDuration = responses.reduce((acc, response) => {
const data = response.data.items[0].contentDetails.duration;
duration += parseISODurationToMinutes(data);
}
return { totalDuration: duration, videoCount: videoIds.length };
return acc + parseISODurationToMinutes(data);
}, 0);

return { totalDuration, videoCount: videoIds.length };
} catch (error) {
console.log("Error fetching video duration:", error);
return null;
Expand Down

0 comments on commit d7d8e41

Please sign in to comment.