UNPKG

bookgrabs

Version:

Interactive CLI tool for LibGen ebook searches and downloads with batch processing support

124 lines (119 loc) 4.89 kB
// Get query from command line arguments export function parseArgs() { const args = process.argv.slice(2); let author = ''; let title = ''; let query = ''; let csvFile = ''; let searchOptions = { topics: ['l', 'f'], // default: libgen + fiction maxResults: 25, resultsPerPage: 100, fileStatus: 'all', language: 'eng' // default: English }; for (let i = 0; i < args.length; i++) { if (args[i] === '--help' || args[i] === '-h') { showUsage(); process.exit(0); } else if (args[i] === '--author' || args[i] === '-a') { author = args[i + 1] || ''; i++; // skip next argument } else if (args[i] === '--title' || args[i] === '-t') { title = args[i + 1] || ''; i++; // skip next argument } else if (args[i] === '--csv' || args[i] === '-c') { csvFile = args[i + 1] || ''; i++; // skip next argument } else if (args[i] === '--fiction') { searchOptions.topics = ['f']; // fiction only } else if (args[i] === '--nonfiction') { searchOptions.topics = ['l']; // libgen only (non-fiction) } else if (args[i] === '--comics') { searchOptions.topics = ['c']; // comics only } else if (args[i] === '--all-topics') { searchOptions.topics = ['l', 'c', 'f', 'a', 'm', 's']; // all topics } else if (args[i] === '--results' || args[i] === '-r') { const num = parseInt(args[i + 1], 10); if (num > 0) { searchOptions.maxResults = num; } i++; // skip next argument } else if (args[i] === '--page-size') { const size = parseInt(args[i + 1], 10); if ([25, 50, 100].includes(size)) { searchOptions.resultsPerPage = size; } i++; // skip next argument } else if (args[i] === '--sorted-only') { searchOptions.fileStatus = 'sorted'; } else if (args[i] === '--unsorted-only') { searchOptions.fileStatus = 'uns'; } else if (args[i] === '--english') { searchOptions.language = 'eng'; } else if (args[i] === '--any-language') { searchOptions.language = null; // No language filter } else if (args[i] === '--language' || args[i] === '-l') { const lang = args[i + 1]; if (lang && lang.length === 3) { searchOptions.language = lang; } i++; // skip next argument } else if (args[i] === '--ebooks-only') { searchOptions.ebooksOnly = true; } else if (!args[i].startsWith('-')) { // If no flags, treat as general search query query += args[i] + ' '; } } query = query.trim(); // If author and title are provided, combine them for search if (author && title) { query = `${author} ${title}`; } else if (author) { query = author; } else if (title) { query = title; } return { author: author.toLowerCase(), title: title.toLowerCase(), query, csvFile, searchOptions }; } // Display usage information export function showUsage() { console.log('BookGrabs - Interactive LibGen Book Search & Download Tool'); console.log(''); console.log('Usage:'); console.log(' npx bookgrabs # Interactive mode (default)'); console.log(' npx bookgrabs "search term" # Quick search'); console.log(' npx bookgrabs --author "Author Name" --title "Book Title"'); console.log(' npx bookgrabs -a "Author" -t "Title"'); console.log(' npx bookgrabs -c "path/to/books.csv" # CSV must have author,title columns, optionally year'); console.log(''); console.log('Search Options:'); console.log(' --fiction Search fiction only'); console.log(' --nonfiction Search non-fiction only'); console.log(' --comics Search comics only'); console.log(' --all-topics Search all topics (fiction, non-fiction, comics, articles, magazines, standards)'); console.log(' --results, -r NUM Maximum number of results (default: 25)'); console.log(' --page-size SIZE Results per page: 25, 50, or 100 (default: 100)'); console.log(' --sorted-only Only search sorted files'); console.log(' --unsorted-only Only search unsorted files'); console.log(' --english Search English books only (default)'); console.log(' --any-language Search all languages'); console.log(' --language, -l CODE 3-letter language code (e.g., eng, fra, deu, spa)'); console.log(''); console.log('Examples:'); console.log(' npx bookgrabs --fiction "Harry Potter"'); console.log(' npx bookgrabs --nonfiction --results 50 "JavaScript"'); console.log(' npx bookgrabs --comics "Batman"'); console.log(' npx bookgrabs --fiction --language fra "Harry Potter" # French books'); console.log(' npx bookgrabs --any-language "1984" # All languages'); console.log(''); console.log('For interactive mode with a beautiful UI, run without arguments:'); console.log(' npx bookgrabs'); }