Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mysql = require('mysql');
- var request = require("request");
- var fs = require('fs');
- var connection = mysql.createConnection({
- host: 'localhost', // MySQL Host
- user: 'root', // MySQL User
- password: '', // MySQL Password
- database: 'skinpay' // MySQL Databse
- });
- connection.connect(); //Connect to MySQL
- console.log('[SERVER] Connected to the MySQL Service.');
- function update() {
- var url = "http://csgobackpack.net/api/GetItemsList/?key=dfr6r7apyj9cn7rn";
- request({
- url: url,
- json: true
- }, function (error, response, body) {
- if (!error && response.statusCode === 200) {
- //console.log(body) // Print the json response
- jsonString = JSON.stringify(body);
- var prices = require('./prices.json');
- fs.truncate('prices.json', 0, function(){console.log('done')})
- fs.writeFile('prices.json', jsonString, function (err) {
- if (err) throw err;
- doa();
- });
- }
- });
- };
- function doa() {
- var prices = require('./prices.json');
- for (var item in prices) {
- if (prices.hasOwnProperty(item)) {
- var itemjson = prices[item]; // do something with each item...
- var name = itemjson['name'];
- name = name.replace(/ /g, "%20");
- name = name.replace(/★/g, "%E2%98%85");
- updatePricesinDB(name);
- }
- }
- };
- function updatePricesinDB(name) {
- var url = "http://csgobackpack.net/api/GetItemPrice/?currency=USD&id=" + name + "&time=7?key=dfr6r7apyj9cn7rn";
- request({
- url: url,
- json: true
- }, function(error, response, body) {
- if (!error && response.statusCode === 200) {
- jsonString = JSON.stringify(body);
- var price = body['average_price'];
- connection.query('INSERT INTO prices (name, price) VALUES ("' + name + '", "' + price + '")', function (err, rows, fields) {
- if (err) throw err;
- });
- }
- });
- };
- doa();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement