WIKIPEDIA
Wikipedia for node. Works in the browser as well.
Implements legacy wiki endpoints and also the newer
REST API.
Try out the new summary()
REST endpoint for a introduction to your page and the main images optimized for browsers and mobile!
You can also now get the events which happened on a particular day using the onThisDay()
api, which supports filtering by event types as well.
Built with latest ES6 and native support for async/await and promises.
Built with TypeScript - exports all the types used.
INSTALLATION
$ npm install wikipedia
Highlights
For detailed documentation of methods available on wiki
and page
,
What can it do?
- Get a summary for a page which contains the intro and main image optimized for web and mobile with the new wikipedia REST APIs
- Fetch article content
- Find all links/images/categories in a page
- Gets all the relevant events that happened on a particular day. You can further filter this by event type
- Find related articles from the given article
- Find articles by geographical location
- Get a wikipedia page as a pdf document
- Supports switching languages
- Parses infoboxes using infobox-parser
Usage
const wiki = require('wikipedia');
(async () => {
try {
const page = await wiki.page('Batman');
console.log(page);
const summary = await page.summary();
console.log(summary);
} catch (error) {
console.log(error);
}
})();
The page method returns a Page class object which has fields like pageid
, title
, parentid
, revisionid
and methods like summary()
, intro()
, images()
, html()
and more.
All the page methods can take a title parameter or a pageId. Read up on the Page documentation here to see a detailed overview of the methods available in page.
You can also call methods like summary()
on the wiki
object directly. Read up here to see when you should use the page
object and when you should call summary()
directly. There's a performance difference! Long story short, use the method directly if you are using only the summary
of the page and are not expecting to use any of the other page
attributes.
const wiki = require('wikipedia');
(async () => {
try {
const summary = await wiki.summary('Batman');
console.log(summary);
} catch (error) {
console.log(error);
}
})();
You can now get the events which happened on a particular day using the new onThisDay()
api on the wiki object.
const wiki = require('wikipedia');
(async () => {
try {
const events = await wiki.onThisDay();
const deaths = await wiki.onThisDay({type:'deaths', month:'2', day:'28'});
console.log(events);
console.log(deaths);
} catch (error) {
console.log(error);
}
})();
There are other methods like search()
, geoSearch()
, suggest()
, setLang()
, setUserAgent()
which should be called on the wiki object directly. Read up on the wiki documentation to see a complete list of methods available on the wiki default object.
const wiki = require('wikipedia');
(async () => {
try {
const searchResults = await wiki.search('Batma');
console.log(searchResults);
const newUrl = await wiki.setLang('fr');
console.log(newUrl);
} catch (error) {
console.log(error);
}
})();
You can export types or even specific methods if you are using modern ES6 js or TypeScript.
import wiki from 'wikipedia';
import { wikiSummary, summaryError } from 'wikipedia';
import { summary } from 'wikipedia';
(async () => {
try {
let summary: wikiSummary;
summary = await wiki.summary('Batman');
console.log(summary);
let summary2 = await summary('Batman');
} catch (error) {
console.log(error);
}
})();
Options
All methods have options you can pass them. You can find them in optionTypes documentation.
Result Types
All the returned result types are documented as well. You can find them here.
Contributing
Before opening a pull request please make sure your changes follow the contribution guidelines.
Contributors
The project would not be the way it is without these rockstars.