|
| 1 | +/* |
| 2 | +
|
| 3 | +TibiaChar RPC Server 2.1 |
| 4 | +
|
| 5 | +Provies Tibia character profile data via a JSON API. |
| 6 | +
|
| 7 | +Hint: Hash the name with a seed, have users put it in their comment section, |
| 8 | +and you have a confirmed method of linking tibia profiles to other profiles? |
| 9 | +
|
| 10 | +love, Erin Steph 2017 |
| 11 | +(See me on refugia!) |
| 12 | +
|
| 13 | +*/ |
| 14 | + |
| 15 | +/*------ installing ------// |
| 16 | +Express: $ npm install express --save |
| 17 | +Request: $ npm install request |
| 18 | +Sha.js: $ npm install sha.js |
| 19 | +//------------------------*/ |
| 20 | + |
| 21 | +// Config: |
| 22 | + const port = 9980; // set the port for the RPC server to listen on. Default for TibiaChar is 9980. |
| 23 | + const seed = 'ygfkdsgif87yt7o8fhkshksjhf7'; // CHANGE THIS. 10 - infinity random chars, more chars = more secure, but slower. Around 30 is good. |
| 24 | + |
| 25 | + |
| 26 | +//This is the code. Don't mess if you think you might break something. |
| 27 | + |
| 28 | + |
| 29 | +console.log('-----------------------------------------'); |
| 30 | +console.log('| _____ _ _ _ _____ _ |'); |
| 31 | +console.log('| |_ _|_| |_|_|___| | |_ ___ ___ |'); |
| 32 | +console.log('| | | | | . | | .\'| --| | .\'| _| |'); |
| 33 | +console.log('| |_| |_|___|_|__,|_____|_|_|__,|_| |'); |
| 34 | +console.log('------------------------------------v2.1-'); |
| 35 | + |
| 36 | + |
| 37 | +function tibiachar(){ |
| 38 | + this.request = require('request'); |
| 39 | +} |
| 40 | + |
| 41 | +tibiachar.prototype = { |
| 42 | + |
| 43 | + get : function(name, collo){ |
| 44 | + |
| 45 | + function strip_tags(html){ |
| 46 | + if(arguments.length < 3){ |
| 47 | + html = html.replace(/<\/?(?!\!)[^>]*>/gi, ''); |
| 48 | + }else{ |
| 49 | + var allowed = arguments[1]; |
| 50 | + var specified = eval("["+arguments[2]+"]"); |
| 51 | + if(allowed){ |
| 52 | + var regex='</?(?!(' + specified.join('|') + '))\b[^>]*>'; |
| 53 | + html = html.replace(new RegExp(regex, 'gi'), ''); |
| 54 | + }else{ |
| 55 | + var regex='</?(' + specified.join('|') + ')\b[^>]*>'; |
| 56 | + html = html.replace(new RegExp(regex, 'gi'), ''); |
| 57 | + } |
| 58 | + } |
| 59 | + var clean_string = html; |
| 60 | + return clean_string; |
| 61 | + } |
| 62 | + this.request('https://secure.tibia.com/community/?subtopic=characters&name=' + name.replace(/[^A-Za-z]+/g, '+'), function(error, response, body){ |
| 63 | + var data = {}; |
| 64 | + data.name = name.replace(/[^A-Za-z]+/g, ' '); |
| 65 | + data['name-linked'] = '<a target="_blank" href="https://secure.tibia.com/community/?subtopic=characters&name=' + name.replace(/[^A-Za-z]+/g, '+') + '">' + data.name + '</a>'; |
| 66 | + if(body.indexOf('<td>Sex:</td><td>') > 1){ |
| 67 | + data['sex'] = body.split('<td>Sex:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, ''); |
| 68 | + }else{ |
| 69 | + data['sex'] = null; |
| 70 | + } |
| 71 | + if(body.indexOf('<td>Vocation:</td><td>') > 1){ |
| 72 | + data['vocation'] = body.split('<td>Vocation:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, ''); |
| 73 | + }else{ |
| 74 | + data['vocation'] = null; |
| 75 | + } |
| 76 | + if(body.indexOf('Level:</td><td>') > 1){ |
| 77 | + data['level'] = body.split('Level:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, ''); |
| 78 | + }else{ |
| 79 | + data['level'] = null; |
| 80 | + } |
| 81 | + if(body.indexOf('<nobr>Achievement Points:</nobr></td><td>') > 1){ |
| 82 | + data['points'] = body.split('<nobr>Achievement Points:</nobr></td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, ''); |
| 83 | + }else{ |
| 84 | + data['points'] = null; |
| 85 | + } |
| 86 | + if(body.indexOf('<td>World:</td><td>') > 1){ |
| 87 | + data['world'] = body.split('><td>World:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, ''); |
| 88 | + }else{ |
| 89 | + data['world'] = null; |
| 90 | + } |
| 91 | + if(body.indexOf('<td>Residence:</td><td>') > 1){ |
| 92 | + data['home'] = body.split('<td>Residence:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, ''); |
| 93 | + }else{ |
| 94 | + data['home'] = null; |
| 95 | + } |
| 96 | + if(body.indexOf('<td>Guild Membership:</td><td>') > 1){ |
| 97 | + data['guild-text'] = strip_tags(body.split('<td>Guild Membership:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '')).replace(/\&\#160\;/g, ' '); |
| 98 | + data['guild-linked'] = body.split('<td>Guild Membership:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '').replace(/\&\#160\;/g, ' '); |
| 99 | + data['guild-name'] = strip_tags(body.split('<td>Guild Membership:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '')).replace(/\&\#160\;/g, ' ').split(' of the ')[1]; |
| 100 | + data['guild-rank'] = strip_tags(body.split('<td>Guild Membership:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '')).replace(/\&\#160\;/g, ' ').split(' of the ')[0]; |
| 101 | + }else{ |
| 102 | + data['guild-text'] = null; |
| 103 | + data['guild-linked'] = null; |
| 104 | + data['guild-name'] = null; |
| 105 | + data['guild-rank'] = null; |
| 106 | + } |
| 107 | + if(body.indexOf('<td>Last Login:</td><td>') > 1){ |
| 108 | + data['last-online'] = body.split('<td>Last Login:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '').replace(/\&\#160\;/g, ' '); |
| 109 | + }else{ |
| 110 | + data['last-online'] = null; |
| 111 | + } |
| 112 | + if(body.indexOf('<td valign=top>Comment:</td><td>') > 1){ |
| 113 | + data['comment'] = body.split('<td valign=top>Comment:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, ''); |
| 114 | + }else{ |
| 115 | + data['comment'] = null; |
| 116 | + } |
| 117 | + if(body.indexOf('<td>Account Status:</td><td>') > 1){ |
| 118 | + data['status'] = body.split('<td>Account Status:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, ''); |
| 119 | + }else{ |
| 120 | + data['status'] = null; |
| 121 | + } |
| 122 | + collo(data); |
| 123 | + }); |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +var tib = new tibiachar(); |
| 128 | +var rpc = require('express')(); |
| 129 | +var createHash = require('sha.js') |
| 130 | + |
| 131 | +function sha(inp){ |
| 132 | + var sha256 = createHash('sha1') |
| 133 | + var h = sha256.update(inp, 'utf8').digest('hex') |
| 134 | + return h; |
| 135 | +} |
| 136 | + |
| 137 | +rpc.use(function(req, res, next){ |
| 138 | + res.header("Access-Control-Allow-Origin", "*"); |
| 139 | + //res.header("Content-Type", "application/json"); |
| 140 | + next(); |
| 141 | +}); |
| 142 | + |
| 143 | +rpc.get('/:data', function(req, res){ |
| 144 | + tib.get(req.params.data, function(i){ |
| 145 | + i['hash'] = sha(i['name']); |
| 146 | + console.log('> loaded data for ' + i['name']); |
| 147 | + res.header("Content-Type", "application/json"); |
| 148 | + res.send(JSON.stringify(i)); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +rpc.get('', function(req, res){ |
| 153 | + if(req.query.n){ |
| 154 | + tib.get(req.query.n, function(i){ |
| 155 | + i['hash'] = sha(i['name']); |
| 156 | + console.log('> loaded data for ' + i['name']); |
| 157 | + res.header("Content-Type", "application/json"); |
| 158 | + res.send(JSON.stringify(i)); |
| 159 | + }); |
| 160 | + }else{ |
| 161 | + res.send('<html><body><form action="/" method="GET"><input name="n" type="text" placeholder="Name"><button id="get" type="submit">Get</button></form></body></html>'); |
| 162 | + } |
| 163 | +}); |
| 164 | + |
| 165 | +rpc.listen(port, function(){ |
| 166 | + console.log('> TibiaChar RPC Server listening on port ' + port + '.'); |
| 167 | +}); |
0 commit comments