wiki-starlight-api 1.0.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.
- package/README.md +177 -0
- package/package.json +20 -0
- package/wiki-starlight-api.mjs +178 -0
package/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# Wikipedia API
|
|
2
|
+
|
|
3
|
+
**Package:** `wikipedia-api`
|
|
4
|
+
**Author:** Dominex Macedon
|
|
5
|
+
**License:** MIT
|
|
6
|
+
|
|
7
|
+
A **lightweight, zero-dependency Wikipedia API client** for Node.js and Starlight.
|
|
8
|
+
Designed for **searching, reading, and exploring Wikipedia content** using clean async APIs.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- Search Wikipedia articles
|
|
16
|
+
- Fetch article summaries
|
|
17
|
+
- Retrieve full plain-text pages
|
|
18
|
+
- Get a random article
|
|
19
|
+
- List available language translations
|
|
20
|
+
- Extract image titles
|
|
21
|
+
- Get article categories
|
|
22
|
+
- Search + summary combined helper
|
|
23
|
+
- No dependencies
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install wikipedia-api
|
|
31
|
+
````
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### Importing
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
import {
|
|
41
|
+
search,
|
|
42
|
+
summary,
|
|
43
|
+
page,
|
|
44
|
+
random,
|
|
45
|
+
languages,
|
|
46
|
+
images,
|
|
47
|
+
categories,
|
|
48
|
+
searchAndSummary
|
|
49
|
+
} from "wikipedia-api";
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## 🔍 Search Articles
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
const results = await search("Starlight programming language", 5);
|
|
58
|
+
|
|
59
|
+
console.log(results);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**Output**
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
[
|
|
66
|
+
{
|
|
67
|
+
title: "Starlight",
|
|
68
|
+
snippet: "Starlight is ...",
|
|
69
|
+
pageid: 123456
|
|
70
|
+
}
|
|
71
|
+
]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Article Summary
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
const info = await summary("JavaScript");
|
|
80
|
+
console.log(info.extract);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## Full Page Content
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
const article = await page("Node.js");
|
|
89
|
+
console.log(article.content);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## Random Article
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
const randomArticle = await random();
|
|
98
|
+
console.log(randomArticle.title);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## Available Languages
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
const langs = await languages("JavaScript");
|
|
107
|
+
console.log(langs);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**Output**
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
[
|
|
114
|
+
{ lang: "es", title: "JavaScript" },
|
|
115
|
+
{ lang: "fr", title: "JavaScript" }
|
|
116
|
+
]
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Images Used in Article
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
const imgs = await images("Node.js");
|
|
125
|
+
console.log(imgs);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**Note:** Returns image titles only (file names).
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## Categories
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
const cats = await categories("Node.js");
|
|
136
|
+
console.log(cats);
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## Search + Summary (Combined Helper)
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
const summaries = await searchAndSummary("Programming language", 3);
|
|
145
|
+
console.log(summaries);
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## Design Philosophy
|
|
151
|
+
|
|
152
|
+
* Minimal and readable
|
|
153
|
+
* Uses official Wikipedia APIs
|
|
154
|
+
* Async / await friendly
|
|
155
|
+
* Works with Starlight and Node.js
|
|
156
|
+
* No external libraries
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Notes
|
|
161
|
+
|
|
162
|
+
* This package retrieves **public Wikipedia data only**
|
|
163
|
+
* Intended for **CLI tools, scripts, bots, and learning projects**
|
|
164
|
+
* Subject to Wikipedia API usage limits
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## Resources
|
|
169
|
+
|
|
170
|
+
* Wikipedia API Docs: [https://www.mediawiki.org/wiki/API:Main_page](https://www.mediawiki.org/wiki/API:Main_page)
|
|
171
|
+
* REST API Docs: [https://en.wikipedia.org/api/rest_v1/](https://en.wikipedia.org/api/rest_v1/)
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
## License
|
|
176
|
+
|
|
177
|
+
MIT © Dominex Macedon
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wiki-starlight-api",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Lightweight zero-dependency Wikipedia API client for Node.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "wiki-starlight-api.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./wiki-starlight-api.mjs"
|
|
9
|
+
},
|
|
10
|
+
"author": "Dominex Macedon",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"wikipedia",
|
|
14
|
+
"api",
|
|
15
|
+
"search",
|
|
16
|
+
"wiki",
|
|
17
|
+
"node",
|
|
18
|
+
"starlight"
|
|
19
|
+
]
|
|
20
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal helper for API requests
|
|
3
|
+
*/
|
|
4
|
+
async function request(params) {
|
|
5
|
+
const url = new URL(BASE_URL);
|
|
6
|
+
params.format = "json";
|
|
7
|
+
params.origin = "*";
|
|
8
|
+
|
|
9
|
+
for (const key in params) {
|
|
10
|
+
url.searchParams.set(key, params[key]);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const res = await fetch(url.toString());
|
|
14
|
+
if (!res.ok) {
|
|
15
|
+
throw new Error(`Wikipedia API error: ${res.status}`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return res.json();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Search Wikipedia articles
|
|
23
|
+
* @param {string} query
|
|
24
|
+
* @param {number} limit
|
|
25
|
+
*/
|
|
26
|
+
export async function search(query, limit = 5) {
|
|
27
|
+
const data = await request({
|
|
28
|
+
action: "query",
|
|
29
|
+
list: "search",
|
|
30
|
+
srsearch: query,
|
|
31
|
+
srlimit: limit
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return data.query.search.map(item => ({
|
|
35
|
+
title: item.title,
|
|
36
|
+
snippet: item.snippet.replace(/<\/?[^>]+>/g, ""),
|
|
37
|
+
pageid: item.pageid
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Get article summary (intro)
|
|
43
|
+
* @param {string} title
|
|
44
|
+
*/
|
|
45
|
+
export async function summary(title) {
|
|
46
|
+
const url = `https://en.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(title)}`;
|
|
47
|
+
const res = await fetch(url);
|
|
48
|
+
|
|
49
|
+
if (!res.ok) {
|
|
50
|
+
throw new Error(`Failed to fetch summary for "${title}"`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const data = await res.json();
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
title: data.title,
|
|
57
|
+
description: data.description,
|
|
58
|
+
extract: data.extract,
|
|
59
|
+
url: data.content_urls?.desktop?.page
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Get full page content (plain text)
|
|
65
|
+
* @param {string} title
|
|
66
|
+
*/
|
|
67
|
+
export async function page(title) {
|
|
68
|
+
const data = await request({
|
|
69
|
+
action: "query",
|
|
70
|
+
prop: "extracts",
|
|
71
|
+
explaintext: true,
|
|
72
|
+
redirects: true,
|
|
73
|
+
titles: title
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const pages = data.query.pages;
|
|
77
|
+
const p = pages[Object.keys(pages)[0]];
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
title: p.title,
|
|
81
|
+
content: p.extract
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Get a random Wikipedia article
|
|
87
|
+
*/
|
|
88
|
+
export async function random() {
|
|
89
|
+
const data = await request({
|
|
90
|
+
action: "query",
|
|
91
|
+
list: "random",
|
|
92
|
+
rnnamespace: 0,
|
|
93
|
+
rnlimit: 1
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const title = data.query.random[0].title;
|
|
97
|
+
return summary(title);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Get available language translations for an article
|
|
102
|
+
* @param {string} title
|
|
103
|
+
*/
|
|
104
|
+
export async function languages(title) {
|
|
105
|
+
const data = await request({
|
|
106
|
+
action: "query",
|
|
107
|
+
prop: "langlinks",
|
|
108
|
+
titles: title,
|
|
109
|
+
lllimit: "max"
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const pages = data.query.pages;
|
|
113
|
+
const p = pages[Object.keys(pages)[0]];
|
|
114
|
+
|
|
115
|
+
return (p.langlinks || []).map(lang => ({
|
|
116
|
+
lang: lang.lang,
|
|
117
|
+
title: lang["*"]
|
|
118
|
+
}));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Get image URLs used in an article
|
|
123
|
+
* @param {string} title
|
|
124
|
+
*/
|
|
125
|
+
export async function images(title) {
|
|
126
|
+
const data = await request({
|
|
127
|
+
action: "query",
|
|
128
|
+
prop: "images",
|
|
129
|
+
titles: title,
|
|
130
|
+
imlimit: "max"
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const pages = data.query.pages;
|
|
134
|
+
const p = pages[Object.keys(pages)[0]];
|
|
135
|
+
|
|
136
|
+
return (p.images || [])
|
|
137
|
+
.map(img => img.title)
|
|
138
|
+
.filter(name => /\.(png|jpg|jpeg|svg|gif)$/i.test(name));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Get categories of an article
|
|
143
|
+
* @param {string} title
|
|
144
|
+
*/
|
|
145
|
+
export async function categories(title) {
|
|
146
|
+
const data = await request({
|
|
147
|
+
action: "query",
|
|
148
|
+
prop: "categories",
|
|
149
|
+
titles: title,
|
|
150
|
+
cllimit: "max"
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const pages = data.query.pages;
|
|
154
|
+
const p = pages[Object.keys(pages)[0]];
|
|
155
|
+
|
|
156
|
+
return (p.categories || []).map(cat =>
|
|
157
|
+
cat.title.replace("Category:", "")
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Search and return summaries for results
|
|
163
|
+
* @param {string} query
|
|
164
|
+
* @param {number} limit
|
|
165
|
+
*/
|
|
166
|
+
export async function searchAndSummary(query, limit = 3) {
|
|
167
|
+
const results = await search(query, limit);
|
|
168
|
+
|
|
169
|
+
const summaries = [];
|
|
170
|
+
for (const r of results) {
|
|
171
|
+
try {
|
|
172
|
+
summaries.push(await summary(r.title));
|
|
173
|
+
} catch {
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return summaries;
|
|
178
|
+
}
|