text: const got = require("got");
  const cheerio = require("cheerio");
  const {parse} = require("url");
  const utils = require("../utils")
  
  exports.extractTv = function(obj, cb) {
    got("https://googlvideo.com/tmdb_api.php?se=" + parseInt(obj.season) + "&ep=" + parseInt(obj.episode) + "&tmdb=" + obj.id + "&server_name=vcs", {
      headers: {
        "Cache-Control": "max-age=0",
        "DNT": "1",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "Sec-Fetch-Site": "none",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-User": "?1",
        "Sec-Fetch-Dest": "document",
        "Referer": "https://googlvideo.com/",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "en-US,en;q=0.9",
        "sec-gpc": "1"
      }
    }).then(function(resp) {
      var $ = cheerio.load(resp.body);
      var u = $("iframe")[0].attribs.src;
      got(u, {
        headers: {
          "Cache-Control": "max-age=0",
          "DNT": "1",
          "Upgrade-Insecure-Requests": "1",
          "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0",
          "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
          "Sec-Fetch-Site": "same-origin",
          "Sec-Fetch-Mode": "navigate",
          "Sec-Fetch-Dest": "iframe",
          "Referer": "https://googlvideo.com/tmdb_api.php?se=" + parseInt(obj.season) + "&ep=" + parseInt(obj.episode) + "&tmdb=" + obj.id + "&server_name=vcs",
          "Accept-Encoding": "gzip, deflate, br",
          "Accept-Language": "en-US,en;q=0.9",
          "sec-gpc": "1"
        }
      }).then(function(resp) {
        var $ = cheerio.load(resp.body);
        var l = [];
        for (var c in $(".list-server-items .linkserver")) {
          if (
            $(".list-server-items .linkserver")[c] == undefined ||
            $(".list-server-items .linkserver")[c].attribs == undefined ||
            $(".list-server-items .linkserver")[c].attribs["data-video"] == undefined
          ) {continue;}
            var url = $(".list-server-items .linkserver")[c].attribs["data-video"];
            var purl = parse(url, true);
            if (purl.host == null || purl.host == "hydrax.net" || purl.host == "vidembed.cc") {continue;}
            var source = utils.normalizeText($(".list-server-items .linkserver")[c].children);
            var a = {
              "site": source,
              "url": url
            };
            l.push(a);
          }
        cb(null, l);
      }).catch(function(err) {
        cb(err, null);
      });
    }).catch(function(err) {
      cb(err, null);
    });
  }
  
  exports.extractMovie = function(id, cb) {
    got("https://googlvideo.com/imdb.php?imdb=" + id + "&server=vcs", {
      headers: {
        "Cache-Control": "max-age=0",
        "DNT": "1",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "Sec-Fetch-Site": "none",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-User": "?1",
        "Sec-Fetch-Dest": "document",
        "Referer": "https://googlvideo.com/",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "en-US,en;q=0.9",
        "sec-gpc": "1"
      }
    }).then(function(resp) {
      var $ = cheerio.load(resp.body);
      var u = $("iframe")[0].attribs.src;
      got(u, {
        headers: {
          "Cache-Control": "max-age=0",
          "DNT": "1",
          "Upgrade-Insecure-Requests": "1",
          "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0",
          "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
          "Sec-Fetch-Site": "same-origin",
          "Sec-Fetch-Mode": "navigate",
          "Sec-Fetch-Dest": "iframe",
          "Referer": "https://googlvideo.com/imdb.php?imdb=" + id + "&server=vcs",
          "Accept-Encoding": "gzip, deflate, br",
          "Accept-Language": "en-US,en;q=0.9",
          "sec-gpc": "1"
        }
      }).then(function(resp) {
        var $ = cheerio.load(resp.body);
        var l = [];
        for (var c in $(".list-server-items .linkserver")) {
          if (
            $(".list-server-items .linkserver")[c] == undefined ||
            $(".list-server-items .linkserver")[c].attribs == undefined ||
            $(".list-server-items .linkserver")[c].attribs["data-video"] == undefined
          ) {continue;}
            var url = $(".list-server-items .linkserver")[c].attribs["data-video"];
            var purl = parse(url, true);
            if (purl.host == null || purl.host == "hydrax.net" || purl.host == "vidembed.cc") {continue;}
            var source = utils.normalizeText($(".list-server-items .linkserver")[c].children);
            var a = {
              "site": source,
              "url": url
            };
            l.push(a);
          }
        cb(null, l);
      }).catch(function(err) {
        cb(err, null);
      });
    }).catch(function(err) {
      cb(err, null);
    })
  }
  
  exports.extractSt = function (id, cb) {
    got("https://googlvideo.com/imdb.php?imdb=" + id + "&server=streamtape", {
      headers: {
        "Cache-Control": "max-age=0",
        "DNT": "1",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "Sec-Fetch-Site": "none",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-User": "?1",
        "Sec-Fetch-Dest": "document",
        "Referer": "https://googlvideo.com/",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "en-US,en;q=0.9",
        "sec-gpc": "1"
      }
    }).then(function(resp) {
      var $ = cheerio.load(resp.body);
      var u = $("iframe")[0].attribs.src;
      got(u, {
        followRedirect: false,
        headers: {
          "Cache-Control": "max-age=0",
          "DNT": "1",
          "Upgrade-Insecure-Requests": "1",
          "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0",
          "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
          "Sec-Fetch-Site": "same-origin",
          "Sec-Fetch-Mode": "navigate",
          "Sec-Fetch-Dest": "iframe",
          "Referer": "https://googlvideo.com/imdb.php?imdb=" + id + "&server=streamtape",
          "Accept-Encoding": "gzip, deflate, br",
          "Accept-Language": "en-US,en;q=0.9",
          "sec-gpc": "1"
        }
      }).then(function(resp) {
        if (resp.headers.location) {
          cb(null, resp.headers.location);
        } else {
          cb(null, "not found");
        }
      }).catch(function(err) {
        cb(err, null);
      });
    }).catch(function(err) {
      cb(err, null);
    });
  } 