Skip to content

Latest commit

 

History

History
271 lines (215 loc) · 11.4 KB

wiki.md

File metadata and controls

271 lines (215 loc) · 11.4 KB

Wiki

Highlights

Functions

search()

Returns the search result for the input string as wikiSearchResult object. Accepts two inputs, the first the query string and the second a searchOptions object.

search = async (query: string, searchOptions?: searchOptions): Promise<wikiSearchResult>
  • @param query - The string you want to search for. This is required.
  • @param searchOptions - the options for the page
  • @resultwikiSearchResult - the summary object for the wiki page
//example
const searchResults = await wiki.search('Batma', {suggestion: true, limit: 10});
console.log(searchResults.results) // The search results
console.log(searchResults.suggestion) // Suggestion string if suggestion set to true, null otherwise.

page()

The main page method for the wiki. The title param can be either a string or a pageId. Returns a Page object which can be used to call any of the page methods. Use the preload and fields object present in pageOptions if you want to load multiple attributes of a page together.

page = async (title: string, pageOptions?: pageOptions): Promise<Page>
  • @param title - The title or pageId of the page. This is required.
  • @param pageOptions - the options for the page
  • @resultPage - the summary object for the wiki page
//example
const page = await wiki.page('Batman', {autoSuggest: true, preload:true, fields:["summary", "html"]});
const summary = await page.summary(); // Does not call API, returns summary immediately as it is preloaded

onThisDay()

Returns the events that happened on a day depending on input type, month, day arguments. Type can be any one of the ones defined in eventOptions. By default, it will return all events that happened on the current day. All options are documented in the eventOptions object. Returns a array of eventResult object which internally has arrays of wikiSummary.

onThisDay = async ({type: string, month: string, day: string}): Promise<eventResult>
  • @param type - Any one of the valid event types. By default, all.
  • @param month - The month to search for. Takes current month by default.
  • @param day - The day to search for. Takes current day by default.
  • @resulteventResult - a eventResult object.
//example
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

featuredContent()

Returns featured content of a given day, depending on input year, month, day arguments. By default, it will return featured content of the current day. Returns a array of fcResult object.

featuredContent = async ({year: string, month: string, day: string}): Promise<fcResult>
  • @param year - The year to search for. Takes current year by default.
  • @param month - The month to search for. Takes current month by default.
  • @param day - The day to search for. Takes current day by default.
  • @result fcResult - a fcResult object.
//example
const content = await wiki.featuredContent();
const contentNewYear2020 = await wiki.featuredContent({year:'2020', month:'01', day:'01'});
const contentNewYear = await wiki.featuredContent({month:'01', day:'01'});
console.log(content); // returns featured content from today
console.log(contentNewYear2020); // returns featured content from 2020-01-01
console.log(contentNewYear); // returns featured content from 01-01 of this year

geoSearch()

Searches for a page based on input latitude, longitude coordinates. Optionally takes a limit and radius(the search radius in meters) parameter in the geoOptions object. Returns an array of geoSearchResult object.

geoSearch = async (latitude: number, longitude: number, geoOptions?: geoOptions): Promise<Array<geoSearchResult>>
  • @param latitude - The latitude to search for. This is required.
  • @param longitude - The longitude to search for. This is required.
  • @param geoOptions - the options for the geo search.
  • @resultgeoSearchResult - an array of geoSearchResult object.
//example
const geoResult = await wiki.geoSearch(2.088, 4.023, { radius: 5000, limit: 20 });
console.log(geoResult[0]); // the closest page to given coordinates

languages()

The array of languages available in wikipedia. Mainly meant for use before setLang to check if the language is available before actually setting it. Returns an array of languageResult object.

languages = async (): Promise<Array<languageResult>>
//example
const languages = await wiki.languages();

setLang()

Uses the input language code to set language for the wikimedia and the REST APIs. This is useful in case you want to switch languages while using wikipedia.

setLang = (language: string): string
  • @param language - The language code for the wiki.
  • @result Returns the new wikimedia url as string.
//example
const changedLang = await wiki.setLang('fr'); // sets language to french

setUserAgent()

Changes the user agent used in the request to wikipedia from 'wikipedia (https://github.com/dopecodez/Wikipedia/)' to the string passed. As per the wikipedia docs - Set a unique userAgent header that allows us to contact you quickly. Email addresses or URLs of contact pages work well. Check https://meta.wikimedia.org/wiki/User-Agent_policy for more info.

setUserAgent = (userAgent: string)
  • @param language - The language code for the wiki.
  • @result null
//example
await wiki.setUserAgent('Govind (gvind4@gmail.com)'); // sets userAgent to this value

suggest()

Returns the wiki suggestion for a given query string. This method returns null if no suggestion was returned.

suggest = async (query: string): Promise<string | null>
  • @param query - The query string.
  • @result the suggestion or null if no suggestion present.
//example
const suggestion = await wiki.suggest('Batma');
console.log(suggestion); //Returns 'Batman'

random()

Returns a random wiki page in any of the available formats. Formats can be summary, title, related, mobile-sections or mobile-sections-lead. Defaults to summary.

random = async (format?: randomFormats): Promise<wikiSummary | title | string | relatedResult | mobileSections>
  • @param format - the format for the random page
  • @result the random page in requested format
//example
const randomSummary = await wiki.random();
console.log(random); //Returns wikiSummary of a random pageOption
const randomMobileSections = await wiki.random("mobile-sections");
console.log(randomMobileSections); // Returns random mobile sections for a page

citation

Generates citation data given a URL, DOI, PMID, ISBN, or PMCID. Defaults to mediawiki format. Format can be 'mediawiki', 'mediawiki-basefields', 'zotero', 'bibtex', 'wikibase'.

citation = async (query: string, format?: citationFormat, language?: string): Promise<any>
  • @param query - url or query string
  • @param format - the format of the citation result
  • @param language - if you want lanuage enabled results
  • @returns Returns citation data
//example
const citation = await wiki.citation("batman");
console.log(random); //Returns citations in an array
const citationsWiki = await wiki.citation("test", "mediawiki", "fr") ;
console.log(citationsWiki); // Returns citations in mediawiki format

autocompletions()

Returns the autocompletions of a given title

autocompletions = async (autocompletionOptions?: autocompletionOptions): Promise<Array<string>>
//example
const autocompletions = await wiki.autocompletions({limit: 20});

Page Methods

All the methods defined in the Page documentation can be called directly from the wiki object as well. This includes summary(), images(), intro(), html(), related(), content(), categories(), links(), references(), coordinates(), langLinks(), infobox(), mobileHtml(), pdf() and tables().

Read up here to understand when you should use these methods directly and when you should use the page methods. Also, note that if called directly from the wiki object, you can use the autoSuggest option present in the pageOptions object which will be ignored if called through page.

//example
const summary = await wiki.summary('Batma', {autoSuggest:true});
console.log(summary); //Returns summary for 'Batman'
const html = await wiki.summary('David Attenborough');
console.log(html); //Returns html for the environmentalist