@jonandev/longbeard 0.5.2 → 0.6.0

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.
@@ -1,8 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
  // List Dribbble search/popular cards from a listing URL.
3
- // Headless Chrome; prints TSV: id-slug\tviews\timageURL\ttitle
3
+ // Headless Chrome; prints TSV: id-slug\tlikes\tviews\timageURL\ttitle
4
+ // Counts come from the card's own stat elements. A column it cannot read
5
+ // stays empty. It never guesses a count from the title.
6
+ //
7
+ // Off-topic cards (merch, resumes, worksheets, posters) are dropped by slug
8
+ // and title before anything is fetched, and the noise rate goes to stderr.
4
9
  //
5
10
  // node dribbble-cards.mjs "https://dribbble.com/search/shots/popular/product-design?q=writing+app&timeframe=year" --scrolls 4
11
+ // node dribbble-cards.mjs "<url>" --drop "mood,habit" --keep "font,illustration"
6
12
  import { spawn } from "node:child_process";
7
13
  import { existsSync, mkdtempSync, rmSync } from "node:fs";
8
14
  import { tmpdir } from "node:os";
@@ -16,15 +22,48 @@ const flag = (name, def) => {
16
22
  args.splice(i, 2);
17
23
  return v;
18
24
  };
25
+ const has = (name) => {
26
+ const i = args.indexOf(`--${name}`);
27
+ if (i === -1) return false;
28
+ args.splice(i, 1);
29
+ return true;
30
+ };
19
31
  const url = args[0];
20
32
  const scrolls = Math.max(0, Number(flag("scrolls", "3")) || 3);
33
+ const noFilter = has("no-filter");
34
+ const words = (s) =>
35
+ String(s || "")
36
+ .toLowerCase()
37
+ .split(",")
38
+ .map((w) => w.trim())
39
+ .filter(Boolean);
40
+ // The shared negative list. These sell the word, not the form: a search for
41
+ // "writing app" returns tees, CV services and tracing worksheets.
42
+ const OFF_TOPIC = [
43
+ "t-shirt", "tshirt", "tee", "merch", "apparel", "hoodie",
44
+ "resume", "cv", "cover letter", "essay",
45
+ "worksheet", "tracing", "handwriting practice", "alphabet",
46
+ "halloween", "christmas", "valentine",
47
+ "logo", "logotype", "branding", "brand identity", "packaging",
48
+ "poster", "book cover", "sticker", "mockup",
49
+ "chatbot", "mood tracker", "habit tracker",
50
+ ];
51
+ const keep = words(flag("keep", ""));
52
+ const dropWords = [...OFF_TOPIC.filter((w) => !keep.includes(w)), ...words(flag("drop", ""))];
21
53
  if (!url || !/^https?:\/\/(www\.)?dribbble\.com\//i.test(url)) {
22
54
  console.error(
23
- 'usage: node dribbble-cards.mjs "https://dribbble.com/search/..." [--scrolls 4]',
55
+ 'usage: node dribbble-cards.mjs "https://dribbble.com/search/..." [--scrolls 4] [--drop "a,b"] [--keep "a,b"] [--no-filter]',
24
56
  );
25
57
  process.exit(2);
26
58
  }
27
59
 
