<commit_before>var express = require('express')
  , router = express.Router()
  , settings = require('../lib/settings')
  , locale = require('../lib/locale')
  , db = require('../lib/database')
  , lib = require('../lib/explorer')
  , qr = require('qr-image');

function route_get_block(res, blockhash) {
  lib.get_block(blockhash, function (block) {
    if (block != 'There was an error. Check your console.') {
      if (blockhash == settings.genesis_block) {
        db.get_stats(settings.coin, function(stats) {
          res.render('block', { active: 'block', block: block, stats: stats, confirmations: settings.confirmations, txs: 'GENESIS'});
        });
      } else {
        //console.log(block);
        db.get_txs(block, function(txs) {
          if (txs.length > 0) {
            //console.log(txs);
            db.get_stats(settings.coin, function(stats) {
              res.render('block', { active: 'block', block: block, stats: stats, confirmations: settings.confirmations, txs: txs});
            });
          } else {
            db.create_txs(block, function(){
              db.get_txs(block, function(ntxs) {
                if (ntxs.length > 0) {
                  db.get_stats(settings.coin, function(stats) {
                    res.render('block', { active: 'block', block: block, stats: stats, confirmations: settings.confirmations, txs: ntxs});
                  });
                } else {
                  route_get_index(res, 'Block not found: ' + blockhash);
                }
              });
            });
          }
        });
      }
    } else {
      route_get_index(res, 'Block not found: ' + blockhash);
    }
  });
}
/* GET functions */

function route_get_tx(res, txid) {
  if (txid == settings.genesis_tx) {
    route_get_block(res, settings.genesis_block);
  } else {
    db.get_tx(txid, function(tx) {
      if (tx) {
        db.get_stats(settings.coin, function(stats){
          lib.get_blockcount(function(blockcount) {
            res.render('tx', { active: 'tx', tx: tx, stats: stats, confirmations: settings.confirmations, blockcount: blockcount});
          });
        });
      } 
      else {
        lib.get_rawtransaction(txid, function(rtx) {
          if (rtx.txid) {
            db.create_tx(txid, function(err) {
              if (err) {
                route_get_index(res, null);
              } else {
                db.get_tx(txid, function(newtx) {
                  db.get_stats(settings.coin, function(stats){
                    res.render('tx', { active: 'tx', tx: newtx, stats: stats, confirmations: settings.confirmations});
                  });
                });
              }
            });
          } else {
            route_get_index(res, null);
          }
        });  
      }
    });
  }
}

function route_get_index(res, error) {
  db.get_stats(settings.coin, function(stats) {
    //console.log(stats.difficulty);
    lib.get_blockhash(stats.last, function(hash) {
      lib.get_block(hash, function (block) {
        res.render('index', { active: 'home', stats: stats, block: block, txs: stats.last_txs, error: error, warning: null});       
      });
    });
  });
}

function route_get_address(res, hash, count) {
  db.get_stats(settings.coin, function(stats) {
    db.get_address(hash, function(address) {
      if (address) {
        var txs = [];
        var hashes = address.txs.reverse();
        if (address.txs.length < count) {
          count = address.txs.length;
        }
        lib.syncLoop(count, function (loop) {
          var i = loop.iteration();
          db.get_tx(hashes[i].addresses, function(tx) {
            if (tx) {
              txs.push(tx);
              loop.next();
            } else {
              loop.next();
            }
          });
        }, function(){
         
          res.render('address', { active: 'address', stats: stats, address: address, txs: txs});
        });
        
      } else {
        route_get_index(res, hash + ' not found');
      }
    });
  });
}

/* GET home page. */
router.get('/', function(req, res) {
  route_get_index(res, null);
});

router.get('/info', function(req, res) {
  db.get_stats(settings.coin, function(stats){
  	res.render('info', { active: 'info', address: settings.address, hashes: settings.api,  stats: stats });
  });
});

router.get('/markets/:market', function(req, res) {
  var market = req.param('market');
  if (settings.markets.enabled.indexOf(market) != -1) {
    db.get_stats(settings.coin, function (stats) { 
      db.get_market(market, function(data) {
        var market_data = {
          coin: settings.markets.coin,
          exchange: settings.markets.exchange,
          data: data,
        };
        res.render('./markets/' + market, { 
          active: 'markets', 
          marketdata: market_data, 
          market: market,
          stats: stats
        });
      });
    });
  } else {
    route_get_index(res, null);
  }
});

router.get('/richlist', function(req, res) {
  if (settings.display.richlist == true ) {  
    db.get_stats(settings.coin, function (stats) { 
      db.get_richlist(settings.coin, function(richlist){
        //console.log(richlist);
        if (richlist) {
          db.get_distribution(richlist, stats, function(distribution) {
            //console.log(distribution);
            res.render('richlist', { 
              active: 'richlist', 
              balance: richlist.balance, 
              received: richlist.received,
              stats: stats,
              dista: distribution.t_1_25,
              distb: distribution.t_26_50,
              distc: distribution.t_51_75,
              distd: distribution.t_76_100,
              diste: distribution.t_101plus,
              show_dist: settings.richlist.distribution,
              show_received: settings.richlist.received,
              show_balance: settings.richlist.balance,
            });
          });
        } else {
          route_get_index(res, null);
        }
      });
    });   
  } else {
    route_get_index(res, null);
  }
});

router.get('/reward', function(req, res){
  db.get_stats(settings.coin, function (stats) {
    console.log(stats);
    db.get_heavy(settings.coin, function (heavy) {
      //heavy = heavy;
      var votes = heavy.votes; 
      votes.sort(function (a,b) {
        if (a.count < b.count) {
          return -1;
        } else if (a.count > b.count) {
          return 1;
        } else {
         return 0;
        }
      });
          
      res.render('reward', { active: 'reward', stats: stats, heavy: heavy, votes: heavy.votes });
    });
  });
});

router.get('/tx/:txid', function(req, res) {
  route_get_tx(res, req.param('txid'));
});

router.get('/block/:hash', function(req, res) {
  route_get_block(res, req.param('hash'));
});

router.get('/address/:hash', function(req, res) {
  route_get_address(res, req.param('hash'), settings.txcount);
});

router.get('/address/:hash/:count', function(req, res) {
  route_get_address(res, req.param('hash'), req.param('count'));
});

router.post('/search', function(req, res) {
  var query = req.body.search;
  if (query.length == 64) {
    if (query == settings.genesis_tx) {
      route_get_block(res, settings.genesis_block);
    } else {
      db.get_tx(query, function(tx) {      
        if (tx) {
          db.get_stats(settings.coin, function(stats){
            lib.get_blockcount(function(blockcount) {
              res.render('tx', { active: 'tx', tx: tx, stats: stats, confirmations: settings.confirmations, blockcount: blockcount});
            });
          });
        } else {
          lib.get_block(query, function(block) {
            if (block != 'There was an error. Check your console.') {
              route_get_block(res, query);
            } else {
              route_get_index(res, locale.ex_search_error + query );
            }
          });
        }
      });
    }
  } else {
    db.get_address(query, function(address) {
      if (address) {
        route_get_address(res, address.a_id, settings.txcount);
      } else {
        lib.get_blockhash(query, function(hash) {
          if (hash != 'There was an error. Check your console.') {
            route_get_block(res, hash);
          } else {
            route_get_index(res, locale.ex_search_error + query );
          }
        });
      }
    });
  }
});

router.get('/qr/:string', function(req, res) {
  if (req.param('string')) {
    var address = qr.image(req.param('string'), { 
      type: 'png', 
      size: 4, 
      margin: 1, 
      ec_level: 'M' 
    });
    res.type('png');
    address.pipe(res);
  }
});

module.exports = router;
<commit_msg>search url fixes<commit_after>var express = require('express')
  , router = express.Router()
  , settings = require('../lib/settings')
  , locale = require('../lib/locale')
  , db = require('../lib/database')
  , lib = require('../lib/explorer')
  , qr = require('qr-image');

function route_get_block(res, blockhash) {
  lib.get_block(blockhash, function (block) {
    if (block != 'There was an error. Check your console.') {
      if (blockhash == settings.genesis_block) {
        res.render('block', { active: 'block', block: block, confirmations: settings.confirmations, txs: 'GENESIS'});
      } else {
        db.get_txs(block, function(txs) {
          if (txs.length > 0) {          
            res.render('block', { active: 'block', block: block, confirmations: settings.confirmations, txs: txs});
          } else {
            db.create_txs(block, function(){
              db.get_txs(block, function(ntxs) {
                if (ntxs.length > 0) {
                  res.render('block', { active: 'block', block: block, confirmations: settings.confirmations, txs: ntxs});
                } else {
                  route_get_index(res, 'Block not found: ' + blockhash);
                }
              });
            });
          }
        });
      }
    } else {
      route_get_index(res, 'Block not found: ' + blockhash);
    }
  });
}
/* GET functions */

function route_get_tx(res, txid) {
  if (txid == settings.genesis_tx) {
    route_get_block(res, settings.genesis_block);
  } else {
    db.get_tx(txid, function(tx) {
      if (tx) {
        lib.get_blockcount(function(blockcount) {
          res.render('tx', { active: 'tx', tx: tx, confirmations: settings.confirmations, blockcount: blockcount});
        });
      } 
      else {
        lib.get_rawtransaction(txid, function(rtx) {
          if (rtx.txid) {
            db.create_tx(txid, function(err) {
              if (err) {
                route_get_index(res, null);
              } else {
                db.get_tx(txid, function(newtx) {
                  res.render('tx', { active: 'tx', tx: newtx, confirmations: settings.confirmations});
                });
              }
            });
          } else {
            route_get_index(res, null);
          }
        });  
      }
    });
  }
}

function route_get_index(res, error) {
  res.render('index', { active: 'home', error: error, warning: null});        
}

function route_get_address(res, hash, count) {
  db.get_address(hash, function(address) {
    if (address) {
      var txs = [];
      var hashes = address.txs.reverse();
      if (address.txs.length < count) {
        count = address.txs.length;
      }
      lib.syncLoop(count, function (loop) {
        var i = loop.iteration();
        db.get_tx(hashes[i].addresses, function(tx) {
          if (tx) {
            txs.push(tx);
            loop.next();
          } else {
            loop.next();
          }
        });
      }, function(){
       
        res.render('address', { active: 'address', address: address, txs: txs});
      });
      
    } else {
      route_get_index(res, hash + ' not found');
    }
  });
}

/* GET home page. */
router.get('/', function(req, res) {
  route_get_index(res, null);
});

router.get('/info', function(req, res) {
  res.render('info', { active: 'info', address: settings.address, hashes: settings.api });
});

router.get('/markets/:market', function(req, res) {
  var market = req.param('market');
  if (settings.markets.enabled.indexOf(market) != -1) {
    db.get_market(market, function(data) {
      var market_data = {
        coin: settings.markets.coin,
        exchange: settings.markets.exchange,
        data: data,
      };
      res.render('./markets/' + market, { 
        active: 'markets', 
        marketdata: market_data, 
        market: market,
      });
    });
  } else {
    route_get_index(res, null);
  }
});

router.get('/richlist', function(req, res) {
  if (settings.display.richlist == true ) {  
    db.get_stats(settings.coin, function (stats) { 
      db.get_richlist(settings.coin, function(richlist){
        //console.log(richlist);
        if (richlist) {
          db.get_distribution(richlist, stats, function(distribution) {
            //console.log(distribution);
            res.render('richlist', { 
              active: 'richlist', 
              balance: richlist.balance, 
              received: richlist.received,
              stats: stats,
              dista: distribution.t_1_25,
              distb: distribution.t_26_50,
              distc: distribution.t_51_75,
              distd: distribution.t_76_100,
              diste: distribution.t_101plus,
              show_dist: settings.richlist.distribution,
              show_received: settings.richlist.received,
              show_balance: settings.richlist.balance,
            });
          });
        } else {
          route_get_index(res, null);
        }
      });
    });   
  } else {
    route_get_index(res, null);
  }
});

router.get('/reward', function(req, res){
  //db.get_stats(settings.coin, function (stats) {
    console.log(stats);
    db.get_heavy(settings.coin, function (heavy) {
      //heavy = heavy;
      var votes = heavy.votes; 
      votes.sort(function (a,b) {
        if (a.count < b.count) {
          return -1;
        } else if (a.count > b.count) {
          return 1;
        } else {
         return 0;
        }
      });
          
      res.render('reward', { active: 'reward', stats: stats, heavy: heavy, votes: heavy.votes });
    });
  //});
});

router.get('/tx/:txid', function(req, res) {
  route_get_tx(res, req.param('txid'));
});

router.get('/block/:hash', function(req, res) {
  route_get_block(res, req.param('hash'));
});

router.get('/address/:hash', function(req, res) {
  route_get_address(res, req.param('hash'), settings.txcount);
});

router.get('/address/:hash/:count', function(req, res) {
  route_get_address(res, req.param('hash'), req.param('count'));
});

router.post('/search', function(req, res) {
  var query = req.body.search;
  if (query.length == 64) {
    if (query == settings.genesis_tx) {
      res.redirect('/block/' + settings.genesis_block);
    } else {
      db.get_tx(query, function(tx) {      
        if (tx) {
          res.redirect('/tx/' +tx.txid);
        } else {
          lib.get_block(query, function(block) {
            if (block != 'There was an error. Check your console.') {
              res.redirect('/block/' + query);
            } else {
              route_get_index(res, locale.ex_search_error + query );
            }
          });
        }
      });
    }
  } else {
    db.get_address(query, function(address) {
      if (address) {
        res.redirect('/address/' + address.a_id);
      } else {
        lib.get_blockhash(query, function(hash) {
          if (hash != 'There was an error. Check your console.') {
            res.redirect('/block/' + hash);
          } else {
            route_get_index(res, locale.ex_search_error + query );
          }
        });
      }
    });
  }
});

router.get('/qr/:string', function(req, res) {
  if (req.param('string')) {
    var address = qr.image(req.param('string'), { 
      type: 'png', 
      size: 4, 
      margin: 1, 
      ec_level: 'M' 
    });
    res.type('png');
    address.pipe(res);
  }
});

router.get('/ext/summary', function(req, res) {
  lib.get_difficulty(function(difficulty) {
    if (difficulty['proof-of-work']) {
      if (settings.index.difficulty == 'POW') {
        difficulty = difficulty['proof-of-work'];
      } else {
        difficulty = difficulty['proof-of-stake'];
      }
    }
    lib.get_hashrate(function(hashrate) {
      lib.get_connectioncount(function(connections){
        db.get_stats(settings.coin, function (stats) {
          res.send({ data: [{ 
            difficulty: difficulty, 
            supply: stats.supply,
            hashrate: hashrate,
            lastPrice: stats.last_price,
            connections: connections
          }]});
        });
      });
    });
  });
});
module.exports = router;

{'fraction_non_alphanumeric': 0.1559271424305763, 'fraction_numerical': 0.003463720513586145, 'mean_word_length': 1.9978517722878626, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '297854', 'n_tokens_mistral': 5321, 'n_tokens_neox': 5193, 'n_words': 961}