Advertisement
Guest User

Untitled

a guest
Oct 7th, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var mysql = require('mysql');
  2. var request = require("request");
  3. var fs = require('fs');
  4.  
  5. var connection = mysql.createConnection({
  6.     host: 'localhost', // MySQL Host
  7.     user: 'root', // MySQL User
  8.     password: '', // MySQL Password
  9.     database: 'skinpay' // MySQL Databse
  10. });
  11. connection.connect(); //Connect to MySQL
  12. console.log('[SERVER] Connected to the MySQL Service.');
  13.  
  14.  
  15. function update() {
  16.     var url = "http://csgobackpack.net/api/GetItemsList/?key=dfr6r7apyj9cn7rn";
  17.     request({
  18.         url: url,
  19.         json: true
  20.     }, function (error, response, body) {
  21.         if (!error && response.statusCode === 200) {
  22.             //console.log(body) // Print the json response
  23.             jsonString = JSON.stringify(body);
  24.             var prices = require('./prices.json');
  25.             fs.truncate('prices.json', 0, function(){console.log('done')})
  26.             fs.writeFile('prices.json', jsonString, function (err) {
  27.                 if (err) throw err;
  28.                 doa();
  29.             });
  30.         }
  31.     });
  32. };
  33.  
  34.  
  35. function doa() {
  36.     var prices = require('./prices.json');
  37.     for (var item in prices) {
  38.         if (prices.hasOwnProperty(item)) {
  39.             var itemjson = prices[item]; // do something with each item...
  40.             var name = itemjson['name'];
  41.             name = name.replace(/ /g, "%20");
  42.             name = name.replace(/★/g, "%E2%98%85");
  43.             updatePricesinDB(name);
  44.         }
  45.     }
  46. };
  47. function updatePricesinDB(name) {
  48.     var url = "http://csgobackpack.net/api/GetItemPrice/?currency=USD&id=" + name + "&time=7?key=dfr6r7apyj9cn7rn";
  49.     request({
  50.         url: url,
  51.         json: true
  52.     }, function(error, response, body) {
  53.         if (!error && response.statusCode === 200) {
  54.             jsonString = JSON.stringify(body);
  55.             var price = body['average_price'];
  56.             connection.query('INSERT INTO prices (name, price) VALUES ("' + name + '", "' + price + '")', function (err, rows, fields) {
  57.                 if (err) throw err;
  58.                 });
  59.             }
  60.         });
  61. };
  62.  
  63.  
  64. doa();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement