Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
extract lengthSeconds for YouTube Shorts (#75)
Browse files Browse the repository at this point in the history
* extract lengthSeconds for YouTube Shorts

* improve regex & set duration text
  • Loading branch information
ChunkyProgrammer authored May 24, 2022
1 parent e80df44 commit a6ce60a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class YoutubeGrabberHelper {
}

parseVideo(obj, channelInfo) {
const shortsRegex = /(months?|years?|days?|hours?|weeks?) ago (\d*) (second|minute)/
let video
let liveNow = false
let premiere = false
Expand Down Expand Up @@ -236,6 +237,14 @@ class YoutubeGrabberHelper {
const seconds = parseInt(durationSplit[1])

lengthSeconds = (minutes * 60) + seconds
} else if (durationSplit[0] === 'SHORTS') { // durationText will still be 'SHORTS' for shorts
const regexMatch = video.title.accessibility.accessibilityData.label.match(shortsRegex)
lengthSeconds = parseInt(regexMatch[2])
durationText = '0:' + (lengthSeconds.toString().padStart(2,'0'))
if (regexMatch[3] == 'minute') {
lengthSeconds *= 60
durationText = '1:00'
}
}
} else {
lengthSeconds = 0
Expand Down
7 changes: 7 additions & 0 deletions test/channelVideos.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ describe('Getting channel videos', () => {
})
})

test('Shorts Channel', () => {
const parameters = { channelId: 'UC4-79UOlP48-QNGgCko5p2g', channelIdType: 1 }
return ytch.getChannelVideos(parameters).then((data) => {
expect(data.items[0].lengthSeconds).not.toBe(0)
})
})

test('Public channel w/o videos', () => {
const parameters = { channelId: 'UCS-DgEvT4XuQsrrmI7iZVsA', channelIdType: 1 }
return ytch.getChannelVideos(parameters).then((data) => {
Expand Down

0 comments on commit a6ce60a

Please sign in to comment.