allSites.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var allSites = getAllSites();
  2. /**
  3. * Get all Gamepedia sites.
  4. * @returns {Promise<Object[]>}
  5. */
  6. function getAllSites() {
  7. if ( typeof got === 'undefined' ) return Promise.resolve([]);
  8. return got.get( 'https://commons.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 => {
  9. var body = response.body;
  10. if ( response.statusCode !== 200 || !body || body.status !== 'okay' || !body.data || !body.data.wikis ) {
  11. console.log( '- ' + shardId + ': ' + response.statusCode + ': Error while gettings all sites: ' + ( body && body.error && body.error.info ) );
  12. return [];
  13. }
  14. else {
  15. console.log( '- ' + shardId + ': Sites successfully loaded.' );
  16. var sites = JSON.parse(JSON.stringify(body.data.wikis.filter( site => /^[a-z\d-]{1,50}\.gamepedia\.com$/.test(site.wiki_domain) )));
  17. 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' ) );
  18. return sites;
  19. }
  20. }, error => {
  21. console.log( '- ' + shardId + ': Error while gettings all sites: ' + error );
  22. return [];
  23. } );
  24. }
  25. /**
  26. * Update the list of all sites.
  27. * @returns {Promise<Object[]>}
  28. */
  29. function updateAllSites() {
  30. return new Promise( function(resolve, reject) {
  31. getAllSites().then( newSites => {
  32. if ( newSites.length ) allSites.then( sites => {
  33. sites.splice(0, sites.length);
  34. sites.push(...newSites);
  35. resolve(sites);
  36. } );
  37. else resolve(newSites);
  38. } );
  39. } );
  40. }
  41. module.exports = {
  42. update: updateAllSites,
  43. then: (callback) => allSites.then(callback)
  44. };