const cheerio = require("cheerio");
const fetch = require("node-fetch");

async function getUrl(url) {
  try {
    const response = await fetch(url, {
      headers: {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml'
      }
    });
    const body = await response.text();

    if (body) {
      const $ = cheerio.load(body);
      if ($("head").find('link[type="application/rss+xml"]').length > 0) {
        autoDiscoverUrl = $('head link[type="application/rss+xml"]').attr(
          "href"
        );
        url = autoDiscoverUrl;
      }
    }
    return url;
  } catch (err) {
    console.log(err);
  }
}

exports.discover = async function(url) {
  try {
    const autodiscover = await getUrl(url);
    return autodiscover;
  } catch (err) {
    console.log(err);
  }
};

@@ -415,16 +415,100 @@
 ext();%0A%0A
+    //TODO: fix situation where autoDiscoverUrl is not a valid url, but a directory%0A
     if (
@@ -511,24 +511,24 @@
 if (body) %7B%0A
-
       const 
@@ -764,24 +764,243 @@
 rl;%0A      %7D%0A
+      if ($(%22head%22).find('link%5Btype=%22application/atom+xml%22%5D').length %3E 0) %7B%0A        autoDiscoverUrl = $('head link%5Btype=%22application/atom+xml%22%5D').attr(%0A          %22href%22%0A        );%0A        url = autoDiscoverUrl;%0A      %7D%0A
     %7D%0A    re
@@ -1089,16 +1089,17 @@
 function
+ 
 (url) %7B%0A
@@ -1223,7 +1223,6 @@
   %7D%0A
+
 %7D;
-%0A

add resolving application/atom+xml urls