Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

parse dates relative

Christian Fei787ffb124d35
diff --git a/client/main.js b/client/main.js
index 218c241..f32e3f5 100644
--- a/client/main.js
+++ b/client/main.js
@@ -227,13 +227,15 @@ function channelVideosContents (videos) {
const ignoreList = store.get(store.ignoreVideoKey)
const $videosContainer = document.createElement('div')
$videosContainer.classList.add('videos-container')
- for (const video of videos) {
+ videos
+ .sort((a, b) => +new Date(b.publishedAt) - +new Date(a.publishedAt))
+ .forEach(video => {
if (ignoreList.includes(video.id)) {
console.log('ignoring video', video.id)
- continue
- }
+ return
+ }
$videosContainer.appendChild(createVideoElement(video))
- }
+ })
return $videosContainer
}
@@ -251,7 +253,7 @@ function createVideoElement (video) {
</p>
</video>`
: `<img src="${video.thumbnail}"/>`}
- <span>${video.publishedTime}</span> | <span>${video.viewCount}</span> | <span>${video.duration || 'N/A'}</span><br/>
+ <span>${video.publishedTime} | ${video.publishedAt}</span> | <span>${video.viewCount}</span> | <span>${video.duration || 'N/A'}</span><br/>
${video.title}
<div class="actions">
${video.downloaded
diff --git a/lib/server.js b/lib/server.js
index eb72ed5..b927f60 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -161,9 +161,9 @@ async function updateAndPersistVideos (repo, callback = () => {}) {
async function updateAndPersistVideosForChannel(name, repo, callback = () => {}) {
console.log('updating videos for', name)
- const videos = await getVideosFor(name)
+ let videos = await getVideosFor(name)
if (videos) {
- videos.map(v => patchVideo(v, repo))
+ videos = videos.map(v => patchVideo(v, repo))
repo.saveChannelVideos(name, videos)
callback({name, videos})
}
@@ -177,4 +177,5 @@ function patchVideo(video, repo) {
const existingVideo = existingChannelVideos.find(v => v.id === video.id)
Object.assign(video, existingVideo)
}
+ return video
}
\ No newline at end of file
diff --git a/lib/youtube.js b/lib/youtube.js
index b88982e..5675a97 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -58,8 +58,43 @@ export async function getVideosFor(channelName) {
description: v.richItemRenderer.content.videoRenderer.descriptionSnippet?.runs[0].text,
id: v.richItemRenderer.content.videoRenderer.videoId,
publishedTime: v.richItemRenderer.content.videoRenderer.publishedTimeText?.simpleText,
+ publishedAt: parseRelativeTime(v.richItemRenderer.content.videoRenderer.publishedTimeText?.simpleText),
viewCount: v.richItemRenderer.content.videoRenderer.viewCountText?.simpleText,
duration: v.richItemRenderer.content.videoRenderer.lengthText?.simpleText,
}
}
}
+
+function parseRelativeTime(relativeTime) {
+ const now = new Date();
+ let match;
+
+ const regexes = [
+ { regex: /(\d+)\s+seconds? ago/, unit: 'seconds' },
+ { regex: /(\d+)\s+minutes? ago/, unit: 'minutes' },
+ { regex: /(\d+)\s+hours? ago/, unit: 'hours' },
+ { regex: /(\d+)\s+days? ago/, unit: 'days' },
+ { regex: /(\d+)\s+weeks? ago/, unit: 'weeks' },
+ { regex: /(\d+)\s+months? ago/, unit: 'months' },
+ { regex: /(\d+)\s+years? ago/, unit: 'years' }
+ ];
+
+ for (let i = 0; i < regexes.length; i++) {
+ match = relativeTime.match(regexes[i].regex);
+ if (match) {
+ const amount = parseInt(match[1], 10);
+ switch (regexes[i].unit) {
+ case 'seconds': now.setSeconds(now.getSeconds() - amount); break;
+ case 'minutes': now.setMinutes(now.getMinutes() - amount); break;
+ case 'hours': now.setHours(now.getHours() - amount); break;
+ case 'days': now.setDate(now.getDate() - amount); break;
+ case 'weeks': now.setDate(now.getDate() - amount * 7); break;
+ case 'months': now.setMonth(now.getMonth() - amount); break;
+ case 'years': now.setFullYear(now.getFullYear() - amount); break;
+ }
+ return now;
+ }
+ }
+ console.error('Invalid relative time format', relativeTime);
+ return null
+}