-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
92 lines (82 loc) · 2.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var config = require('./config');
var proxy = require('./custom_modules/proxy');
var card_finder_factory = require('./custom_modules/card/finder/factory');
var request = require('request');
var cheerio = require('cheerio');
var http = require('http');
var express = require('express');
var app = new express();
var proxy_list = [];
var sites = ['ligamagic', 'tcgplayer', 'magicdomain'];
String.prototype.urldecode = function() {
return decodeURIComponent(this).replace(/\+/g, ' ');
}
/* UPDATE the proxy_list */
var update_proxies = function (callback) {
console.log("Updating proxy list");
proxy.get(function(pl) {
proxy_list = pl;
console.log("Proxy list updated");
});
callback !== undefined && callback();
setTimeout(update_proxies, 1000 * 60 * 60 * 24); //Update proxies every 24 hours
}
update_proxies();
app.get('/', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.json({status: 'error', data: { msg : 'Method not found' } });
});
app.get('/update_list', function(req, res) {
res.setHeader('Content-Type', 'application/json');
update_proxies(function() {
res.json({status: 'ok', data: { msg : 'Proxy list updated' } });
});
});
app.get('/get_proxy', function(req, res) {
res.setHeader('Content-Type', 'application/json');
if(proxy_list.length == 0) {
update_proxies(function() {
var random = parseInt(Math.random() * proxy_list.length, 10);
res.json({status: 'ok', data: { proxy: proxy_list[random] } });
});
}else{
var random = parseInt(Math.random() * proxy_list.length, 10);
res.json({status: 'ok', data: { proxy: proxy_list[random] } });
}
});
app.get('/:website/card/:cardname', function(req, res) {
var website = req.params.website;
var cardname = req.params.cardname;
res.setHeader('Content-Type', 'application/json');
if(sites.indexOf(website) == -1) {
res.json({status: 'error', data: { msg : 'Source not found.' } });
}else{
try {
var random = parseInt(Math.random() * proxy_list.length, 10);
var finder = card_finder_factory.get(website);
var find_promise = finder.find(cardname, proxy_list[random]);
find_promise.then(function(card) {
res.json({status: 'ok', data:
{
card: card.title.urldecode(),
currencies : card.currencies,
sets: card.sets,
prices : card.prices,
url: card.url
}
});
}, function(err) {
res.json({status: 'error', data:
{
msg: err
}
});
});
}catch(err) {
console.log(err);
}
}
});
const PORT = process.env.PORT || 5000
app.listen(PORT);
console.log("Running on port " + PORT);