ausername-12345 commited on
Commit
1839952
Β·
1 Parent(s): 824f0a6

Use Wikipedia API for images, remove image URLs from prompt

Browse files
Files changed (2) hide show
  1. server.js +16 -26
  2. src/rememberit.jsx +1 -1
server.js CHANGED
@@ -48,33 +48,23 @@ app.post("/api/images", async (req, res) => {
48
  const { itemName } = req.body;
49
  if (!itemName) return res.status(400).json({ error: "Missing itemName" });
50
 
51
- const ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
52
- const headers = { "User-Agent": ua, "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5" };
53
-
54
- const searchUrl = `https://duckduckgo.com/?q=${encodeURIComponent(itemName)}&iax=images&ia=images`;
55
- const pageRes = await fetch(searchUrl, { headers });
56
- const html = await pageRes.text();
57
-
58
- let vqd = null;
59
- const patterns = [/vqd=["'](\d+(?:-\d+)*)["']/, /vqd["']?\s*[:=]\s*["'](\d+(?:-\d+)*)["']/];
60
- for (const p of patterns) {
61
- const m = html.match(p);
62
- if (m) { vqd = m[1]; break; }
63
- }
64
- if (!vqd) {
65
- const scriptTag = html.match(/<script[^>]*>([^<]*)<\/script>/gi);
66
- if (scriptTag) {
67
- for (const s of scriptTag) {
68
- const m = s.match(/["'](\d+(?:-\d+)*)["']/);
69
- if (m) { vqd = m[1]; break; }
70
- }
71
- }
72
- }
73
- if (!vqd) return res.json({ images: [] });
74
-
75
- const imgRes = await fetch(`https://duckduckgo.com/i.js?q=${encodeURIComponent(itemName)}&o=json&vqd=${vqd}`, { headers });
76
  const imgData = await imgRes.json();
77
- const images = (imgData.results || []).map(r => r.image).filter(Boolean).slice(0, 4);
 
 
78
  res.json({ images });
79
  } catch (err) {
80
  res.json({ images: [], error: err.message });
 
48
  const { itemName } = req.body;
49
  if (!itemName) return res.status(400).json({ error: "Missing itemName" });
50
 
51
+ const searchRes = await fetch(
52
+ `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(itemName)}&format=json&srlimit=3`,
53
+ { headers: { "User-Agent": "RememberIt/1.0" } }
54
+ );
55
+ const searchData = await searchRes.json();
56
+ const pages = searchData?.query?.search || [];
57
+ if (!pages.length) return res.json({ images: [] });
58
+
59
+ const titles = pages.slice(0, 2).map(p => p.title);
60
+ const imgRes = await fetch(
61
+ `https://en.wikipedia.org/w/api.php?action=query&titles=${encodeURIComponent(titles.join("|"))}&prop=pageimages&format=json&pithumbsize=300`,
62
+ { headers: { "User-Agent": "RememberIt/1.0" } }
63
+ );
 
 
 
 
 
 
 
 
 
 
 
 
64
  const imgData = await imgRes.json();
65
+ const imgPages = imgData?.query?.pages || {};
66
+ const images = Object.values(imgPages).map(p => p.thumbnail?.source).filter(Boolean).slice(0, 3);
67
+
68
  res.json({ images });
69
  } catch (err) {
70
  res.json({ images: [], error: err.message });
src/rememberit.jsx CHANGED
@@ -71,7 +71,7 @@ Rules:
71
  - confidence: a short honest phrase, e.g. "High confidence", "Fairly confident", "Moderate β€” a few possibilities", "Low confidence β€” hard to say without more context"
72
  - description: 2–3 sentences covering what it is, what it looks like, and what it is used for
73
  - alternatives: 0–1 entries if highly confident; 2–3 entries if uncertain; must be genuinely distinct items not synonyms
74
- - images: when confidence is high, include 2–3 relevant direct image URLs of the item (from Wikimedia, Commons, or known CDNs). Otherwise empty array.
75
  - searchLinks: always exactly these three, with the itemName URL-encoded:
76
  {"label":"Amazon","url":"https://www.amazon.com/s?k=ENCODED"},
77
  {"label":"Google Shopping","url":"https://www.google.com/search?q=ENCODED&tbm=shop"},
 
71
  - confidence: a short honest phrase, e.g. "High confidence", "Fairly confident", "Moderate β€” a few possibilities", "Low confidence β€” hard to say without more context"
72
  - description: 2–3 sentences covering what it is, what it looks like, and what it is used for
73
  - alternatives: 0–1 entries if highly confident; 2–3 entries if uncertain; must be genuinely distinct items not synonyms
74
+ - images: always an empty array
75
  - searchLinks: always exactly these three, with the itemName URL-encoded:
76
  {"label":"Amazon","url":"https://www.amazon.com/s?k=ENCODED"},
77
  {"label":"Google Shopping","url":"https://www.google.com/search?q=ENCODED&tbm=shop"},