@@ -12,131 +12,134 @@
|
||||
* bun run scripts/parse-facebook-cookies.ts "cookie_string" --output my-cookies.json
|
||||
*/
|
||||
|
||||
import { parseFacebookCookieString } from '../src/facebook';
|
||||
import { parseFacebookCookieString } from "../src/facebook";
|
||||
|
||||
interface Cookie {
|
||||
name: string;
|
||||
value: string;
|
||||
domain: string;
|
||||
path: string;
|
||||
secure?: boolean;
|
||||
httpOnly?: boolean;
|
||||
sameSite?: "strict" | "lax" | "none" | "unspecified";
|
||||
expirationDate?: number;
|
||||
storeId?: string;
|
||||
name: string;
|
||||
value: string;
|
||||
domain: string;
|
||||
path: string;
|
||||
secure?: boolean;
|
||||
httpOnly?: boolean;
|
||||
sameSite?: "strict" | "lax" | "none" | "unspecified";
|
||||
expirationDate?: number;
|
||||
storeId?: string;
|
||||
}
|
||||
|
||||
function parseFacebookCookieStringCLI(cookieString: string): Cookie[] {
|
||||
if (!cookieString || !cookieString.trim()) {
|
||||
console.error('❌ Error: Empty or invalid cookie string provided');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!cookieString || !cookieString.trim()) {
|
||||
console.error("❌ Error: Empty or invalid cookie string provided");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const cookies = parseFacebookCookieString(cookieString);
|
||||
const cookies = parseFacebookCookieString(cookieString);
|
||||
|
||||
if (cookies.length === 0) {
|
||||
console.error('❌ Error: No valid cookies found in input string');
|
||||
console.error('Expected format: "name1=value1; name2=value2;"');
|
||||
process.exit(1);
|
||||
}
|
||||
if (cookies.length === 0) {
|
||||
console.error("❌ Error: No valid cookies found in input string");
|
||||
console.error('Expected format: "name1=value1; name2=value2;"');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
return cookies;
|
||||
return cookies;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const args = process.argv.slice(2);
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
if (args.length === 0 && process.stdin.isTTY === false) {
|
||||
// Read from stdin
|
||||
let input = '';
|
||||
for await (const chunk of process.stdin) {
|
||||
input += chunk;
|
||||
}
|
||||
input = input.trim();
|
||||
if (args.length === 0 && process.stdin.isTTY === false) {
|
||||
// Read from stdin
|
||||
let input = "";
|
||||
for await (const chunk of process.stdin) {
|
||||
input += chunk;
|
||||
}
|
||||
input = input.trim();
|
||||
|
||||
if (!input) {
|
||||
console.error('❌ Error: No input provided via stdin');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!input) {
|
||||
console.error("❌ Error: No input provided via stdin");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const cookies = parseFacebookCookieStringCLI(input);
|
||||
await writeOutput(cookies, './cookies/facebook.json');
|
||||
return;
|
||||
}
|
||||
const cookies = parseFacebookCookieStringCLI(input);
|
||||
await writeOutput(cookies, "./cookies/facebook.json");
|
||||
return;
|
||||
}
|
||||
|
||||
let cookieString = '';
|
||||
let outputPath = './cookies/facebook.json';
|
||||
let inputPath = '';
|
||||
let cookieString = "";
|
||||
let outputPath = "./cookies/facebook.json";
|
||||
let inputPath = "";
|
||||
|
||||
// Parse command line arguments
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const arg = args[i];
|
||||
// Parse command line arguments
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const arg = args[i];
|
||||
|
||||
if (arg === '--input' || arg === '-i') {
|
||||
inputPath = args[i + 1];
|
||||
i++; // Skip next arg
|
||||
} else if (arg === '--output' || arg === '-o') {
|
||||
outputPath = args[i + 1];
|
||||
i++; // Skip next arg
|
||||
} else if (arg === '--help' || arg === '-h') {
|
||||
showHelp();
|
||||
return;
|
||||
} else if (!arg.startsWith('-')) {
|
||||
// Assume this is the cookie string
|
||||
cookieString = arg;
|
||||
} else {
|
||||
console.error(`❌ Unknown option: ${arg}`);
|
||||
showHelp();
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
if (arg === "--input" || arg === "-i") {
|
||||
inputPath = args[i + 1];
|
||||
i++; // Skip next arg
|
||||
} else if (arg === "--output" || arg === "-o") {
|
||||
outputPath = args[i + 1];
|
||||
i++; // Skip next arg
|
||||
} else if (arg === "--help" || arg === "-h") {
|
||||
showHelp();
|
||||
return;
|
||||
} else if (!arg.startsWith("-")) {
|
||||
// Assume this is the cookie string
|
||||
cookieString = arg;
|
||||
} else {
|
||||
console.error(`❌ Unknown option: ${arg}`);
|
||||
showHelp();
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Read from file if specified
|
||||
if (inputPath) {
|
||||
try {
|
||||
const file = Bun.file(inputPath);
|
||||
if (!(await file.exists())) {
|
||||
console.error(`❌ Error: Input file not found: ${inputPath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
cookieString = await file.text();
|
||||
} catch (error) {
|
||||
console.error(`❌ Error reading input file: ${error}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
// Read from file if specified
|
||||
if (inputPath) {
|
||||
try {
|
||||
const file = Bun.file(inputPath);
|
||||
if (!(await file.exists())) {
|
||||
console.error(`❌ Error: Input file not found: ${inputPath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
cookieString = await file.text();
|
||||
} catch (error) {
|
||||
console.error(`❌ Error reading input file: ${error}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!cookieString.trim()) {
|
||||
console.error('❌ Error: No cookie string provided');
|
||||
console.error('Provide cookie string as argument, --input file, or via stdin');
|
||||
showHelp();
|
||||
process.exit(1);
|
||||
}
|
||||
if (!cookieString.trim()) {
|
||||
console.error("❌ Error: No cookie string provided");
|
||||
console.error(
|
||||
"Provide cookie string as argument, --input file, or via stdin",
|
||||
);
|
||||
showHelp();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const cookies = parseFacebookCookieStringCLI(cookieString);
|
||||
await writeOutput(cookies, outputPath);
|
||||
const cookies = parseFacebookCookieStringCLI(cookieString);
|
||||
await writeOutput(cookies, outputPath);
|
||||
}
|
||||
|
||||
async function writeOutput(cookies: Cookie[], outputPath: string) {
|
||||
try {
|
||||
await Bun.write(outputPath, JSON.stringify(cookies, null, 2));
|
||||
console.log(`✅ Successfully parsed ${cookies.length} Facebook cookies`);
|
||||
console.log(`📁 Saved to: ${outputPath}`);
|
||||
try {
|
||||
await Bun.write(outputPath, JSON.stringify(cookies, null, 2));
|
||||
console.log(`✅ Successfully parsed ${cookies.length} Facebook cookies`);
|
||||
console.log(`📁 Saved to: ${outputPath}`);
|
||||
|
||||
// Show summary of parsed cookies
|
||||
console.log('\n📋 Parsed cookies:');
|
||||
for (const cookie of cookies) {
|
||||
console.log(` • ${cookie.name}: ${cookie.value.substring(0, 20)}${cookie.value.length > 20 ? '...' : ''}`);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error(`❌ Error writing to output file: ${error}`);
|
||||
process.exit(1);
|
||||
}
|
||||
// Show summary of parsed cookies
|
||||
console.log("\n📋 Parsed cookies:");
|
||||
for (const cookie of cookies) {
|
||||
console.log(
|
||||
` • ${cookie.name}: ${cookie.value.substring(0, 20)}${cookie.value.length > 20 ? "..." : ""}`,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`❌ Error writing to output file: ${error}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function showHelp() {
|
||||
console.log(`
|
||||
console.log(`
|
||||
Facebook Cookie Parser CLI
|
||||
|
||||
Parses Facebook cookie strings into JSON format for the marketplace scraper.
|
||||
@@ -173,8 +176,8 @@ OUTPUT:
|
||||
|
||||
// Run the CLI
|
||||
if (import.meta.main) {
|
||||
main().catch(error => {
|
||||
console.error(`❌ Unexpected error: ${error}`);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
main().catch((error) => {
|
||||
console.error(`❌ Unexpected error: ${error}`);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user