/* TibiaChar RPC Server 2.0 Provies Tibia character profile data via a JSON API. Hint: Hash the name with a seed, have users put it in their comment section, and you have a confirmed method of linking tibia profiles to other profiles? love, Erin Steph 2017 (See me on refugia!) */ /*------ installing ------// Express: $ npm install express --save Request: $ npm install request Sha.js: $ npm install sha.js //------------------------*/ // Config: const port = 9980; //set the port for the RPC server to listen on. const seed = 'jhhghsalhgusrhghurhguhjvxjsvhra4y79ty8498w'; //This is the code. console.log('-----------------------------------------'); console.log('| _____ _ _ _ _____ _ |'); console.log('| |_ _|_| |_|_|___| | |_ ___ ___ |'); console.log('| | | | | . | | .\'| --| | .\'| _| |'); console.log('| |_| |_|___|_|__,|_____|_|_|__,|_| |'); console.log('------------------------------------v2.0-'); function tibiachar(){ this.request = require('request'); } tibiachar.prototype = { get : function(name, collo){ function strip_tags(html){ if(arguments.length < 3){ html = html.replace(/<\/?(?!\!)[^>]*>/gi, ''); }else{ var allowed = arguments[1]; var specified = eval("["+arguments[2]+"]"); if(allowed){ var regex=']*>'; html = html.replace(new RegExp(regex, 'gi'), ''); }else{ var regex=']*>'; html = html.replace(new RegExp(regex, 'gi'), ''); } } var clean_string = html; return clean_string; } this.request('https://secure.tibia.com/community/?subtopic=characters&name=' + name.replace(/[^A-Za-z]+/g, '+'), function(error, response, body){ var data = {}; data.name = name.replace(/[^A-Za-z]+/g, ' '); data['name-linked'] = '' + data.name + ''; if(body.indexOf('Sex:') > 1){ data['sex'] = body.split('Sex:')[1].split('')[0].replace(/[ ]+$/g, ''); }else{ data['sex'] = null; } if(body.indexOf('Vocation:') > 1){ data['vocation'] = body.split('Vocation:')[1].split('')[0].replace(/[ ]+$/g, ''); }else{ data['vocation'] = null; } if(body.indexOf('Level:') > 1){ data['level'] = body.split('Level:')[1].split('')[0].replace(/[ ]+$/g, ''); }else{ data['level'] = null; } if(body.indexOf('Achievement Points:') > 1){ data['points'] = body.split('Achievement Points:')[1].split('')[0].replace(/[ ]+$/g, ''); }else{ data['points'] = null; } if(body.indexOf('World:') > 1){ data['world'] = body.split('>World:')[1].split('')[0].replace(/[ ]+$/g, ''); }else{ data['world'] = null; } if(body.indexOf('Residence:') > 1){ data['home'] = body.split('Residence:')[1].split('')[0].replace(/[ ]+$/g, ''); }else{ data['home'] = null; } if(body.indexOf('Guild Membership:') > 1){ data['guild-text'] = strip_tags(body.split('Guild Membership:')[1].split('')[0].replace(/[ ]+$/g, '')).replace(/\&\#160\;/g, ' '); data['guild-linked'] = body.split('Guild Membership:')[1].split('')[0].replace(/[ ]+$/g, '').replace(/\&\#160\;/g, ' '); data['guild-name'] = strip_tags(body.split('Guild Membership:')[1].split('')[0].replace(/[ ]+$/g, '')).replace(/\&\#160\;/g, ' ').split(' of the ')[1]; data['guild-rank'] = strip_tags(body.split('Guild Membership:')[1].split('')[0].replace(/[ ]+$/g, '')).replace(/\&\#160\;/g, ' ').split(' of the ')[0]; }else{ data['guild-text'] = null; data['guild-linked'] = null; data['guild-name'] = null; data['guild-rank'] = null; } if(body.indexOf('Last Login:') > 1){ data['last-online'] = body.split('Last Login:')[1].split('')[0].replace(/[ ]+$/g, '').replace(/\&\#160\;/g, ' '); }else{ data['last-online'] = null; } if(body.indexOf('Comment:') > 1){ data['comment'] = body.split('Comment:')[1].split('')[0].replace(/[ ]+$/g, ''); }else{ data['comment'] = null; } if(body.indexOf('Account Status:') > 1){ data['status'] = body.split('Account Status:')[1].split('')[0].replace(/[ ]+$/g, ''); }else{ data['status'] = null; } collo(data); }); } }; var tib = new tibiachar(); var rpc = require('express')(); var createHash = require('sha.js') function sha(inp){ var sha256 = createHash('sha1') var h = sha256.update(inp, 'utf8').digest('hex') return h; } rpc.use(function(req, res, next){ res.header("Access-Control-Allow-Origin", "*"); res.header("Content-Type", "application/json"); next(); }); rpc.get('/:data', function(req, res){ tib.get(req.params.data, function(i){ i['hash'] = sha(i['name']); console.log('> loaded data for ' + i['name']); res.send(JSON.stringify(i)); }); }); rpc.listen(port, function(){ console.log('> TibiaChar RPC Server listening on port ' + port + '.'); });