Skip to content

Commit

Permalink
fixed calculation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pandeymangg committed Nov 28, 2021
1 parent 3f27e74 commit 44e5674
Show file tree
Hide file tree
Showing 4 changed files with 1,595 additions and 69 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
*.env
138 changes: 71 additions & 67 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,79 @@
const { google } = require('googleapis')
const youtube = google.youtube('v3')
const { google } = require("googleapis");
const youtube = google.youtube("v3");
const dotenv = require("dotenv");
dotenv.config({ path: "./.env" });

const api_key = 'your_api_key'
const api_key = process.env.API_KEY;
const playlistId = "some_playlist_id";

let videoIds = []
let regex = /(\d+)/g
let duration = []
let durationString = ''
let nextPageToken = null
let durationSeconds = 0
let totalSeconds = 0
let videoIds = [];
let hourRegex = /(\d+)H/g;
let minuteRegex = /(\d+)M/g;
let secondRegex = /(\d+)S/g;

async function getResponse() {
try {
while (true) {
const plResponse = await youtube.playlistItems.list({
key: api_key,
part: 'contentDetails, snippet',
playlistId: 'any playlist id here',
maxResults: 50,
pageToken: nextPageToken
})

videoIds = []
plResponse.data.items.forEach(item => {
videoIds.push(item.contentDetails.videoId)
})

const vidResponse = await youtube.videos.list({
key: api_key,
part: 'contentDetails',
id: videoIds.join(',')
})

vidResponse.data.items.forEach(item => {
duration = item.contentDetails.duration.match(regex)

if (duration.length < 4) {
durationString = `${duration[0]} ${duration[1]} ${duration[2]}`
}

if (duration.length < 3) {
durationString = `0 ${duration[0]} ${duration[1]}`
}

if (duration.length < 2) {
durationString = `0 0 ${duration[0]}`
}
let nextPageToken = null;
let durationSeconds = 0;
let totalSeconds = 0;

duration = durationString.split(' ')

durationSeconds = parseInt(duration[0])*3600 + parseInt(duration[1])*60 + parseInt(duration[2])

totalSeconds += durationSeconds
})

nextPageToken = plResponse.data.nextPageToken
if (!nextPageToken) {
break;
}

}

let [ minutes, seconds ] = [parseInt(totalSeconds / 60), parseInt(totalSeconds % 60)]
let hours = parseInt(minutes / 60)
minutes = parseInt(minutes % 60)
console.log(hours, minutes, seconds)
} catch (e) {
console.log(e.message)
async function getResponse() {
try {
while (true) {
const plResponse = await youtube.playlistItems.list({
key: api_key,
part: "contentDetails, snippet",
playlistId,
maxResults: 50,
pageToken: nextPageToken,
});

videoIds = [];
plResponse.data.items.forEach((item) =>
videoIds.push(item.contentDetails.videoId)
);

const vidResponse = await youtube.videos.list({
key: api_key,
part: "contentDetails",
id: videoIds.join(","),
});

vidResponse.data.items.forEach((item, index) => {
const hour =
item.contentDetails.duration
?.match(hourRegex)?.[0]
?.replace("H", "") || 0;
const min =
item.contentDetails.duration
?.match(minuteRegex)?.[0]
?.replace("M", "") || 0;
const sec =
item.contentDetails.duration
?.match(secondRegex)?.[0]
?.replace("S", "") || 0;

durationSeconds =
parseInt(hour) * 3600 + parseInt(min) * 60 + parseInt(sec);

totalSeconds += durationSeconds;
});

nextPageToken = plResponse.data.nextPageToken;
if (!nextPageToken) {
break;
}
}

let [minutes, seconds] = [
parseInt(totalSeconds / 60),
parseInt(totalSeconds % 60),
];

let hours = parseInt(minutes / 60);
minutes = parseInt(minutes % 60);
console.log(hours, minutes, seconds);
} catch (e) {
console.log(e.message);
}
}

getResponse()
getResponse();
Loading

0 comments on commit 44e5674

Please sign in to comment.