Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Node JS battle.net API tool - load guild members data
- *
- * This example will setup a simple express HTTP server for serving /public/ directory
- * and load guild members data from battle.net and save a copy to your server for later use.
- *
- * 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)
- * this will first search a copy of the guild members data from your local server,
- * if local copy is not present it will download a new copy from the battle.net.
- * In this way you won't run out of free requests to your battle net api.
- *
- * Your copy of the data will be used about 1 day, then it will load a new copy.
- */
- var fs = require('fs');
- var path = require('path');
- var request = require("request");
- var express = require('express');
- var app = express();
- var server = require('http').createServer(app);
- var port = process.env.PORT || 80;
- server.listen(port, function () {
- console.log('Server listening at port %d', port);
- });
- // Server static assets
- app.use(express.static(path.join(__dirname, '/public')));
- //app.use('/static', express.static(path.join(__dirname, '/public')));
- // Route for home
- app.get('/', function (req, res) {
- res.sendFile(path.join(__dirname, '/public/index.html'));
- });
- /*
- * Battle.net API tools
- * - public/guild-members.json ( /api/json/guild-members )
- * - loaded once per day, then use local copy
- */
- // Function: Load new JSON from battle.net
- function getMembers(res, wFile) {
- request({
- url: 'https://eu.api.battle.net/wow/guild/YOUR_SERVER_NAME/YOUR_GUILD_NAME?fields=members&locale=en_GB&apikey=YOUR_API_KEY',
- json: true
- }, function (error, response, body) {
- if (!error && response.statusCode === 200) {
- fs.writeFile(wFile, JSON.stringify(body), 'utf8', function (errorWrite, data) {
- if (errorWrite) {
- console.log("Write error:", errorWrite);
- } else {
- res.setHeader('Content-Type', 'application/json');
- res.send(body);
- }
- });
- } else {
- console.log("Request error:", error);
- }
- });
- }
- // Express route for the battle.net api: guild-members
- app.get('/api/json/guild-members', function (req, res) {
- var guildFile = path.join(__dirname, '/public/data-guild-members.json');
- // Load JSON file from local
- fs.readFile(guildFile, 'utf8', function (errorRead, fileData) {
- if (errorRead) {
- // File does not exists or can't be read, try creating new file
- console.log('Downloading new copy!');
- getMembers(res, guildFile);
- } else {
- fs.stat(guildFile, function (errorStat, statData) {
- if (errorStat) {
- console.log("Stat error:", errorStat);
- } else {
- var timeModifyed = new Date(statData.mtime);
- var timeNow = new Date();
- // 1 day delay
- if (timeNow.getDate() !== timeModifyed.getDate()) {
- // File is past its expiration date, get new file
- console.log('Downloading new copy!');
- getMembers(res, guildFile);
- } else {
- console.log('Using local copy!');
- res.setHeader('Content-Type', 'application/json');
- res.send(fileData);
- }
- }
- });
- }
- });
- });
- // Route for everything else.
- app.get('*', function (req, res) {
- //res.send('Route not found.');
- res.sendFile(path.join(__dirname, '/public/404.html'));
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement