Advertisement
erinx

TibiaChar RPC Server

Jun 5th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. TibiaChar RPC Server.
  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. //------------------------*/
  19.  
  20. // Config:
  21.   const port = 9980; //set the port for the RPC server to listen on.
  22.  
  23.  
  24. //This is the code.
  25.  
  26. function tibiachar(){
  27.   this.request = require('request');
  28. }
  29.  
  30. tibiachar.prototype = {
  31.    
  32.   get : function(name, collo){
  33.       this.request('https://secure.tibia.com/community/?subtopic=characters&name=' + name, function(error, response, body){
  34.         var data = {};
  35.         data.name = name.replace(/[^A-Za-z]/g, ' ');
  36.         if(body.indexOf('<td>Sex:</td><td>') > 1){
  37.           data['sex'] = body.split('<td>Sex:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
  38.         }else{
  39.           data['sex'] = null;
  40.         }        
  41.         if(body.indexOf('<td>Vocation:</td><td>') > 1){
  42.           data['vocation'] = body.split('<td>Vocation:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
  43.         }else{
  44.           data['vocation'] = null;
  45.         }  
  46.         if(body.indexOf('Level:</td><td>') > 1){
  47.           data['level'] = body.split('Level:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
  48.         }else{
  49.           data['level'] = null;
  50.         }      
  51.         if(body.indexOf('<nobr>Achievement Points:</nobr></td><td>') > 1){
  52.           data['points'] = body.split('<nobr>Achievement Points:</nobr></td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
  53.         }else{
  54.           data['points'] = null;
  55.         }      
  56.         if(body.indexOf('<td>World:</td><td>') > 1){
  57.           data['world'] = body.split('><td>World:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
  58.         }else{
  59.           data['world'] = null;
  60.         }        
  61.         if(body.indexOf('<td>Residence:</td><td>') > 1){
  62.           data['home'] = body.split('<td>Residence:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
  63.         }else{
  64.           data['home'] = null;
  65.         }
  66.         if(body.indexOf('<td>Guild&#160;Membership:</td><td>') > 1){
  67.           data['guild'] = body.split('<td>Guild&#160;Membership:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
  68.         }else{
  69.           data['guild'] = null;
  70.         }      
  71.         if(body.indexOf('<td>Last Login:</td><td>') > 1){
  72.           data['last'] = body.split('<td>Last Login:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
  73.         }else{
  74.           data['last'] = null;
  75.         }  
  76.         if(body.indexOf('<td valign=top>Comment:</td><td>') > 1){
  77.           data['comment'] = body.split('<td valign=top>Comment:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
  78.         }else{
  79.           data['comment'] = null;
  80.         }
  81.         if(body.indexOf('<td>Account&#160;Status:</td><td>') > 1){
  82.           data['status'] = body.split('<td>Account&#160;Status:</td><td>')[1].split('</td>')[0].replace(/[ ]+$/g, '');
  83.         }else{
  84.           data['status'] = null;
  85.         }
  86.         collo(data);  
  87.     });    
  88.   }        
  89. };    
  90.  
  91. var tib = new tibiachar();
  92. var rpc = require('express')();
  93.  
  94. rpc.use(function(req, res, next){
  95.   res.header("Access-Control-Allow-Origin", "*");
  96.   res.header("Content-Type", "application/json");
  97.   next();
  98. });
  99.  
  100. rpc.get('/:data', function(req, res){
  101.   tib.get(req.params.data, function(i){
  102.     res.send(JSON.stringify(i));
  103.   });
  104. });
  105.  
  106. rpc.listen(port, function(){
  107.   console.log('> TibiaChar RPC Server listening on port ' + port + '.');
  108. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement