Lucas Sierota

Lucas Sierota

// Name: Merge pdfs
// Author: Lucas Sierota
// Description: Merge multiple pdfs into one
// Dependencies: pdf-lib
import "@johnlindquist/kit";
import fs from "fs/promises";
const { PDFDocument } = await npm("pdf-lib");
let name = await selectFile("Select a PDF file to merge");
const paths = [name];
// keep adding paths until there's no input
while (
(await arg({
placeholder: "Select another?",
hint: `Press only y or n? [y]/[n]`,
})) === "y"
) {
name = await selectFile("Select another PDF file to merge");
if (name !== "") paths.push(name);
}
// merge the pdfs
const pdfsToMerge = await Promise.all(
paths.map(async (path) => await fs.readFile(path))
);
let finalPath = await selectFolder("Select a folder to save the merged PDF");
let finalName = await arg("Enter a name for the merged PDF");
finalPath = `${finalPath}${finalName}.pdf`;
try {
const mergedPdf = await PDFDocument.create();
for (const pdfBytes of pdfsToMerge) {
const pdf = await PDFDocument.load(pdfBytes);
const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
copiedPages.forEach((page) => {
mergedPdf.addPage(page);
});
}
const buf = await mergedPdf.save(); // Uint8Array
let exists = await pathExists(finalPath);
if (!exists) {
await writeFile(finalPath, buf);
} else {
await div(md(`The path ${finalPath} already exists`));
}
} catch (e) {
log(`Error: ${e}`);
await div(md(`Error: ${e}`));
}
await div(md(`Merged PDF saved to ${finalPath}`));

// Name: download-youtube-video
// Description: Downloads an youtube video using the url
// Author: Lucas Sierota
// Twitter: @lucasemanuelss
import "@johnlindquist/kit";
import youtubeDlExec from "youtube-dl-exec";
const response = await widget(
`
<div class="w-full h-full text-center flex items-center justify-center">
<h1 :class="responseClass">{{ content }}</h1>
<div>
`
);
async function downloadVideo() {
try {
const videoSrc = await arg("Video url:");
const videoName = await arg("File name: (don't include extension)");
const videoPath = "Downloads"
const fileName = videoName !== "" ? videoName : videoSrc;
// setting path to be downloaded and including file extension
const path = home(videoPath, path.basename(fileName) + ".mp4");
// set loading state on widget
response.setState({
content: `Downloading...`,
responseClass: "text-yellow-600",
});
const res = await youtubeDlExec(videoSrc, { output: path });
console.log(res);
response.setState({
content: `${fileName} downloaded at ${path}`,
responseClass: "text-green-600",
});
} catch (err) {
console.log(err);
if (
err.stderr.includes("'NoneType' object has no attribute 'decompress'") ||
err.stderr.includes("is not a valid URL")
) {
response.setState({
content: `🔴 Error: The url provided is not an youtube video 🔴`,
responseClass: "text-red-600",
});
} else {
response.setState({
content: `🔴 Error ${err} 🔴`,
responseClass: "w-full h-full text-red-600",
});
}
}
}
await downloadVideo();