Repository navigation
Expand file tree
/
Copy pathtest.ts
More file actions
371 lines (315 loc) · 11.2 KB
/
Copy pathtest.ts
File metadata and controls
371 lines (315 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* News Extraction & Classification Pipeline
*
* This script fetches news content in markdown form using a proxy scraping API (`r.jina.ai`),
* parses and structures it into articles, optionally classifies them using an AI-based
* zero-shot classifier (e.g., OpenAI's embedding models), and groups them into categories.
*
* Sources are defined per label (e.g., "breaking_news") per site (e.g., "bbc", "apnews").
* Input and output directories are read from environment variables.
*
* Usage:
* bun run script.ts <source> <label>
* Or used programmatically via `processMultipleSources()` and a pre-defined config.
*/
import { unified } from "unified";
import remarkParse from "remark-parse";
import { visit } from "unist-util-visit";
import { getEnv } from "./core/env.ts";
import { ClassificationService } from "./core/classification-service.ts";
export type NewsExtractionResponse = {
title: string;
link: string;
description?: string | null;
image?: {
url: string;
caption?: string | null;
};
};
const getMd = (file: string) => Bun.file(`${getEnv("INPUT_DIR")}/${file}`)
const getJson = (file: string) => Bun.file(`${getEnv("OUTPUT_DIR")}/${file}`);
export type Source = "apnews" | "bbc" | "reuters" | "aljazeera"
export type FetchMarkdownArgs = {
url: string;
selector: string;
};
export type SaveMarkdownArgs = {
filename: string;
content: string;
};
export type ExtractNewsArgs = {
markdown: string;
ignoreList: string[];
};
export type ProcessNewsArgs = {
url: string;
filename: string;
selector: string;
};
export type ExtractNewsFromFileArgs = {
filename: string;
ignoreList: string[];
};
/**
* Fetches markdown content from a given URL using Jina’s reverse proxy and selector.
*
* @param url - The target URL to scrape.
* @param selector - CSS selector used by the proxy to extract content.
* @returns Raw markdown string extracted from the target page.
* @throws If the HTTP response fails.
*/
export async function fetchMarkdownContent({
url,
selector,
}: FetchMarkdownArgs): Promise<string> {
const response = await fetch(`https://r.jina.ai/${url}`, {
headers: {
Authorization: `Bearer ${getEnv("JINA_API_KEY")}`,
"x-target-selector": selector,
},
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.text();
}
/**
* Determines if a given article title should be ignored based on ignore patterns.
*
* @param title - The article title to evaluate.
* @param ignoreList - List of string filters to match against.
* @returns True if the title matches any filter.
*/
export function shouldIgnoreTitle(title: string, ignoreList: string[]): boolean {
return ignoreList.some((filter) => title.includes(filter));
}
/**
* Extracts all unique external links from markdown content.
*
* @param markdown - Raw markdown input.
* @returns A set of unique HTTP(S) URLs.
*/
export function extractUniqueLinks(markdown: string): Set<string> {
const tree = unified().use(remarkParse).parse(markdown);
const links = new Set<string>();
visit(tree, "link", (node) => {
if (typeof node.url === "string" && node.url.startsWith("http")) {
links.add(node.url);
}
});
return links;
}
/**
* Parses markdown and structures news articles with titles, links, and optional images.
* Handles inline image captions and de-duplicates entries.
*
* @param markdown - Raw markdown string from a source.
* @param ignoreList - List of title patterns to exclude.
* @returns An array of structured news articles.
*/
export function extractNewsArticles({
markdown,
ignoreList,
}: ExtractNewsArgs): NewsExtractionResponse[] {
const tree = unified().use(remarkParse).parse(markdown);
const articles = new Map<string, NewsExtractionResponse>();
let lastImage: NewsExtractionResponse["image"] | null = null;
visit(tree, "link", (node) => {
const url = node.url;
if (!url.startsWith("http")) return;
const title = node.children
.filter((child) => child.type === "text")
.map((child) => child.value)
.join("")
.trim();
// Handle inline images
if (node.children.length > 0 && node.children[0].type === "image") {
const imageNode = node.children[0];
let caption = imageNode.alt?.trim();
if (caption) {
caption = caption.replace(/^Image \d+:\s*/, "");
}
lastImage = { url: imageNode.url, caption };
return;
}
// Assign title or fallback to last image caption
const finalTitle = title || lastImage?.caption;
if (!finalTitle) return;
if (shouldIgnoreTitle(finalTitle.trim(), ignoreList)) return;
if (!articles.has(url)) {
articles.set(url, { title: finalTitle, link: url });
}
// Attach image if available
if (lastImage) {
if (lastImage.caption === finalTitle) {
lastImage.caption = null;
}
articles.get(url)!.image = lastImage;
lastImage = null;
}
});
return Array.from(articles.values());
}
export interface DownloadAndExtractArgs {
cats: string[];
ignored: string[];
label: string;
config: { selector: string, urls: { label: string; url: string; filename: string }[] };
}
/**
* Loads markdown for a given site/label combo, either from disk or by fetching it.
* Then extracts articles into structured form.
*
* @param cats - Category labels to be used downstream.
* @param label - Label for the content (e.g., "breaking_news").
* @param config - Site configuration containing selector and URL+filename mapping.
* @param ignored - List of patterns to filter out irrelevant articles.
* @returns Structured article list (not yet classified).
*/
async function downloadAndExtract({cats, label, config, ignored}: DownloadAndExtractArgs) {
const { urls } = config;
const cat = urls.find((url) => url.label === label);
if (!cat) {
throw new Error(`Category not found: ${label}`);
}
const mdFile = getMd(`${cat.filename}.md`);
const exists = await mdFile.exists();
let markdown: string;
if (!exists) {
console.log(`File not found: ${cat.filename}.md, fetching...`);
markdown = await fetchMarkdownContent({
url: cat.url,
selector: config.selector,
})
console.log(`Saving to ${cat.filename}.md...`);
await Bun.write(mdFile, markdown);
} else {
markdown = await mdFile.text();
}
// const jsonFile = getJson(`${cat.filename}.json`);
const structuredNews = extractNewsArticles({ markdown, ignoreList: ignored });
return structuredNews
// Bun.write(jsonFile, JSON.stringify(structuredNews, null, 2));
// const classificationResults = await classifyNewsArticles(structuredNews, cats);
// return classificationResults
// console.log(classificationResults)
// console.log(`Extracted ${structuredNews.length} articles from ${cat.filename}.md`);
}
/**
* Uses a zero-shot classifier to assign categories to structured news articles.
*
* @param structuredNews - Array of news articles with `title` and `link`.
* @param categories - Labels to classify into (e.g., politics, tech).
* @returns An object mapping each category to its matched articles.
* @throws If no input articles are provided.
*/
export async function classifyNewsArticles(
structuredNews: NewsExtractionResponse[],
categories: string[]
): Promise<{ [category: string]: NewsExtractionResponse[] }> {
if (!structuredNews.length) {
throw new Error("No articles provided for classification.");
}
console.log(`Classifying ${structuredNews.length} articles...`);
// Initialize the classifier with `ai-zero-shot-classifier`
const classifier = await ClassificationService.instance(
"openai", // AI provider (e.g., "openai", "groq")
"text-embedding-3-small", // Model for text classification
getEnv("OPENAI_API_KEY"), // API key (ensure it's set)
categories,
(article: NewsExtractionResponse) => article.title // Use article titles as input
);
// Perform classification and group by category
const groupedArticles = await classifier.groupByCategory(structuredNews);
console.log(`Classification complete. Grouped into ${Object.keys(groupedArticles).length} categories.`);
return groupedArticles;
}
/**
* Processes and classifies news across multiple sources and labels.
* Aggregates all results into a master category map and writes final output.
*
* @param sourcesWithLabels - Mapping of source → label list (e.g., { bbc: ["breaking_news"] }).
* @returns Master dictionary mapping each category to matched articles (with source info).
*/
async function processMultipleSources(sourcesWithLabels: { [source: string]: string[] }) {
const ignored = await Bun.file("./src/ignore.json").json();
const sites = await Bun.file("./src/sites.json").json();
const categories = await Bun.file("./src/categories.json").json();
const masterCategorizedData: { [category: string]: { title: string; link: string; source: string }[] } = {};
for (const [source, labels] of Object.entries(sourcesWithLabels)) {
const config = sites[source as Source];
if (!config) {
console.error(`Skipping: Site not found: ${source}`);
continue;
}
for (const label of labels) {
console.log(`Processing ${source} - ${label}...`);
try {
// Download and classify articles
const structuredNews = await downloadAndExtract({
cats: categories,
ignored,
label,
config,
});
// Perform classification
const classifiedArticles = await classifyNewsArticles(structuredNews, categories);
// Accumulate into master set
for (const [category, articles] of Object.entries(classifiedArticles)) {
if (!masterCategorizedData[category]) {
masterCategorizedData[category] = [];
}
articles.forEach((article) => {
masterCategorizedData[category].push({
title: article.title,
link: article.link,
source,
});
});
}
console.log(`✅ Finished processing: ${source} - ${label}`);
} catch (error) {
console.error(`❌ Error processing ${source} - ${label}:`, error);
}
}
}
console.log("🔥 All sources processed!");
return masterCategorizedData;
}
const sourcesWithLabels = {
aljazeera: ["breaking_news"],
bbc: ["breaking_news"],
apnews: ["breaking_news"],
reuters: ["breaking_news"],
};
(async () => {
const result = await processMultipleSources(sourcesWithLabels);
await Bun.write(getJson("output.json"), JSON.stringify(result, null, 2));
})();
async function main() {
const ignored = await Bun.file('./src/ignore.json').json()
const sites = await Bun.file('./src/sites.json').json()
const categories = await Bun.file('./src/categories.json').json()
const args = process.argv.slice(2); // Skip the first two elements (bun and script name)
if (args.length !== 2) {
console.error("Usage: bun run your-script.ts <label> <source>");
process.exit(1);
}
const [source, label] = args;
const config = sites[source as Source];
if (!config) {
throw new Error(`Site not found: ${source}`);
}
try {
await downloadAndExtract({
cats: categories,
ignored,
label,
config,
});
} catch (err) {
console.error("An error occurred:", err);
process.exit(1);
}
}
// main();