Created
October 10, 2023 12:47
-
-
Save roose/805c5f75fd7a67c1be6db72167d71abb to your computer and use it in GitHub Desktop.
Parse youtube history file
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
import fs from "fs"; | |
const viewHistory = JSON.parse(fs.readFileSync("./data/view-history.json")); | |
function getVideosById(videos) { | |
let videosById = {}; | |
videos.forEach((video) => { | |
let regex = /https:\/\/www\.youtube\.com\/watch\?v=(.*)/; | |
let title = video.title.substring(18).replace(/\s+/, " "); | |
if (video.titleUrl && regex.test(video.titleUrl)) { | |
let videoId = regex.exec(video.titleUrl)[1]; | |
if (videosById.hasOwnProperty(videoId)) { | |
videosById[videoId].count += 1; | |
} else { | |
videosById[videoId] = { | |
title: title, | |
id: videoId, | |
videoUrl: video.titleUrl, | |
count: 1, | |
}; | |
} | |
} | |
}); | |
return videosById; | |
} | |
let videos = getVideosById(viewHistory); | |
let top40 = Object.values(videos) | |
.sort((a, b) => b.count - a.count) | |
.slice(0, 40); | |
console.log(top40); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment