'use strict';

var request = require('request');
var cheerio = require('cheerio');

module.exports = {
  search: search
};

function search(options, callback) {
  if (!options || (!options.query && !options.category)) {
    throw 'Kickass.search needs options.';
  }
  var url;
  if (!options.query) {
    url = encodeURI('https://kickass.to/' + options.category + '/');
  } else {
    url = encodeURI('https://kickass.to/usearch/' + options.query + ' category:' + options.category + '/');
  }
  request({
    uri: url,
    gzip: true
  }, onKickstartLoaded);

  function onKickstartLoaded(err, res, body) {
    if (err || res.statusCode !== 200) {
      callback(err || new Error('Status code was not 200'));
      return;
    }
    var $ = cheerio.load(body);
    var results = [];
    $('tr:nth-of-type(n+2) td:nth-of-type(1)').each(function (i, element) {
      var $element = $(element);
      results.push({
        name: $element.find('a.cellMainLink').text(),
        magnet: $element.find('a.imagnet').attr('href')
      });
    });
    callback(null, results || []);
  }
}

@@ -324,26 +324,30 @@
 https://
-kickass.to
+dx-torrent.com
 /' + opt
@@ -412,18 +412,22 @@
 s://
-kickass.to
+dx-torrent.com
 /use
@@ -985,16 +985,26 @@
 magnet: 
+getMagnet(
 $element
@@ -1014,31 +1014,49 @@
 nd('
-a.imagnet').attr('href'
+%5Bdata-sc-params%5D').attr('data-sc-params')
 )%0A  
@@ -1109,11 +1109,233 @@
 %5B%5D);%0A  %7D
+%0A%0A  function getMagnet(params) %7B%0A    if (!params) return '';%0A    try %7B%0A      var json = JSON.parse(params.replace(new RegExp('%5C'', 'g'), '%22'));%0A      return json.magnet;%0A    %7D catch (exception) %7B%0A      return '';%0A    %7D%0A  %7D
 %0A%7D%0A

use kickass replacement