You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
839 B

  1. var httpGet = require('./httpGet')
  2. var loadClash = {}
  3. var cache = {}
  4. function wikidataLoad (id, callback) {
  5. if (id in cache) {
  6. return callback(null, cache[id])
  7. }
  8. if (id in loadClash) {
  9. loadClash[id].push(callback)
  10. return
  11. }
  12. loadClash[id] = []
  13. httpGet('https://www.wikidata.org/wiki/Special:EntityData/' + id + '.json', {}, function (err, result) {
  14. if (err) {
  15. return callback(err, null)
  16. }
  17. result = JSON.parse(result.body)
  18. if (!result.entities || !result.entities[id]) {
  19. console.log('invalid result', result)
  20. return callback(err, null)
  21. }
  22. cache[id] = result.entities[id]
  23. callback(null, result.entities[id])
  24. loadClash[id].forEach(function (d) {
  25. d(null, result.entities[id])
  26. })
  27. delete loadClash[id]
  28. })
  29. }
  30. module.exports = {
  31. load: wikidataLoad
  32. }