Browse Source

ImageLoader/Wikipedia: load images from wikipedia article

master
parent
commit
d352c9414f
  1. 32
      src/ImageLoader.js
  2. 35
      src/wikipedia.js

32
src/ImageLoader.js

@ -1,4 +1,5 @@
var wikidata = require('./wikidata')
var wikipedia = require('./wikipedia')
var cache = {}
function ImageLoader (data) {
@ -55,6 +56,13 @@ ImageLoader.prototype.parseObject = function (data) {
}
}
if (data.object.tags.wikipedia) {
this.sources.push({
type: 'wikipedia',
value: data.object.tags.wikipedia
})
}
if (data.object.tags.wikidata) {
this.sources.push({
type: 'wikidata',
@ -142,6 +150,28 @@ ImageLoader.prototype.loadWikimediaCommons = function (src, callback) {
}
}
ImageLoader.prototype.loadWikipedia = function (src, callback) {
var value = src.value
wikipedia.getImages(value, function (err, result) {
if (err) {
return callback(err, null)
}
result.forEach(function (d) {
if (this.found.indexOf(d) === -1) {
this.found.push(d)
this.data[d] = {
id: d,
type: 'wikimedia'
}
}
}.bind(this))
callback(null)
}.bind(this))
}
ImageLoader.prototype.handlePending = function () {
var pending = this.pendingCallbacks
delete this.pendingCallbacks
@ -169,6 +199,8 @@ ImageLoader.prototype.callbackCurrent = function (index, options, callback) {
this.loadWikimediaCommons(src, this.handlePending.bind(this))
} else if (src.type === 'wikidata') {
this.loadWikidata(src, this.handlePending.bind(this))
} else if (src.type === 'wikipedia') {
this.loadWikipedia(src, this.handlePending.bind(this))
}
return

35
src/wikipedia.js

@ -283,3 +283,38 @@ function showWikipedia (tagValue, dom, callback) {
callback(err)
})
}
function getImages (tagValue, callback) {
get(tagValue, function (err, result) {
if (err) {
return callback(err, null)
}
var imgs = result.div.getElementsByTagName('img')
var result = []
for (i = 0; i < imgs.length; i++) {
var img = imgs[i]
// ignore icons
if (img.width <= 64 && img.height <= 64) {
continue
}
img.removeAttribute('width')
img.removeAttribute('height')
var m = img.src.match(/^https?:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/\w+\/\w+\/([^\/]+)/)
if (m) {
var file = decodeURIComponent(m[1]).replace(/_/g, ' ')
result.push(file)
}
}
callback(null, result)
})
}
module.exports = {
getImages: getImages
}
Loading…
Cancel
Save