Advertisement
Sehrentos

Node JS Battle.net API example guild members data

Sep 9th, 2016
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Node JS battle.net API tool - load guild members data
  3. *
  4. * This example will setup a simple express HTTP server for serving /public/ directory
  5. * and load guild members data from battle.net and save a copy to your server for later use.
  6. *
  7. * When a new request starts from /api/json/guild-members (You can make JS ajax call to this link and use JSON.parse the response)
  8. * this will first search a copy of the guild members data from your local server,
  9. * if local copy is not present it will download a new copy from the battle.net.
  10. * In this way you won't run out of free requests to your battle net api.
  11. *
  12. * Your copy of the data will be used about 1 day, then it will load a new copy.
  13. */
  14. var fs = require('fs');
  15. var path = require('path');
  16. var request = require("request");
  17. var express = require('express');
  18. var app = express();
  19. var server = require('http').createServer(app);
  20. var port = process.env.PORT || 80;
  21.  
  22. server.listen(port, function () {
  23.     console.log('Server listening at port %d', port);
  24. });
  25.  
  26. // Server static assets
  27. app.use(express.static(path.join(__dirname, '/public')));
  28. //app.use('/static', express.static(path.join(__dirname, '/public')));
  29.  
  30. // Route for home
  31. app.get('/', function (req, res) {
  32.     res.sendFile(path.join(__dirname, '/public/index.html'));
  33. });
  34.  
  35. /*
  36. * Battle.net API tools
  37. * - public/guild-members.json ( /api/json/guild-members )
  38. * - loaded once per day, then use local copy
  39. */
  40. // Function: Load new JSON from battle.net
  41. function getMembers(res, wFile) {
  42.     request({
  43.         url: 'https://eu.api.battle.net/wow/guild/YOUR_SERVER_NAME/YOUR_GUILD_NAME?fields=members&locale=en_GB&apikey=YOUR_API_KEY',
  44.         json: true
  45.     }, function (error, response, body) {
  46.         if (!error && response.statusCode === 200) {
  47.             fs.writeFile(wFile, JSON.stringify(body), 'utf8', function (errorWrite, data) {
  48.                 if (errorWrite) {
  49.                     console.log("Write error:", errorWrite);
  50.                 } else {
  51.                     res.setHeader('Content-Type', 'application/json');
  52.                     res.send(body);
  53.                 }
  54.             });
  55.         } else {
  56.             console.log("Request error:", error);
  57.         }
  58.     });
  59. }
  60.  
  61. // Express route for the battle.net api: guild-members
  62. app.get('/api/json/guild-members', function (req, res) {
  63.  
  64.     var guildFile = path.join(__dirname, '/public/data-guild-members.json');
  65.  
  66.     // Load JSON file from local
  67.     fs.readFile(guildFile, 'utf8', function (errorRead, fileData) {
  68.         if (errorRead) {
  69.             // File does not exists or can't be read, try creating new file
  70.             console.log('Downloading new copy!');
  71.             getMembers(res, guildFile);
  72.         } else {
  73.             fs.stat(guildFile, function (errorStat, statData) {
  74.                 if (errorStat) {
  75.                     console.log("Stat error:", errorStat);
  76.                 } else {
  77.                     var timeModifyed = new Date(statData.mtime);
  78.                     var timeNow = new Date();
  79.                     // 1 day delay
  80.                     if (timeNow.getDate() !== timeModifyed.getDate()) {
  81.                         // File is past its expiration date, get new file
  82.                         console.log('Downloading new copy!');
  83.                         getMembers(res, guildFile);
  84.                     } else {
  85.                         console.log('Using local copy!');
  86.                         res.setHeader('Content-Type', 'application/json');
  87.                         res.send(fileData);
  88.                     }
  89.                 }
  90.             });
  91.         }
  92.     });
  93. });
  94.  
  95. // Route for everything else.
  96. app.get('*', function (req, res) {
  97.     //res.send('Route not found.');
  98.     res.sendFile(path.join(__dirname, '/public/404.html'));
  99. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement