Advertisement
Guest User

Untitled

a guest
May 29th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fetch = require('node-fetch');
  2. const express = require('express');
  3. const bodyParser = require('body-parser');
  4. const moment = require('moment');
  5.  
  6. const app = express();
  7.  
  8. app.use(bodyParser.urlencoded({
  9.     extended: false
  10. }));
  11.  
  12. const options = {
  13.     method: 'GET',
  14.     headers: {
  15.         'Accept': 'application/json',
  16.         'Accept-Charset': 'utf-8',
  17.         'Accept-Encoding': 'gzip'
  18.     },
  19.     compress: true
  20. };
  21.  
  22. const getLatestChangeId = () =>
  23.     fetch('http://api.poe.ninja/api/Data/GetStats', options)
  24.         .then(result => result.json)
  25.         .catch(() => throw new Error('Could not get latest change id from poe.ninja '));
  26.  
  27. const getStashData = changeId => fetch(`http://www.pathofexile.com/api/public-stash-tabs?id=${changeId}`, options)
  28.         .then(result => result.json())
  29.         .catch(() => throw new Error('Could not fetch POE Stash tab data from the API'));
  30.  
  31. const loadItemPrices = currentTime =>
  32.     fetch(
  33.         `http://cdn.poe.ninja/api/Data/GetUniqueWeaponOverview?league=Legacy&date=${currentTime}`,
  34.         options
  35.     )
  36.     .then(result => result.json())
  37.     .catch(() => throw new Error('Could not fetch armor price data from poe.ninja'));
  38.  
  39. let currencyRates = [];
  40. let itemPrices = [];
  41.  
  42. loadItemPrices(new moment().format("YYYY-MM-DD"))
  43.     .then(response => {
  44.         const lines = response['lines'];
  45.  
  46.         itemPrices = lines
  47.             .map(line => {
  48.                 name: lines[i].name,
  49.                 price: lines[i].chaosValue
  50.             });
  51.  
  52.         console.log('Loaded unique armors into the price list');
  53.     })
  54.     .catch(() => throw new Error('Could not load item prices'));
  55.  
  56. app.get('/uniques', function (req, res) {
  57.     var changeId = undefined;
  58.  
  59.     getLatestChangeId()
  60.         .then(function (response) {
  61.             changeId = response['nextChangeId'];
  62.             console.log(`success, latest change id is : ${changeId}`);
  63.  
  64.             getStashData(changeId)
  65.                 .then(response => console.log(response['next_change_id']))
  66.                 .catch(error => console.error('failed to grab stash data', error));
  67.         })
  68.         .catch(error => console.error(`failed to parse latest change id ${error}`));
  69.  
  70.     res.status(200).send('got request for unique items');
  71.     res.end('Response will be available on console, nothing to look here!');
  72. });
  73.  
  74. const port = 8000;
  75. app.listen(port);
  76. console.log(`Listening at http://localhost:${port}`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement