mvp-ai's picture
download
raw
4.13 kB
#!/usr/bin/env node
import { searchReddit, fetchPostByUrl, discoverSubreddits } from "./reddit.js";
import { loadProxies, hasProxies } from "./proxy.js";
type Args = {
_: string[];
[key: string]: string | boolean | string[] | undefined;
};
function parseArgs(argv: string[]): Args {
const args: Args = { _: [] };
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
if (a.startsWith("--")) {
const key = a.slice(2);
const next = argv[i + 1];
if (!next || next.startsWith("--")) {
args[key] = true;
} else {
args[key] = next;
i++;
}
} else {
args._.push(a);
}
}
return args;
}
function printHelp(): void {
console.log(`redditlens — find pain points on Reddit
USAGE
redditlens search "<query>" [options]
redditlens post <url> [--comments]
redditlens subreddits "<niche>"
SEARCH OPTIONS
--subreddits a,b,c Restrict to specific subreddits (comma-separated)
--period P Time window: day | week | month | year (default: week)
--limit N Max results (default: 20)
--comments Include top comments per post
--json Output JSON (default)
ENVIRONMENT
SERPER_API_KEY Required. Get a free key at https://serper.dev
PROXIES_FILE Optional. Path to proxy list (ip:port:user:pass, one per line)
EXAMPLES
redditlens search "notion alternatives" --period month --limit 10 --comments
redditlens search "prompt engineering" --subreddits ClaudeAI,LocalLLaMA --comments
redditlens post https://reddit.com/r/SaaS/comments/abc123/title/ --comments
redditlens subreddits "indie hacking"
`);
}
async function cmdSearch(args: Args): Promise<void> {
const query = args._[1];
if (!query) {
console.error("Error: query required");
process.exit(1);
}
const subs =
typeof args.subreddits === "string"
? args.subreddits
.split(",")
.map((s) => s.trim())
.filter(Boolean)
: [];
const period = (args.period as string) || "week";
if (!["day", "week", "month", "year"].includes(period)) {
console.error(`Error: --period must be day|week|month|year`);
process.exit(1);
}
const limit = args.limit ? parseInt(args.limit as string, 10) : 20;
const posts = await searchReddit({
query,
subreddits: subs,
period: period as "day" | "week" | "month" | "year",
limit,
comments: args.comments === true,
});
console.log(JSON.stringify({ query, count: posts.length, posts }, null, 2));
}
async function cmdPost(args: Args): Promise<void> {
const url = args._[1];
if (!url) {
console.error("Error: url required");
process.exit(1);
}
const post = await fetchPostByUrl(url, args.comments !== false);
if (!post) {
console.error("Error: failed to fetch post");
process.exit(1);
}
console.log(JSON.stringify(post, null, 2));
}
async function cmdSubreddits(args: Args): Promise<void> {
const niche = args._[1];
if (!niche) {
console.error("Error: niche required");
process.exit(1);
}
const subs = await discoverSubreddits(niche);
console.log(JSON.stringify({ niche, subreddits: subs }, null, 2));
}
async function main(): Promise<void> {
const args = parseArgs(process.argv.slice(2));
const cmd = args._[0];
if (!cmd || cmd === "--help" || cmd === "-h" || cmd === "help") {
printHelp();
return;
}
loadProxies();
if (process.env.REDDITLENS_DEBUG) {
console.error(
`[redditlens] proxies: ${hasProxies() ? "enabled (rotating per request)" : "disabled (polite throttle mode)"}`,
);
}
try {
switch (cmd) {
case "search":
await cmdSearch(args);
break;
case "post":
await cmdPost(args);
break;
case "subreddits":
await cmdSubreddits(args);
break;
default:
console.error(`Unknown command: ${cmd}`);
printHelp();
process.exit(1);
}
} catch (error) {
console.error(
`Error: ${error instanceof Error ? error.message : String(error)}`,
);
process.exit(1);
}
}
main();

Xet Storage Details

Size:
4.13 kB
·
Xet hash:
2bd0e7b444b576bbca820a4240e8adafbbbc07590ed21824c704aa2d07fc3c9a

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.