mvp-ai's picture
download
raw
3.48 kB
import { fetchJson } from "./http.js";
import { serperSearch } from "./serper.js";
import type { RedditComment, RedditPost, SearchOptions } from "./types.js";
type RedditListing = Array<{
data?: {
children?: Array<{
data?: {
title?: string;
selftext?: string;
body?: string;
subreddit?: string;
permalink?: string;
score?: number;
num_comments?: number;
author?: string;
created_utc?: number;
};
}>;
};
}>;
export async function searchReddit(opts: SearchOptions): Promise<RedditPost[]> {
const {
query,
subreddits = [],
limit = 20,
period = "week",
comments = false,
} = opts;
const queries: string[] =
subreddits.length > 0
? subreddits
.slice(0, 5)
.map((s) => `${query} site:reddit.com/r/${s.replace(/^r\//, "")}`)
: [`${query} site:reddit.com`];
const postUrls = new Set<string>();
for (const q of queries) {
const result = await serperSearch(q, { period });
for (const item of result.organic ?? []) {
if (
item.link.includes("reddit.com") &&
item.link.includes("/comments/")
) {
const clean = item.link.replace(
/(\/comments\/[^/]+\/[^/]+)\/.*/,
"$1/",
);
postUrls.add(clean);
}
}
}
const urls = [...postUrls].slice(0, limit * 2);
const posts: RedditPost[] = [];
for (const url of urls) {
const post = await fetchPostByUrl(url, comments);
if (post) posts.push(post);
}
posts.sort((a, b) => b.score + b.numComments - (a.score + a.numComments));
return posts.slice(0, limit);
}
export async function fetchPostByUrl(
url: string,
includeComments: boolean,
): Promise<RedditPost | null> {
const jsonUrl = url.replace(/\/?$/, ".json");
const result = await fetchJson(jsonUrl);
if (!result.ok || !result.data) return null;
const data = result.data as RedditListing;
const postData = data[0]?.data?.children?.[0]?.data;
if (!postData) return null;
const post: RedditPost = {
title: postData.title ?? "",
body: (postData.selftext ?? "").slice(0, 2000),
subreddit: postData.subreddit ?? "",
url: `https://reddit.com${postData.permalink ?? ""}`,
score: postData.score ?? 0,
numComments: postData.num_comments ?? 0,
author: postData.author ?? "",
created: postData.created_utc
? new Date(postData.created_utc * 1000).toISOString()
: "",
};
if (includeComments) {
post.topComments = parseComments(data);
}
return post;
}
function parseComments(data: RedditListing): RedditComment[] {
const listing = data[1]?.data?.children ?? [];
const comments: RedditComment[] = [];
for (const child of listing) {
const c = child.data;
if (!c || !c.author) continue;
if (c.author === "AutoModerator") continue;
comments.push({
author: c.author,
body: (c.body ?? "").slice(0, 500),
score: c.score ?? 0,
});
if (comments.length >= 10) break;
}
return comments;
}
export async function discoverSubreddits(niche: string): Promise<string[]> {
const result = await serperSearch(
`best subreddits for ${niche} site:reddit.com`,
{
num: 20,
},
);
const subs = new Set<string>();
for (const item of result.organic ?? []) {
const match = item.link.match(/reddit\.com\/r\/([A-Za-z0-9_]+)/);
if (match) subs.add(match[1]);
}
return [...subs].slice(0, 10);
}

Xet Storage Details

Size:
3.48 kB
·
Xet hash:
1745750a136dc2bd9bdc9baca5917a19b2fb1ffa3efaf4baf2c85cfa2c3a5c98

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