60
+ // Whole words only, so "ai" does not swallow "detail" and "cv" does not
61
+ // swallow "cvs". A hyphen in a slug counts as a space.
62
+ function offTopic(text) {
63
+ const hay = ` ${String(text).toLowerCase().replace(/[^a-z0-9]+/g, " ").trim()} `;
64
+ return dropWords.find((word) => hay.includes(` ${word.replace(/[^a-z0-9]+/g, " ")} `)) || "";
65
+ }
66
+
28
67
  function findChrome() {
29
68
  const env = process.env.CHROME_PATH;
30
69
  if (env && existsSync(env)) return env;
@@ -169,6 +208,38 @@ const { result } = await page("Runtime.evaluate", {
169
208
  expression: `(() => {
170
209
  const rows = [];
171
210
  const seen = new Set();
211
+ const tidy = (t) => (t || "").replace(/\\s+/g, "");
212
+ const isCount = (t) => /^[\\d][\\d.,]*[kKmM]?$/.test(t);
213
+ const hints = (el) =>
214
+ ["aria-label", "title", "class", "data-type", "data-test"]
215
+ .map((name) => el.getAttribute(name) || "")
216
+ .join(" ");
217
+ // A counter labelled as likes or views: its own text, or the one
218
+ // descendant that is nothing but a count.
219
+ const labelled = (li, word) => {
220
+ const re = new RegExp(word, "i");
221
+ for (const el of li.querySelectorAll("*")) {
222
+ if (!re.test(hints(el))) continue;
223
+ const own = tidy(el.textContent);
224
+ if (isCount(own)) return own;
225
+ for (const kid of el.querySelectorAll("*")) {
226
+ const t = tidy(kid.textContent);
227
+ if (isCount(t)) return t;
228
+ }
229
+ }
230
+ return "";
231
+ };
232
+ // Fallback: leaf elements whose whole text is a count, in DOM order.
233
+ // Dribbble prints likes first, then views. Free text is never read.
234
+ const leafCounts = (li) => {
235
+ const out = [];
236
+ for (const el of li.querySelectorAll("*")) {
237
+ if (el.children.length) continue;
238
+ const t = tidy(el.textContent);
239
+ if (isCount(t)) out.push(t);
240
+ }
241
+ return out;
242
+ };
172
243
  for (const li of document.querySelectorAll("li")) {
173
244
  const a = li.querySelector('a[href*="/shots/"]');
174
245
  if (!a) continue;
@@ -182,11 +253,15 @@ const { result } = await page("Runtime.evaluate", {
182
253
  const imageURL = raw.replace(/\\?.*$/, "");
183
254
  if (!imageURL) continue;
184
255
  seen.add(idSlug);
185
- const text = (li.innerText || "").replace(/\\s+/g, " ");
186
- const vm = text.match(/([\\d.]+\\s*[kKmM]?)\\s*views?/i) || text.match(/([\\d.]+\\s*[kKmM]?)/);
187
- const views = vm ? vm[1].replace(/\\s+/g, "") : "";
256
+ let likes = labelled(li, "like");
257
+ let views = labelled(li, "view");
258
+ if (!likes && !views) {
259
+ const counts = leafCounts(li);
260
+ likes = counts[0] || "";
261
+ views = counts[1] || "";
262
+ }
188
263
  const title = (img.getAttribute("alt") || a.getAttribute("title") || idSlug).replace(/\\t/g, " ").trim();
189
- rows.push([idSlug, views, imageURL, title].join("\\t"));
264
+ rows.push([idSlug, likes, views, imageURL, title].join("\\t"));
190
265
  }
191
266
  return rows.join("\\n");
192
267
  })()`,
@@ -195,10 +270,42 @@ const { result } = await page("Runtime.evaluate", {
195
270
 
196
271
  const tsv = result?.value || "";
197
272
  if (!tsv.trim()) {
198
- console.error("0 cards (empty listing or rate limit — retry once after a pause)");
273
+ console.error(
274
+ "0 cards (empty listing or rate limit — retry after 8s, then after 30s; if it is still 0, record it under the board's drops as `<query>: 0 cards (rate limit?)`)",
275
+ );
276
+ drop();
277
+ process.exit(1);
278
+ }
279
+
280
+ const all = tsv.split("\n").filter(Boolean);
281
+ const reasons = new Map();
282
+ const kept = noFilter
283
+ ? all
284
+ : all.filter((row) => {
285
+ const [idSlug, , , , title = ""] = row.split("\t");
286
+ const word = offTopic(`${idSlug} ${title}`);
287
+ if (!word) return true;
288
+ reasons.set(word, (reasons.get(word) ?? 0) + 1);
289
+ return false;
290
+ });
291
+ const cut = all.length - kept.length;
292
+ if (cut > 0) {
293
+ const top = [...reasons.entries()]
294
+ .sort((a, b) => b[1] - a[1])
295
+ .slice(0, 6)
296
+ .map(([word, n]) => `${word} ${n}`)
297
+ .join(", ");
298
+ // The noise rate belongs under the board's drops. A high one means the
299
+ // query is wrong, not that there is more scrolling to do.
300
+ console.error(
301
+ `${kept.length} of ${all.length} cards kept, ${Math.round((cut / all.length) * 100)}% off-topic (${top})`,
302
+ );
303
+ }
304
+ if (!kept.length) {
305
+ console.error("every card was off-topic — query the form, not the word (add \"ui\", \"screen\", or a category path)");
199
306
  drop();
200
307
  process.exit(1);
201
308
  }
202
- console.log(tsv);
309
+ console.log(kept.join("\n"));
203
310
  drop();
204
311
  process.exit(0);