allSites.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var allSites = getAllSites();
  2. /**
  3. * Get all Gamepedia sites.
  4. * @returns {Promise<Object[]>}
  5. */
  6. function getAllSites() {
  7. return got.get( 'https://wikisandbox.gamepedia.com/api.php?action=allsites&formatversion=2&do=getSiteStats&filter=wikis|md5_key,wiki_domain,wiki_display_name,wiki_image,wiki_description,wiki_managers,official_wiki,wiki_crossover,created&format=json' ).then( response => {
  8. var body = response.body;
  9. if ( response.statusCode !== 200 || !body || body.status !== 'okay' || !body.data || !body.data.wikis ) {
  10. console.log( '- ' + shardId + ': ' + response.statusCode + ': Error while gettings all sites: ' + ( body && body.error && body.error.info ) );
  11. return [];
  12. }
  13. else {
  14. console.log( '- ' + shardId + ': Sites successfully loaded.' );
  15. var sites = JSON.parse(JSON.stringify(body.data.wikis.filter( site => /^[a-z\d-]{1,50}\.gamepedia\.com$/.test(site.wiki_domain) )));
  16. sites.filter( site => site.wiki_crossover ).forEach( site => site.wiki_crossover = site.wiki_crossover.replace( /^(?:https?:)?\/\/(([a-z\d-]{1,50})\.(?:fandom\.com|wikia\.org)(?:(?!\/wiki\/)\/([a-z-]{1,8}))?).*/, '$1' ) );
  17. return sites;
  18. }
  19. }, error => {
  20. console.log( '- ' + shardId + ': Error while gettings all sites: ' + error );
  21. return [];
  22. } );
  23. }
  24. /**
  25. * Update the list of all sites.
  26. * @returns {Promise<Object[]>}
  27. */
  28. function updateAllSites() {
  29. return new Promise( function(resolve, reject) {
  30. getAllSites().then( newSites => {
  31. if ( newSites.length ) allSites.then( sites => {
  32. sites.splice(0, sites.length);
  33. sites.push(...newSites);
  34. resolve(sites);
  35. } );
  36. else resolve(newSites);
  37. } );
  38. } );
  39. }
  40. module.exports = {
  41. update: updateAllSites,
  42. then: (callback) => allSites.then(callback)
  43. };