Skip to content

Commit

Permalink
Add alternative language support
Browse files Browse the repository at this point in the history
  • Loading branch information
fdesjardins committed Aug 20, 2019
1 parent 15b9fac commit bdc5bcb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 30 deletions.
17 changes: 17 additions & 0 deletions examples/random-quote-espanol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const wikiquote = require('../lib/wikiquote')

wikiquote
.searchPeople('steve jobs')
.then(pages => wikiquote.getRandomQuote(pages[0].title, { language: 'es' }))
.then(quote => console.log(quote))

// You always have to keep pushing to innovate. Dylan could have
// sung protest songs forever and probably made a lot of money,
// but he didn’t. He had to move on, and when he did, by going
// electric in 1965, he alienated a lot of people. His 1966 Europe
// tour was his greatest…. The Beatles were the same way. They kept
// evolving, moving, refining their art. That’s what I’ve always
// tried to do — keep moving. Otherwise, as Dylan says, if you are
// not busy being born, you’re busy dying.
//
// As quoted in Steve Jobs (2011) by Walter Isaacson, p. 570
8 changes: 6 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const config = {
baseUri: 'https://en.wikiquote.org/w/api.php'
}

exports.get = async (options) => {
const response = await superagent.get(`${config.baseUri}${options.query}`)
exports.get = async options => {
let requestUri = config.baseUri
if (options.language !== undefined) {
requestUri = requestUri.replace('en', options.language)
}
const response = await superagent.get(`${requestUri}${options.query}`)
return response.body
}
55 changes: 36 additions & 19 deletions lib/wikiquote.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ const client = require('./client')
* @param {string} query The search terms used to query page titles
* @return {Promise}
*/
exports.searchByTitle = async (query) => {
const results = await client.get({ query: `?srsearch=${query}&action=query&list=search&srwhat=nearmatch&format=json` })
exports.searchByTitle = async (query, opts = {}) => {
const results = await client.get({
query: `?srsearch=${query}&action=query&list=search&srwhat=nearmatch&format=json`,
...opts
})
return results.query.search.map(r => {
return {
title: r.title,
Expand All @@ -21,8 +24,11 @@ exports.searchByTitle = async (query) => {
* @param {string} query The search terms used to query wikiquote
* @return {Promise}
*/
exports.search = async (query) => {
const results = await client.get({ query: `?format=json&action=query&list=search&continue=&srsearch=${query}` })
exports.search = async (query, opts = {}) => {
const results = await client.get({
query: `?format=json&action=query&list=search&continue=&srsearch=${query}`,
...opts
})
return results.query.search.map(r => {
return {
title: r.title,
Expand All @@ -36,8 +42,11 @@ exports.search = async (query) => {
* @param {string} query The search terms used to query people
* @return {Promise}
*/
exports.searchPeople = async (query) => {
const results = await client.get({ query: `?srsearch=${query}&action=query&list=search&srwhat=nearmatch&format=json` })
exports.searchPeople = async (query, opts = {}) => {
const results = await client.get({
query: `?srsearch=${query}&action=query&list=search&srwhat=nearmatch&format=json`,
...opts
})
return results.query.search.map(r => {
return {
title: r.title,
Expand All @@ -51,8 +60,11 @@ exports.searchPeople = async (query) => {
* @param {string} pageTitle
* @return {Promise}
*/
exports.getPageSections = async (pageTitle) => {
const results = await client.get({ query: `?page=${pageTitle}&action=parse&prop=sections&format=json` })
exports.getPageSections = async (pageTitle, opts = {}) => {
const results = await client.get({
query: `?page=${pageTitle}&action=parse&prop=sections&format=json`,
...opts
})
return results.parse
}

Expand All @@ -61,24 +73,27 @@ exports.getPageSections = async (pageTitle) => {
* @param {integer} sectionIndex
* @return {Promise}
*/
exports.getSectionContent = async (pageTitle, sectionIndex) => {
const results = await client.get({ query: `?page=${pageTitle}&section=${sectionIndex}&action=parse&prop=text&format=json` })
exports.getSectionContent = async (pageTitle, sectionIndex, opts = {}) => {
const results = await client.get({
query: `?page=${pageTitle}&section=${sectionIndex}&action=parse&prop=text&format=json`,
...opts
})

const $ = cheerio.load(results.parse.text['*'])

const isQuoteSection = (i, el) => {
const elClass = $(el).attr('class')

return elClass
? (!elClass.match(/toc/))
: true
return elClass ? !elClass.match(/toc/) : true
}

return $('ul > li')
.not('ul > li > ul > li')
.filter(isQuoteSection)
.map((i, el) => {
const quoteSource = $(el).find('li').text()
const quoteSource = $(el)
.find('li')
.text()
return {
quote: $(el).text(),
source: quoteSource ? (quoteSource === '' ? null : quoteSource) : null
Expand All @@ -91,17 +106,19 @@ exports.getSectionContent = async (pageTitle, sectionIndex) => {
* @param {string} pageTitle
* @return {Promise}
*/
exports.list = async (pageTitle) => {
const sections = (await exports.getPageSections(pageTitle)).sections
const quotes = await Promise.all(sections.map(s => exports.getSectionContent(pageTitle, s.index)))
exports.list = async (pageTitle, opts = {}) => {
const sections = (await exports.getPageSections(pageTitle, opts)).sections
const quotes = await Promise.all(
sections.map(s => exports.getSectionContent(pageTitle, s.index, opts))
)
return [].concat(...quotes)
}

/**
* @param {string} pageTitle
* @return {Promise}
*/
exports.getRandomQuote = async (pageTitle) => {
const quotes = await exports.list(pageTitle)
exports.getRandomQuote = async (pageTitle, opts = {}) => {
const quotes = await exports.list(pageTitle, opts)
return quotes[Math.floor(Math.random() * quotes.length)].quote
}
40 changes: 31 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ Some handy methods for Wikiquote
```sh
npm install --save wikiquote
```

### Usage

```js
wikiquote.searchPeople('steve jobs')
wikiquote
.searchPeople('steve jobs')
.then(page => wikiquote.getRandomQuote(page.title))
.then(quote => console.log(quote))
```
Expand All @@ -34,7 +36,7 @@ As quoted in Steve Jobs (2011) by Walter Isaacson, p. 570

## API

### `search(query)`
### `search(query, options)`

#### `query`

Expand All @@ -48,53 +50,73 @@ Examples:
- Book Title
- Movie Title

### `searchByTitle(query)`
### `searchByTitle(query, options)`

#### `query`

The search terms used to query page titles

Type: `string`

### `searchPeople(query)`
### `searchPeople(query, options)`

#### `query`

The search terms used to query people

Type: `string`

### `getPageSections(pageTitle)`
### `getPageSections(pageTitle, options)`

#### `pageTitle`

The title of the wikiquote page

Type: `string`

### `getSectionContent(pageTitle, sectionIndex)`
### `getSectionContent(pageTitle, sectionIndex, options)`

#### `pageTitle`

The title of the wikiquote page

Type: `string`

### `getRandomQuote(pageTitle)`
### `getRandomQuote(pageTitle, options)`

#### `pageTitle`

The title of the wikiquote page

Type: `string`

### `list(pageTitle)`
### `list(pageTitle, options)`

List all the quotes on a given page

Type: `string`

###
## `options`

You can also provide a set of options to each method to customize the query and results.

Type: `object`

Example:

```js
{
language: 'en'
}
```

### `options.language`

The language code to search with

Type: `string`

For a list of available language codes see https://meta.wikimedia.org/wiki/Wikiquote#List_of_Wikiquotes

## License

Expand Down

0 comments on commit bdc5bcb

Please sign in to comment.