youtube-likes-downloader
Version:
Download all liked videos from youtube
77 lines • 3.74 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.importLikes = importLikes;
exports.exportLikes = exportLikes;
const fs_1 = __importDefault(require("fs"));
const google_api_wrapper_1 = require("@anmiles/google-api-wrapper");
const logger_1 = require("@anmiles/logger");
require("@anmiles/prototypes");
const youtube_1 = require("googleapis/build/src/apis/youtube");
const config_1 = require("./config");
const schema_1 = require("./types/schema");
const formatVideo_1 = require("./utils/formatVideo");
const parseVideos_1 = require("./utils/parseVideos");
const paths_1 = require("./utils/paths");
async function importLikes(profile) {
const likesFile = (0, paths_1.getLikesFile)(profile);
const includeLikesFile = (0, paths_1.getIncludeLikesFile)(profile);
const youtubeAPI = await (0, google_api_wrapper_1.getAPI)((auth) => (0, youtube_1.youtube)({ version: 'v3', auth }), profile);
const videosList = await youtubeAPI.getItems((api) => api.playlistItems, { playlistId: 'LL', part: ['snippet'], maxResults: 50 });
const likesData = [];
likesData.push(videosList.map(formatVideo_1.formatVideo).join('\n\n'));
if (fs_1.default.existsSync(includeLikesFile)) {
likesData.push(fs_1.default.readFileSync(includeLikesFile).toString());
}
const allLikesData = likesData.join('\n\n');
fs_1.default.writeFileSync(likesFile, allLikesData);
}
async function exportLikes(profile) {
const youtubeAPI = await (0, google_api_wrapper_1.getAPI)((auth) => (0, youtube_1.youtube)({ version: 'v3', auth }), profile, { temporary: true, scopes: config_1.config.scopes.full });
const videosList = await youtubeAPI.getItems((api) => api.playlistItems, { playlistId: 'LL', part: ['snippet'], maxResults: 50 });
/* istanbul ignore next */
const existingIDs = videosList.map((video) => video.snippet?.resourceId?.videoId ?? 'unknown');
const likesFile = (0, paths_1.getLikesFile)(profile);
if (!fs_1.default.existsSync(likesFile)) {
throw new Error(`Likes file ${likesFile} doesn't exist, please create it and paste each videos URL (like https://www.youtube.com/watch?v=abcabcabc) on each line`);
}
const likesData = fs_1.default.readFileSync(likesFile).toString();
const ids = (0, parseVideos_1.parseVideos)(likesData);
const rating = 'like';
for (const id of ids.reverse()) {
(0, logger_1.log)(id);
if (existingIDs.includes(id)) {
continue;
}
try {
await youtubeAPI.api.videos.rate({ id, rating });
}
catch (ex) {
const commonError = schema_1.commonErrorSchema.safeParse(ex).data;
if (ex instanceof Error || !commonError) {
throw ex;
}
else {
const firstError = schema_1.errorSchema.safeParse(ex).data?.errors[0];
if (firstError) {
switch (firstError.reason) {
case 'videoNotFound':
case 'notFound':
(0, logger_1.warn)(`Video ${id} not found`);
break;
case 'videoRatingDisabled':
case 'videoPurchaseRequired':
(0, logger_1.warn)(`Video ${id} is not allowed to be rated`);
break;
case undefined:
default:
throw ex;
}
}
}
}
}
}
//# sourceMappingURL=likes.js.map