Skip to content

Latest commit

 

History

History
244 lines (206 loc) · 8.93 KB

README.md

File metadata and controls

244 lines (206 loc) · 8.93 KB

WIKIPEDIA build Test Coverage Contributions npm version

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);
		//Response of type @Page object
		const summary = await page.summary();
		console.log(summary);
		//Response of type @wikiSummary - contains the intro and the main image
	} catch (error) {
		console.log(error);
		//=> Typeof wikiError
	}
})();

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);
		//Response of type @wikiSummary - contains the intro and the main image
	} catch (error) {
		console.log(error);
		//=> Typeof wikiError
	}
})();

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); // returns all the events which happened today
		console.log(deaths); // returns all deaths which happened on Feb 28
	} catch (error) {
		console.log(error);
		//=> Typeof wikiError
	}
})();

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);
		//Response of type @wikiSearchResult - contains results and optionally a suggestion
		const newUrl = await wiki.setLang('fr');
		console.log(newUrl);
		//Returns the api url with language changed - use `languages()` method to see a list of available langs
	} catch (error) {
		console.log(error);
		//=> Typeof wikiError
	}
})();

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; //sets the object as type wikiSummary
		summary = await wiki.summary('Batman');
		console.log(summary);
        let summary2 = await summary('Batman');//using summary directly
	} catch (error) {
		console.log(error);
		//=> Typeof summaryError, helpful in case you want to handle this error separately
	}
})();

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.

dopecodez
Govind S
friendofdog
Kevin Kee
bumbummen99
Patrick
gtibrett
Brett
0xflotus
0xflotus
Greeshmareji
Greeshma R
Jman1868
Jamal Ahmed
zoetrope69
Z
bigmistqke
Bigmistqke
yg-i
Null