-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f27e74
commit 44e5674
Showing
4 changed files
with
1,595 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules/ | ||
node_modules/ | ||
*.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.