@apmantza/greedysearch-pi 1.8.2 → 1.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,9 +8,9 @@ import { tmpdir } from "node:os";
8
8
  import { join } from "node:path";
9
9
  import { fetchSourceHttp, shouldUseBrowser } from "../fetcher.mjs";
10
10
  import { fetchGitHubContent, parseGitHubUrl } from "../github.mjs";
11
+ import { fetchRedditContent, parseRedditUrl } from "../reddit.mjs";
11
12
  import { trimContentHeadTail } from "../utils/content.mjs";
12
- import { cdp } from "./chrome.mjs";
13
- import { openNewTab, closeTab, closeTabs } from "./chrome.mjs";
13
+ import { cdp, closeTab, closeTabs, openNewTab } from "./chrome.mjs";
14
14
  import { SOURCE_FETCH_CONCURRENCY } from "./constants.mjs";
15
15
  import { trimText } from "./sources.mjs";
16
16
 
@@ -50,6 +50,38 @@ export async function fetchSourceContent(url, maxChars = 8000) {
50
50
  }
51
51
  }
52
52
 
53
+ // Check if it's a Reddit URL (posts and comments)
54
+ const redditInfo = parseRedditUrl(url);
55
+ if (redditInfo?.type === "post") {
56
+ process.stderr.write(
57
+ `[greedysearch] Using Reddit JSON API for: ${url.slice(0, 60)}...\n`,
58
+ );
59
+ const redditResult = await fetchRedditContent(url, maxChars);
60
+ if (redditResult.ok) {
61
+ const content = trimContentHeadTail(redditResult.markdown, maxChars);
62
+ return {
63
+ url,
64
+ finalUrl: redditResult.finalUrl,
65
+ status: redditResult.status,
66
+ contentType: "text/markdown",
67
+ lastModified: redditResult.lastModified || "",
68
+ publishedTime: redditResult.publishedTime || "",
69
+ byline: redditResult.byline || "",
70
+ siteName: redditResult.siteName || "",
71
+ lang: redditResult.lang || "",
72
+ title: redditResult.title,
73
+ snippet: redditResult.excerpt,
74
+ content,
75
+ contentChars: content.length,
76
+ source: "reddit-api",
77
+ duration: Date.now() - start,
78
+ };
79
+ }
80
+ process.stderr.write(
81
+ `[greedysearch] Reddit API fetch failed, falling back to HTTP: ${redditResult.error}\n`,
82
+ );
83
+ }
84
+
53
85
  // Try HTTP first
54
86
  const httpResult = await fetchSourceHttp(url, { timeoutMs: 15000 });
55
87
 
@@ -227,4 +259,4 @@ export async function fetchTopSource(url) {
227
259
  } finally {
228
260
  await closeTab(tab);
229
261
  }
230
- }
262
+ }
@@ -1,59 +1,59 @@
1
- // src/search/output.mjs — Output serialization for search results
2
- //
3
- // Extracted from search.mjs.
4
-
5
- import { existsSync, mkdirSync, writeFileSync } from "node:fs";
6
- import { join } from "node:path";
7
- import { tmpdir } from "node:os";
8
-
9
- const __dir = import.meta.dirname || new URL(".", import.meta.url).pathname.replace(/^\/([A-Z]:)/, "$1");
10
-
11
- export function slugify(query) {
12
- return query
13
- .toLowerCase()
14
- .replace(/[^a-z0-9]+/g, "-")
15
- .replace(/^-|-$/g, "")
16
- .slice(0, 60);
17
- }
18
-
19
- export function resultsDir() {
20
- const dir = join(__dir, "..", "..", "results");
21
- mkdirSync(dir, { recursive: true });
22
- return dir;
23
- }
24
-
25
- export function writeOutput(
26
- data,
27
- outFile,
28
- { inline = false, synthesize = false, query = "" } = {},
29
- ) {
30
- const json = `${JSON.stringify(data, null, 2)}\n`;
31
-
32
- if (outFile) {
33
- writeFileSync(outFile, json, "utf8");
34
- process.stderr.write(`Results written to ${outFile}\n`);
35
- return;
36
- }
37
-
38
- if (inline) {
39
- process.stdout.write(json);
40
- return;
41
- }
42
-
43
- const ts = new Date()
44
- .toISOString()
45
- .replace("T", "_")
46
- .replace(/[:.]/g, "-")
47
- .slice(0, 19);
48
- const slug = slugify(query);
49
- const base = join(resultsDir(), `${ts}_${slug}`);
50
-
51
- writeFileSync(`${base}.json`, json, "utf8");
52
-
53
- if (synthesize && data._synthesis?.answer) {
54
- writeFileSync(`${base}-synthesis.md`, data._synthesis.answer, "utf8");
55
- process.stdout.write(`${base}-synthesis.md\n`);
56
- } else {
57
- process.stdout.write(`${base}.json\n`);
58
- }
1
+ // src/search/output.mjs — Output serialization for search results
2
+ //
3
+ // Extracted from search.mjs.
4
+
5
+ import { existsSync, mkdirSync, writeFileSync } from "node:fs";
6
+ import { join } from "node:path";
7
+ import { tmpdir } from "node:os";
8
+
9
+ const __dir = import.meta.dirname || new URL(".", import.meta.url).pathname.replace(/^\/([A-Z]:)/, "$1");
10
+
11
+ export function slugify(query) {
12
+ return query
13
+ .toLowerCase()
14
+ .replace(/[^a-z0-9]+/g, "-")
15
+ .replace(/^-|-$/g, "")
16
+ .slice(0, 60);
17
+ }
18
+
19
+ export function resultsDir() {
20
+ const dir = join(__dir, "..", "..", "results");
21
+ mkdirSync(dir, { recursive: true });
22
+ return dir;
23
+ }
24
+
25
+ export function writeOutput(
26
+ data,
27
+ outFile,
28
+ { inline = false, synthesize = false, query = "" } = {},
29
+ ) {
30
+ const json = `${JSON.stringify(data, null, 2)}\n`;
31
+
32
+ if (outFile) {
33
+ writeFileSync(outFile, json, "utf8");
34
+ process.stderr.write(`Results written to ${outFile}\n`);
35
+ return;
36
+ }
37
+
38
+ if (inline) {
39
+ process.stdout.write(json);
40
+ return;
41
+ }
42
+
43
+ const ts = new Date()
44
+ .toISOString()
45
+ .replace("T", "_")
46
+ .replace(/[:.]/g, "-")
47
+ .slice(0, 19);
48
+ const slug = slugify(query);
49
+ const base = join(resultsDir(), `${ts}_${slug}`);
50
+
51
+ writeFileSync(`${base}.json`, json, "utf8");
52
+
53
+ if (synthesize && data._synthesis?.answer) {
54
+ writeFileSync(`${base}-synthesis.md`, data._synthesis.answer, "utf8");
55
+ process.stdout.write(`${base}-synthesis.md\n`);
56
+ } else {
57
+ process.stdout.write(`${base}.json\n`);
58
+ }
59
59
  }