Skip to content

Instantly share code, notes, and snippets.

@ejfox
Created December 20, 2023 19:48
Show Gist options
  • Save ejfox/643e4f5339ae54da1722ad8d5c79e4ab to your computer and use it in GitHub Desktop.
Save ejfox/643e4f5339ae54da1722ad8d5c79e4ab to your computer and use it in GitHub Desktop.
const fs = require('fs');
const readline = require('readline');
const processInput = (input) => {
// Split the input into chunks based on the list numbers
const chunks = input.split(/\n\s*\d+\n/).filter(chunk => chunk.trim() !== '');
// Extract title and URL from each chunk
const videos = chunks.map(chunk => {
const titleMatch = chunk.match(/(.*?)https?:\/\/youtube\.com/g);
const urlMatch = chunk.match(/https?:\/\/youtube\.com[^\s]+/g);
if (titleMatch && urlMatch) {
const title = titleMatch[0].trim();
const url = urlMatch[urlMatch.length - 1].trim();
return { title, url };
}
return null;
}).filter(video => video !== null);
return videos;
}
// Read the entire input from STDIN as a single string
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
let inputData = '';
rl.on('line', (line) => {
inputData += line + '\n';
});
rl.on('close', () => {
const videos = processInput(inputData);
// Create CSV content
let csvContent = "Index,Title,URL\n";
videos.forEach((video, index) => {
csvContent += `${index + 1},"${video.title.replace(/"/g, '""')}","${video.url}"\n`;
});
// Write CSV to file
fs.writeFileSync('output.csv', csvContent);
console.log('CSV file created: output.csv');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment