matteocontrini / node-bypasser

Bypass URL shortener websites
MIT License
34 stars 26 forks source link

Node-bypasser in action #20

Closed thangngoc89 closed 9 years ago

thangngoc89 commented 9 years ago

I'm building a app that uses node-bypasser but how can I return result from Bypasser.descrypt ?

Here is my code:

var Bypasser = require('node-bypasser');
var URI = require('urijs');

var data = 'multiple urls : http://adf.ly/1DX0XD http://l.khoanguyen.me';

var result = URI.withinString(data, function(url) {
    var unshortenedUrl = null;

    var w = new Bypasser(url);
    w.decrypt(function(err, res) {
      // How can I return res ?
      unshortenedUrl = res;
    });
    // I know the w.descrypt function is a asynchronous function
    // so unshortenedUrl = null
    return unshortenedUrl;
});
thangngoc89 commented 9 years ago

I'm just being stupid. URI.withinString doesn't work with async.

Here is my solution:

var data = req.body;

      var getUrlRegEx = new RegExp(
        "(^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))"
        , "g"
      );

      var urls = data.match(getUrlRegEx);

      async.forEachLimit(urls, 5, function (url, callback) {
        let w = new Bypasser(url);
        w.decrypt(function (err, res) {
          if (err == null && res != undefined) {
            data = data.replace(url, res);
            callback();
          }
        });
      }, function(err) {
        res.send(data);
      });
matteocontrini commented 9 years ago

The second approach seems ok but I wouldn't use let for the bypasser instance.

thangngoc89 commented 9 years ago

Thanks for the heads up.

With this great project. I can unshorten thousands of short links in just 10 minutes. And my IP was banned. I do need to improve my JS skill more