forked from pollinations/pollinations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeed-filter-cli.js
More file actions
executable file
·630 lines (543 loc) · 21.4 KB
/
feed-filter-cli.js
File metadata and controls
executable file
·630 lines (543 loc) · 21.4 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#!/usr/bin/env node
import * as EventSource from "eventsource";
import debug from "debug";
import dotenv from "dotenv";
import fs from "fs";
import path from "path";
import { Command } from "commander";
import dns from "dns";
import { promisify } from "util";
// Promisify DNS lookup
const dnsReverse = promisify(dns.reverse);
// Load environment variables for feed password
dotenv.config();
const log = debug("pollinations:filter");
debug.enable("pollinations:filter");
// Enhanced stats for tracking message counts and categorical properties
const stats = {
total: 0,
private: 0,
public: 0,
models: {},
referrers: {},
hasMarkdown: 0,
hasHtml: 0,
isRoblox: 0,
isImagePollinationsReferrer: 0,
tokens: {
total: 0,
byReferrer: {},
},
ips: {},
};
// Number of top IPs to track
const TOP_IPS_COUNT = 5;
// Number of top referrers to display
const TOP_REFERRERS_COUNT = 5;
// Number of top models to display
const TOP_MODELS_COUNT = 5;
// Array to store raw message data for JSON export
const rawMessageData = [];
// Helper functions for content detection
const hasMarkdown = (text) => {
const markdownPatterns = [
/#{1,6}\s+.+/, // Headers
/\[.+\]\(.+\)/, // Links
/\*\*.+\*\*/, // Bold
/`.+`/, // Code
/^\s*[-*+]\s+/m, // Lists
/^\s*\d+\.\s+/m, // Numbered lists
];
return markdownPatterns.some((pattern) => pattern.test(text));
};
const hasHtml = (text) => /<[^>]+>/i.test(text);
// Helper function to truncate text
const truncate = (text, maxLength = 300) => {
if (!text) return "";
return text.length > maxLength ? text.slice(0, maxLength) + "..." : text;
};
// Helper function to convert messages to markdown
const messagesToMarkdown = (messages) => {
if (!Array.isArray(messages)) return "";
return messages
.map((msg) => `**${msg.role}**:\n\n${truncate(msg.content)}\n`)
.join("\n---\n\n");
};
// Helper function to increment counter for a category
const incrementCategoryCount = (category, value) => {
if (!value) return;
if (!stats[category][value]) {
stats[category][value] = 1;
} else {
stats[category][value]++;
}
};
// Helper function to track tokens by referrer
const trackTokensByReferrer = (referrer, tokenData) => {
if (!referrer) referrer = "unknown";
// Initialize referrer token tracking if not exists
if (!stats.tokens.byReferrer[referrer]) {
stats.tokens.byReferrer[referrer] = {
total: 0,
};
}
// Extract token counts from the data
const totalTokens = tokenData?.total_tokens || 0;
// Update token counts
stats.tokens.total += totalTokens;
stats.tokens.byReferrer[referrer].total += totalTokens;
};
// Helper function to track IP addresses
const trackIpAddress = (ip) => {
if (!ip) return;
// Store the full IP
const fullIp = ip;
if (!stats.ips[fullIp]) {
stats.ips[fullIp] = {
requests: 1,
tokens: 0,
};
} else {
stats.ips[fullIp].requests++;
}
};
// Helper function to resolve IP to hostname - only called for top IPs when printing stats
const resolveIpToHostname = async (ip) => {
try {
const hostnames = await dnsReverse(ip);
return hostnames && hostnames.length > 0 ? hostnames[0] : null;
} catch (error) {
// Silently fail if resolution doesn't work
return null;
}
};
// Helper function to update IP token usage
const updateIpTokenUsage = (ip, tokens) => {
if (!ip || !tokens) return;
const fullIp = ip;
if (stats.ips[fullIp]) {
stats.ips[fullIp].tokens += tokens;
}
};
// Helper function to calculate percentage
const calculatePercentage = (count, total) => {
if (total === 0) return "0.00%";
return ((count / total) * 100).toFixed(2) + "%";
};
// Helper function to sort object by values in descending order
const sortObjectByValues = (obj) => {
return Object.entries(obj)
.sort((a, b) => b[1] - a[1])
.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
};
// Helper function to get top N items from an object
const getTopNItems = (obj, n, valueKey = null) => {
let entries = Object.entries(obj);
// If valueKey is provided, sort by that specific property
if (valueKey) {
entries = entries.sort((a, b) => b[1][valueKey] - a[1][valueKey]);
} else {
entries = entries.sort((a, b) => b[1] - a[1]);
}
return entries.slice(0, n).reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
};
// Filter function that checks if a message matches the criteria
const matchesFilters = (data, options = {}) => {
const { response, parameters, isPrivate } = data;
const referrer = parameters?.referrer;
// Check for Roblox first - this is a hard filter that applies to all messages
// regardless of other filters
if (!options.roblox) {
// Check referrer, model name, and system prompt for Roblox
const isRobloxModel =
parameters?.model &&
parameters.model.toLowerCase().includes("roblox");
if (isRobloxModel) {
return false;
}
}
// Filter by privacy status
if (options.onlyPrivate && !isPrivate) return false;
if (options.onlyPublic && isPrivate) return false;
// NEW: Filter out based on referrer substrings
if (
options.excludeReferrerSubstring &&
options.excludeReferrerSubstring.length > 0
) {
if (referrer) {
// Only check if referrer exists
const lowerCaseReferrer = referrer.toLowerCase();
for (const substring of options.excludeReferrerSubstring) {
if (lowerCaseReferrer.includes(substring.toLowerCase())) {
return false; // Exclude if any substring matches
}
}
}
}
// Filter IN based on specific referrer value/regex
if (options.referrer !== undefined) {
if (options.referrer === false && referrer) return false; // --no-referrer
if (
options.referrer instanceof RegExp &&
(!referrer || !options.referrer.test(referrer))
)
return false;
if (
typeof options.referrer === "string" &&
referrer !== options.referrer
)
return false;
}
// Check for markdown OR html if either option is specified
if (options.hasMarkdown !== undefined || options.hasHtml !== undefined) {
const hasMarkdownMatch =
options.hasMarkdown === undefined ||
hasMarkdown(response) === options.hasMarkdown;
const hasHtmlMatch =
options.hasHtml === undefined ||
hasHtml(response) === options.hasHtml;
if (!hasMarkdownMatch && !hasHtmlMatch) return false;
}
return true;
};
// Print current stats
const printStats = async () => {
log("\nStats:");
log(`Total messages: ${stats.total}`);
log(
`Public messages: ${stats.public} (${calculatePercentage(stats.public, stats.total)})`,
);
log(
`Private messages: ${stats.private} (${calculatePercentage(stats.private, stats.total)})`,
);
if (stats.hasMarkdown > 0 || stats.hasHtml > 0) {
log("\nContent Types:");
log(
`Contains Markdown: ${stats.hasMarkdown} (${calculatePercentage(stats.hasMarkdown, stats.total)})`,
);
log(
`Contains HTML: ${stats.hasHtml} (${calculatePercentage(stats.hasHtml, stats.total)})`,
);
}
if (Object.keys(stats.models).length > 0) {
log("\nModels:");
const topModels = getTopNItems(stats.models, TOP_MODELS_COUNT);
Object.entries(topModels).forEach(([model, count]) => {
log(
`${model}: ${count} (${calculatePercentage(count, stats.total)})`,
);
});
// Show how many more models exist beyond the top ones
const totalModels = Object.keys(stats.models).length;
if (totalModels > TOP_MODELS_COUNT) {
log(`... and ${totalModels - TOP_MODELS_COUNT} more models`);
}
}
if (Object.keys(stats.referrers).length > 0) {
log("\nTop Referrers:");
const topReferrers = getTopNItems(stats.referrers, TOP_REFERRERS_COUNT);
Object.entries(topReferrers).forEach(([referrer, count]) => {
log(
`${referrer || "none"}: ${count} (${calculatePercentage(count, stats.total)})`,
);
});
// Show how many more referrers exist beyond the top ones
const totalReferrers = Object.keys(stats.referrers).length;
if (totalReferrers > TOP_REFERRERS_COUNT) {
log(
`... and ${totalReferrers - TOP_REFERRERS_COUNT} more referrers`,
);
}
}
log("\nSpecial Referrers:");
log(
`Roblox: ${stats.isRoblox} (${calculatePercentage(stats.isRoblox, stats.total)})`,
);
log(
`Image Pollinations: ${stats.isImagePollinationsReferrer} (${calculatePercentage(stats.isImagePollinationsReferrer, stats.total)})`,
);
// Print token statistics by referrer
if (stats.tokens.total > 0) {
log("\nToken Usage:");
log(`Total Tokens: ${stats.tokens.total.toLocaleString()}`);
log("\nToken Usage by Referrer:");
const sortedReferrersByTokens = Object.entries(stats.tokens.byReferrer)
.sort((a, b) => b[1].total - a[1].total)
.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
Object.entries(sortedReferrersByTokens).forEach(
([referrer, tokenData]) => {
log(
`${referrer}: ${tokenData.total.toLocaleString()} tokens (${calculatePercentage(tokenData.total, stats.tokens.total)})`,
);
},
);
}
// Print top IP statistics - only one list sorted by requests
if (Object.keys(stats.ips).length > 0) {
log("\nTop IP Addresses by Requests:");
const topIps = getTopNItems(stats.ips, TOP_IPS_COUNT, "requests");
// Resolve hostnames for top IPs only when displaying stats
const resolvePromises = Object.keys(topIps).map(async (ip) => {
const hostname = await resolveIpToHostname(ip);
const hostnameInfo = hostname ? ` (${hostname})` : "";
log(
`${ip}${hostnameInfo}: ${topIps[ip].requests} requests, ${topIps[ip].tokens.toLocaleString()} tokens`,
);
});
// Wait for all resolutions to complete before continuing
await Promise.all(resolvePromises);
}
log("-".repeat(30));
};
// Main function to start the feed listener
const startFeedListener = async (options = {}) => {
// Determine which feed URL to use based on options
const baseUrl = options.baseUrl || "https://text.pollinations.ai";
let feedUrl = `${baseUrl}/feed`;
// Check for password first (from options or environment)
const password = options.password || process.env.FEED_PASSWORD;
// If a password is provided, use the authenticated feed URL
if (password) {
feedUrl = `${baseUrl}/feed?password=${encodeURIComponent(password)}`;
log("Using authenticated feed (includes private messages)");
}
log(
`Connecting to feed at ${feedUrl.replace(/password=([^&]+)/, "password=[REDACTED]")}`,
);
const eventSource = new EventSource.EventSource(feedUrl);
eventSource.onmessage = async (event) => {
try {
const data = JSON.parse(event.data);
// Check filters FIRST
if (matchesFilters(data, options)) {
// If JSON logging is enabled AND the message passes filters, save the raw data
if (options.jsonOutputFile) {
// Extract only the metadata and numerical information we need
const { parameters, response, isPrivate, ip } = data;
const messageData = {
timestamp: new Date().toISOString(),
isPrivate: isPrivate || false,
ip: ip ? ip : "unknown",
metadata: {
referrer: parameters?.referrer || "unknown",
model: parameters?.model || "unknown",
isImagePollinationsReferrer:
parameters?.isImagePollinationsReferrer ||
false,
},
stats: {
promptTokens: parameters?.prompt_tokens || 0,
completionTokens:
parameters?.completion_tokens || 0,
totalTokens: parameters?.total_tokens || 0,
promptLength: {
characters: parameters?.messages
? parameters.messages.reduce(
(sum, msg) =>
sum + (msg.content?.length || 0),
0,
)
: 0,
words: parameters?.messages
? parameters.messages.reduce(
(sum, msg) =>
sum +
(msg.content?.split(/\s+/)
.length || 0),
0,
)
: 0,
},
completionLength: {
characters: response?.length || 0,
words: response?.split(/\s+/).length || 0,
},
hasMarkdown: hasMarkdown(response || ""),
hasHtml: hasHtml(response || ""),
// Attempt to detect if the message is a roleplay based on asterisks
isRoleplay:
/\*.+\*/.test(response || "") ||
parameters?.messages?.some((msg) =>
/\*.+\*/.test(msg.content || ""),
) ||
false,
},
// Store the first 100 chars of system message if available (for prompt analysis)
systemPromptPreview:
parameters?.messages
?.find((msg) => msg.role === "system")
?.content?.substring(0, 100) || null,
};
// Save to our array
rawMessageData.push(messageData);
// Write to file periodically to avoid memory issues
if (rawMessageData.length % 10 === 0) {
writeDataToJson(options.jsonOutputFile);
}
}
// --- Update stats and console output (only happens if filters match) ---
stats.total++;
if (data.isPrivate) {
stats.private++;
} else {
stats.public++;
}
// Track categorical properties
const { parameters, response, ip } = data;
// Track model usage
if (parameters?.model) {
incrementCategoryCount("models", parameters.model);
}
// Track referrers
incrementCategoryCount("referrers", parameters?.referrer);
// Track content types
if (hasMarkdown(response)) {
stats.hasMarkdown++;
}
if (hasHtml(response)) {
stats.hasHtml++;
}
// Track special referrers
if (
parameters?.referrer &&
parameters.referrer.toLowerCase().includes("roblox")
) {
stats.isRoblox++;
}
if (parameters?.isImagePollinationsReferrer) {
stats.isImagePollinationsReferrer++;
}
// Track token usage by referrer
if (parameters) {
trackTokensByReferrer(parameters.referrer, parameters);
}
// Track IP address usage
if (ip) {
trackIpAddress(ip);
// Update token usage for this IP
if (parameters?.total_tokens) {
updateIpTokenUsage(ip, parameters.total_tokens);
}
}
// Only display content if not in count-only mode
if (!options.countOnly) {
// Add privacy indicator to the output
const privacyStatus = data.isPrivate
? "🔒 PRIVATE"
: "🌐 PUBLIC";
console.log(`\n# ${privacyStatus} Message\n`);
console.log(
`Referrer: ${data.parameters?.referrer || "none"}`,
);
if (data.ip) console.log(`IP: ${data.ip}`);
// Add token information if available
if (data.parameters?.total_tokens) {
console.log(
`Tokens: ${data.parameters.total_tokens.toLocaleString()} total`,
);
}
console.log("\n# Response\n");
console.log(truncate(data.response));
console.log("\n# Messages\n");
console.log(messagesToMarkdown(data.parameters.messages));
console.log("\n-------------------\n");
}
// Print stats based on mode
if (options.countOnly) {
// In count-only mode, update stats more frequently
if (stats.total % 5 === 0) {
await printStats();
}
} else if (stats.total % 10 === 0) {
// In normal mode, print stats every 10 messages
await printStats();
}
}
} catch (error) {
log("Error processing message: %O", error);
}
};
eventSource.onerror = (error) => {
if (error && error.status === 401) {
log("Authentication failed: Invalid password");
process.exit(1);
} else {
log("EventSource failed: %O", error);
}
};
log("Connected to feed with filters: %O", options);
// Handle cleanup on process exit
process.on("SIGINT", async () => {
eventSource.close();
await printStats();
// Save data to JSON if enabled
if (options.jsonOutputFile) {
writeDataToJson(options.jsonOutputFile);
log(`Data saved to ${options.jsonOutputFile}`);
}
log("Disconnected from feed");
process.exit(0);
});
};
// Function to write collected data to JSON file
const writeDataToJson = (filePath) => {
try {
// Create directory if it doesn't exist
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(filePath, JSON.stringify(rawMessageData, null, 2));
log(`Data saved to ${filePath} (${rawMessageData.length} entries)`);
} catch (error) {
log("Error writing to JSON file: %O", error);
}
};
// Parse command line arguments
const parseArgs = () => {
const program = new Command();
program
.option("--no-referrer", "Only show messages without a referrer")
.option("--referrer <value>", "Filter by specific referrer")
.option("--has-markdown", "Only show messages containing markdown")
.option("--has-html", "Only show messages containing HTML")
.option("--no-roblox", "Filter out messages with Roblox referrers")
.option(
"--exclude-referrer-substring <value>",
"Filter out messages where referrer contains this substring (can be used multiple times)",
(value, previous) => (previous || []).concat([value]),
[],
)
.option(
"--only-private",
"Show only private messages (requires password)",
)
.option("--only-public", "Show only public messages")
.option("--password <value>", "Password for accessing private messages")
.option(
"--base-url <url>",
"Base URL for API (default: https://text.pollinations.ai)",
)
.option(
"--count-only",
"Only display statistics, not individual messages",
)
.option(
"--json-output <file>",
"Save raw data to JSON file for later analysis",
);
program.parse(process.argv);
return program.opts();
};
// Start the application
const options = parseArgs();
startFeedListener(options);