Skip to content

Instantly share code, notes, and snippets.

@roose
Created October 10, 2023 12:47
Show Gist options
  • Save roose/805c5f75fd7a67c1be6db72167d71abb to your computer and use it in GitHub Desktop.
Save roose/805c5f75fd7a67c1be6db72167d71abb to your computer and use it in GitHub Desktop.
Parse youtube history file
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