From c8e2baaef101b378603bbd9cfe1e8c232d7718fd Mon Sep 17 00:00:00 2001 From: onionchunky <78101139+ChunkyProgrammer@users.noreply.github.com> Date: Sun, 10 Apr 2022 09:00:45 -0400 Subject: [PATCH 1/2] extract lengthSeconds for YouTube Shorts --- app/helper.js | 3 +++ test/channelVideos.test.js | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/app/helper.js b/app/helper.js index 4a60d91..6e12b08 100644 --- a/app/helper.js +++ b/app/helper.js @@ -165,6 +165,7 @@ class YoutubeGrabberHelper { } parseVideo(obj, channelInfo) { + const shortsRegex = /(months?|years?|days?|hours?|weeks?) ago (\d*) seconds/ let video let liveNow = false let premiere = false @@ -236,6 +237,8 @@ class YoutubeGrabberHelper { const seconds = parseInt(durationSplit[1]) lengthSeconds = (minutes * 60) + seconds + } else if (durationSplit[0] === 'SHORTS') { // durationText will still be 'SHORTS' for shorts + lengthSeconds = video.title.accessibility.accessibilityData.label.match(shortsRegex)[2] } } else { lengthSeconds = 0 diff --git a/test/channelVideos.test.js b/test/channelVideos.test.js index ff9f01d..bddabdc 100644 --- a/test/channelVideos.test.js +++ b/test/channelVideos.test.js @@ -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) => { From ea80b8feffd0d6c9fb82649548150d854956ecf7 Mon Sep 17 00:00:00 2001 From: onion chunky <78101139+ChunkyProgrammer@users.noreply.github.com> Date: Tue, 17 May 2022 19:22:20 -0400 Subject: [PATCH 2/2] improve regex & set duration text --- app/helper.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/helper.js b/app/helper.js index 6e12b08..271def5 100644 --- a/app/helper.js +++ b/app/helper.js @@ -165,7 +165,7 @@ class YoutubeGrabberHelper { } parseVideo(obj, channelInfo) { - const shortsRegex = /(months?|years?|days?|hours?|weeks?) ago (\d*) seconds/ + const shortsRegex = /(months?|years?|days?|hours?|weeks?) ago (\d*) (second|minute)/ let video let liveNow = false let premiere = false @@ -238,7 +238,13 @@ class YoutubeGrabberHelper { lengthSeconds = (minutes * 60) + seconds } else if (durationSplit[0] === 'SHORTS') { // durationText will still be 'SHORTS' for shorts - lengthSeconds = video.title.accessibility.accessibilityData.label.match(shortsRegex)[2] + 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