Skip to content

Commit

Permalink
Merge pull request #24 from dijs/info-update
Browse files Browse the repository at this point in the history
Info update
  • Loading branch information
dijs committed Jun 8, 2016
2 parents ca5f013 + 68d840a commit 23e2598
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 20 deletions.
13 changes: 5 additions & 8 deletions README.md
Expand Up @@ -40,16 +40,13 @@ npm test
## Usage

```javascript
var Wiki = require('wikijs');
import Wiki from 'wikijs';

var wiki = new Wiki();

wiki.page('Batman').then(function(page) {
page.info().then(function(info) {
console.log(info['alter_ego']); // Bruce Wayne
});
});
const wiki = new Wiki();

wiki.page('Batman')
.then(page => page.info('alter_ego'))
.then(console.log); // Bruce Wayne
```

## Usage with webpack
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "wikijs",
"description": "Wikipedia interface for node",
"author": "Richard van der Dys",
"version": "0.2.2",
"version": "0.3.0",
"keywords": [
"wiki",
"wikipedia",
Expand All @@ -21,6 +21,7 @@
"dependencies": {
"babel-polyfill": "^6.9.0",
"isomorphic-fetch": "^2.2.1",
"moment": "^2.13.0",
"underscore": "^1.8.3",
"wiki-infobox-parser-core": "0.0.1"
},
Expand Down
19 changes: 19 additions & 0 deletions src/determiners.js
@@ -0,0 +1,19 @@
import moment from 'moment';

const datePattern = /(\d+\|\d+\|\d+)/;

const determiners = {
age: metadata => {
if (!metadata.birth_date) {
return;
}
const matches = metadata.birth_date.match(datePattern);
if (!matches) {
return;
}
const birthDate = moment(matches[1], 'YYYY|MM|DD');
return moment().diff(birthDate, 'years');
}
};

export default determiners;
27 changes: 23 additions & 4 deletions src/wiki.js
Expand Up @@ -5,6 +5,7 @@ import fetch from 'isomorphic-fetch';
import _ from 'underscore';
import wikiInfoboxParser from 'wiki-infobox-parser-core';
import querystring from 'querystring';
import determiners from './determiners';

/**
* @namespace
Expand Down Expand Up @@ -299,18 +300,36 @@ class WikiPage {
.then(res => res.query.pages[this.pageid].coordinates[0]);
}
/**
* Get info from page
* Get information from page
* @example
* new Wiki().page('Batman').then(page => page.info('alter_ego'));
* @method WikiPage#info
* @return {Promise} - info Object contains key/value pairs of infobox data
* @param {String} [key] - Information key
* @return {Promise} - info Object contains key/value pairs of infobox data, or specific value if key given
*/
info() {
info(key) {
return this.wiki.api({
prop: 'revisions',
rvprop: 'content',
rvsection: 0,
titles: this.title
})
.then(res => markupParser(JSON.stringify(res)));
.then(res => markupParser(JSON.stringify(res)))
.then(metadata => {
if (!key) {
return metadata;
}
if (metadata.hasOwnProperty(key)) {
return metadata[key];
}
if (determiners.hasOwnProperty(key)) {
const value = determiners[key](metadata);
if (value) {
return value;
}
}
return undefined;
});
}
/**
* Paginated backlinks from page
Expand Down
22 changes: 22 additions & 0 deletions test/data/batman_1465345136432.json
@@ -0,0 +1,22 @@
{
"batchcomplete": "",
"query": {
"pages": {
"4335": {
"pageid": 4335,
"ns": 0,
"title": "Batman",
"contentmodel": "wikitext",
"pagelanguage": "en",
"pagelanguagehtmlcode": "en",
"pagelanguagedir": "ltr",
"touched": "2016-06-07T21:53:24Z",
"lastrevid": 724220861,
"length": 130346,
"fullurl": "https://en.wikipedia.org/wiki/Batman",
"editurl": "https://en.wikipedia.org/w/index.php?title=Batman&action=edit",
"canonicalurl": "https://en.wikipedia.org/wiki/Batman"
}
}
}
}
19 changes: 19 additions & 0 deletions test/data/batman_1465345136921.json
@@ -0,0 +1,19 @@
{
"batchcomplete": "",
"query": {
"pages": {
"4335": {
"pageid": 4335,
"ns": 0,
"title": "Batman",
"revisions": [
{
"contentformat": "text/x-wiki",
"contentmodel": "wikitext",
"*": "{{About|the fictional character}}\n{{pp-semi-indef}}\n{{pp-move-indef}}\n{{Use mdy dates|date=October 2015}}\n{{Infobox comics character\n|image = Comic Art - Batman by Jim Lee (2002).png\n|imagesize =\n|converted = y\n|caption = Promotional art for ''[[Batman (comic book)|Batman]]'' #608 (Oct. 2002).<br />Pencils by [[Jim Lee]] and inks by [[Scott Williams (comic book artist)|Scott Williams]].\n|publisher = [[DC Comics]]\n|debut = ''[[Detective Comics]]'' #27<br />(May 1939)\n|creators = [[Bill Finger]]<ref>[[Ron Goulart|Goulart, Ron]], ''Comic Book Encyclopedia'' ([[HarperCollins|Harper Entertainment]], New York, 2004) ISBN 978-0-06-053816-3</ref> <small>{{nowrap|(developer)}}</small><br />[[Bob Kane]] <small>(concept)</small>\n|alter_ego = Bruce Wayne <!-- Do not enter a middle name. He has been depicted with too many different middle names to enter a specific one here. Also, there is no past or current, dead or alive in fiction from a real world perspective; the infobox should cover the Batman known to the public consciousness and not a current comic book storyline. -->\n|alliances = [[List of Batman supporting characters#Batman Family|Batman Family]]<br />[[Justice League]]<br />[[Outsiders (comics)|Outsiders]]<br />[[Batmen of All Nations]]<br />[[Batman Incorporated]]<!-- Because the body that was resurrected in Blackest Night #5 was revealed to be a clone from Darkseid, Bruce Wayne himself was never part of the Black Lantern Corps. -->\n|partners = {{plainlist|\n<!--Try to keep this list short. No need to fill with Batman supporting characters and Justice League colleagues-->\n* [[Robin (comics)|Robin]] (various<!--Use Robin to include NIGHTWING and other ex-Robin identities-->)\n* [[Batgirl]] (various<!--Incorporating Oracle, Huntress, Spoiler, etc.-->)\n<!--* [[Nightwing]] included under ROBIN-->\n* [[James Gordon (comics)|James \"Jim\" Gordon]]\n* [[Green Arrow]] <!--Don't disambiguate. The article is primarily about Oliver Queen. Connor Hawke has his own article. -->\n* [[Catwoman]]\n* [[Superman]]\n* [[Wonder Woman]]\n<!--Try to keep this list short. No need to fill with Batman supporting characters and Justice League colleagues-->\n}}\n|aliases = {{plainlist|\n* Matches Malone,<ref>''Batman'' #242 (June 1972)</ref>\n* Lefty Knox,<ref>''Detective Comics'' #846 (Sept. 2008)</ref>\n* Minuteman<ref>''[[Silver Age (DC Comics)|Silver Age 80-Page Giant]]'' #1 (July 2000)</ref>\n}}\n|powers =\n* [[Genius]]-level intellect\n* Peak human physical and mental conditioning\n* Master [[martial artist]] and hand-to-hand combatant\n* Master detective\n* Utilizes high-tech equipment and weapons\n|cat = super\n|subcat = DC Comics\n|hero = y\n|sortkey = Batman\n}}\n\n'''Batman''' is a fictional [[superhero]] appearing in [[American comic book]]s published by [[DC Comics]]. The character was created by artist [[Bob Kane]] and writer [[Bill Finger]],<ref name=\"FingerSep2015\">{{cite web|title=DC Entertainment To Give Classic Batman Writer Credit in 'Gotham' and 'Batman v Superman' (Exclusive)|url=http://www.hollywoodreporter.com/heat-vision/dc-entertainment-give-classic-batman-824572|website=Hollywood Reporter|accessdate=September 21, 2015}}</ref><ref name=\"FingerOct2015\">{{cite web|url=http://comicsalliance.com/bill-finger-batman-comics-credit/|title=Bill Finger Has A Creator Credit On This Week’s Batman Comics|last=Sims|first=Chris|date=October 21, 2015|accessdate=October 21, 2015}}</ref> and first appeared in ''[[Detective Comics]]'' #27 (May 1939). Originally named the '''Bat-Man''', the character is also referred to by such epithets as the '''Caped Crusader''', the [[Dark Knight (nickname)|'''Dark Knight''']], and the '''World's Greatest Detective'''.<ref name=\"Fleisher\">Fleisher, Michael L. ''The Encyclopedia of Comic Book Heroes Volume 1 Batman'' Collier Books 1976 ISBN 978-0-02-080090-3 p. 31</ref>\n\nBatman's [[secret identity]] is '''Bruce Wayne''', an American [[billionaire]], [[genius]], [[playboy (lifestyle)|playboy]] [[philanthropy|philanthropist]], and the owner of [[Wayne Enterprises]]. After witnessing the murder of his parents as a child, he swore revenge on criminals, an oath tempered by a sense of justice. Wayne trains himself physically and intellectually and crafts a [[Batsuit|bat-inspired]] persona to fight crime.<ref name=\"dc-ency\">{{cite book | last = Beatty | first = Scott | contribution = Batman | editor-last = Dougall | editor-first = Alastair | title = The DC Comics Encyclopedia | pages = 40–44 | publisher = [[Dorling Kindersley]] | place = London | year = 2008 | isbn = 978-0-7566-4119-1}}</ref> Batman operates in the fictional [[Gotham City]], with assistance from various supporting characters, including his butler [[Alfred Pennyworth|Alfred]], police commissioner [[James Gordon (comics)|Jim Gordon]], and vigilante allies such as [[Robin (comics)|Robin]]. Unlike most superheroes, Batman does not possess any [[Superpower (ability)|superpowers]]; rather, he relies on his genius intellect, physical prowess, martial arts abilities, detective skills, science and technology, vast wealth, intimidation, and indomitable will. A large assortment of villains make up Batman's [[List of Batman Family adversaries|rogues gallery]], including his [[archenemy]], the [[Joker (comics)|Joker]].\n\nBatman became popular soon after his introduction in 1939 and gained his own comic book title, ''[[Batman (comic book)|Batman]]'', the following year. As the decades went on, differing interpretations of the character emerged. The late 1960s ''[[Batman (TV series)|Batman]]'' television series used a [[Camp (style)|camp]] aesthetic, which continued to be associated with the character for years after the show ended. Various creators worked to return the character to his dark roots, culminating in 1986 with ''[[The Dark Knight Returns]]'' by [[Frank Miller (comics)|Frank Miller]]. The success of [[Warner Bros.]]' live-action [[Batman in film|''Batman'' feature films]] have helped maintain public interest in the character.<ref>{{cite news | url=http://www.independent.co.uk/arts-entertainment/film-and-tv/features/the-big-question-what-is-the-history-of-batman-and-why-does-he-still-appeal-873780.html | title=The Big Question: What is the history of Batman, and why does he still appeal? | work=[[The Independent]] | date=July 22, 2008 | location=London| archiveurl=https://web.archive.org/web/20080729223151/http://www.independent.co.uk:80/arts-entertainment/film-and-tv/features/the-big-question-what-is-the-history-of-batman-and-why-does-he-still-appeal-873780.html | archivedate=July 29, 2008 }}</ref>\n\nAn American cultural icon, Batman has been licensed and adapted into a [[Batman franchise media|variety of media]], from radio to television and film, and appears on a variety of merchandise sold all over the world, such as toys and video games. The character has also intrigued psychiatrists, with many trying to understand the character's psyche. In May 2011, Batman placed second on IGN's Top 100 Comic Book Heroes of All Time,<ref>{{cite web | url=http://www.ign.com/top/comic-book-heroes/2 | title=Batman – Top 100 Comic Book Heroes | publisher=IGN Entertainment | accessdate=May 27, 2011| archiveurl=https://web.archive.org/web/20140220133403/http://www.ign.com/top/comic-book-heroes/2 | archivedate=February 20, 2014 }}</ref> after [[Superman]]. ''[[Empire (magazine)|Empire]]'' magazine listed him second in their 50 Greatest Comic Book Characters of All Time. The character has been portrayed in both film and television by [[Lewis Wilson]], [[Robert Lowery (actor)|Robert Lowery]], [[Adam West]], [[Michael Keaton]], [[Kevin Conroy]], [[Val Kilmer]], [[George Clooney]], [[Christian Bale]],<!-- Do NOT add David Mazouz, per Talk Page discussion. --> [[Bruce Greenwood]], [[Peter Weller]], and [[Ben Affleck]].\n\n{{TOC limit|3}}"
}
]
}
}
}
}
22 changes: 22 additions & 0 deletions test/data/queen_1465350664030.json
@@ -0,0 +1,22 @@
{
"batchcomplete": "",
"query": {
"pages": {
"12153654": {
"pageid": 12153654,
"ns": 0,
"title": "Elizabeth II",
"contentmodel": "wikitext",
"pagelanguage": "en",
"pagelanguagehtmlcode": "en",
"pagelanguagedir": "ltr",
"touched": "2016-06-07T22:20:19Z",
"lastrevid": 724224625,
"length": 103293,
"fullurl": "https://en.wikipedia.org/wiki/Elizabeth_II",
"editurl": "https://en.wikipedia.org/w/index.php?title=Elizabeth_II&action=edit",
"canonicalurl": "https://en.wikipedia.org/wiki/Elizabeth_II"
}
}
}
}

0 comments on commit 23e2598

Please sign in to comment.