Browse Source

Try to fix #81 and probably some other issues when the URL is not valid to properly display that the URL is not valid. Also change a bit on how error are handled

Godzil 5 years ago
parent
commit
4990effa1c
3 changed files with 72 additions and 17 deletions
  1. 48 8
      src/batch.ts
  2. 3 3
      src/my_request.ts
  3. 21 6
      src/series.ts

+ 48 - 8
src/batch.ts

@@ -62,6 +62,11 @@ export default function(args: string[], done: (err?: Error) => void)
         return done(err);
     }
 
+    if (tasksArr[0].address === '')
+    {
+        return done();
+    }
+
     let i = 0;
 
     (function next()
@@ -80,12 +85,18 @@ export default function(args: string[], done: (err?: Error) => void)
       {
         if (errin)
         {
+          if (errin.error)
+          {
+            /* Error from the request, so ignore it */
+            tasksArr[i].retry = 0;
+          }
+
           if (tasksArr[i].retry <= 0)
           {
-            log.error(errin.stack || errin);
+            log.error(JSON.stringify(errin));
             if (config.debug)
             {
-              log.dumpToDebug('BatchGiveUp', errin.stack || errin);
+              log.dumpToDebug('BatchGiveUp', JSON.stringify(errin));
             }
             log.error('Cannot get episodes from "' + tasksArr[i].address + '", please rerun later');
             /* Go to the next on the list */
@@ -95,11 +106,11 @@ export default function(args: string[], done: (err?: Error) => void)
           {
             if (config.verbose)
             {
-              log.error(errin);
+              log.error(JSON.stringify(errin));
             }
             if (config.debug)
             {
-              log.dumpToDebug('BatchRetry', errin.stack || errin);
+              log.dumpToDebug('BatchRetry', JSON.stringify(errin));
             }
             log.warn('Retrying to fetch episodes list from' + tasksArr[i].retry + ' / ' + config.retry);
             tasksArr[i].retry -= 1;
@@ -195,6 +206,27 @@ function get_max_filter(filter: string): number
   return +Infinity;
 }
 
+/**
+ * Check that URL start with http:// or https://
+ * As for some reason request just return an error but a useless one when that happen so check it
+ * soon enough.
+ */
+function checkURL(address: string): boolean
+{
+  if (address.startsWith('http:\/\/'))
+  {
+    return true;
+  }
+  if (address.startsWith('http:\/\/'))
+  {
+    return true;
+  }
+
+  log.error('URL ' + address + ' miss \'http:\/\/\' or \'https:\/\/\' => will be ignored');
+
+  return false;
+}
+
 /**
  * Parses the configuration or reads the batch-mode file for tasks.
  */
@@ -204,8 +236,13 @@ function tasks(config: IConfigLine, batchPath: string, done: (err: Error, tasks?
   {
     return done(null, config.args.map((addressIn) =>
     {
-      return {address: addressIn, retry: config.retry,
-              episode_min: get_min_filter(config.episodes), episode_max: get_max_filter(config.episodes)};
+      if (checkURL(addressIn))
+      {
+        return {address: addressIn, retry: config.retry,
+                episode_min: get_min_filter(config.episodes), episode_max: get_max_filter(config.episodes)};
+      }
+
+      return {address: '', retry: 0, episode_min: 0, episode_max: 0};
     }));
   }
 
@@ -241,8 +278,11 @@ function tasks(config: IConfigLine, batchPath: string, done: (err: Error, tasks?
             return;
           }
 
-          map.push({address: addressIn, retry: lineConfig.retry,
-                    episode_min: get_min_filter(lineConfig.episodes), episode_max: get_max_filter(lineConfig.episodes)});
+          if (checkURL(addressIn))
+          {
+            map.push({address: addressIn, retry: lineConfig.retry,
+                      episode_min: get_min_filter(lineConfig.episodes), episode_max: get_max_filter(lineConfig.episodes)});
+          }
         });
       });
       done(null, map);

+ 3 - 3
src/my_request.ts

@@ -82,7 +82,7 @@ function login(sessionId: string, user: string, pass: string): Promise<any>
 /**
  * Performs a GET request for the resource.
  */
-export function get(config: IConfig, options: string|request.Options, done: (err: Error, result?: string) => void)
+export function get(config: IConfig, options: string|request.Options, done: (err: any, result?: string) => void)
 {
   authenticate(config, (err) =>
   {
@@ -91,7 +91,7 @@ export function get(config: IConfig, options: string|request.Options, done: (err
       return done(err);
     }
 
-    cloudscraper.request(modify(options, 'GET'), (error: Error, response: any, body: any) =>
+    cloudscraper.request(modify(options, 'GET'), (error: any, response: any, body: any) =>
     {
       if (error) return done(error);
       done(null, typeof body === 'string' ? body : String(body));
@@ -223,4 +223,4 @@ function modify(options: string|request.Options, reqMethod: string): request.Opt
     url: options.toString(),
     method: reqMethod
   };
-}
+}

+ 21 - 6
src/series.ts

@@ -27,7 +27,7 @@ function fileExist(path: string)
 /**
  * Streams the series to disk.
  */
-export default function(config: IConfig, task: IConfigTask, done: (err: Error) => void)
+export default function(config: IConfig, task: IConfigTask, done: (err: any) => void)
 {
   const persistentPath = path.join(config.output || process.cwd(), persistent);
 
@@ -45,6 +45,12 @@ export default function(config: IConfig, task: IConfigTask, done: (err: Error) =
     {
       if (errP)
       {
+        const reqErr = errP.error;
+        if ((reqErr.syscall === 'getaddrinfo') && (reqErr.errno === 'ENOTFOUND'))
+        {
+          log.error('The URL \'' + task.address + '\' is invalid, please check => I\'m ignoring it.');
+        }
+
         return done(errP);
       }
 
@@ -61,9 +67,17 @@ export default function(config: IConfig, task: IConfigTask, done: (err: Error) =
         {
           if (errD)
           {
+            /* Check if domain is valid */
+            const reqErr = errD.error;
+            if ((reqErr.syscall === 'getaddrinfo') && (reqErr.errno === 'ENOTFOUND'))
+            {
+               page.episodes[i].retry = 0;
+               log.error('The URL \'' + task.address + '\' is invalid, please check => I\'m ignoring it.');
+            }
+
             if (page.episodes[i].retry <= 0)
             {
-              log.error(errD);
+              log.error(JSON.stringify(errD));
               log.error('Cannot fetch episode "s' + page.episodes[i].volume + 'e' + page.episodes[i].episode +
                             '", please rerun later');
               /* Go to the next on the list */
@@ -76,7 +90,7 @@ export default function(config: IConfig, task: IConfigTask, done: (err: Error) =
                 if (config.debug)
                 {
                   log.dumpToDebug('series address', task.address);
-                  log.dumpToDebug('series error', errD.stack || errD);
+                  log.dumpToDebug('series error', JSON.stringify(errD));
                   log.dumpToDebug('series data', JSON.stringify(page));
                 }
                 log.error(errD);
@@ -120,7 +134,7 @@ export default function(config: IConfig, task: IConfigTask, done: (err: Error) =
  */
 function download(cache: {[address: string]: number}, config: IConfig,
                   task: IConfigTask, item: ISeriesEpisode,
-                  done: (err: Error, ign: boolean) => void)
+                  done: (err: any, ign: boolean) => void)
 {
   const episodeNumber = parseInt(item.episode, 10);
 
@@ -152,7 +166,7 @@ function download(cache: {[address: string]: number}, config: IConfig,
 /**
  * Requests the page and scrapes the episodes and series.
  */
-function pageScrape(config: IConfig, task: IConfigTask, done: (err: Error, result?: ISeries) => void)
+function pageScrape(config: IConfig, task: IConfigTask, done: (err: any, result?: ISeries) => void)
 {
   if (task.address[0] === '@')
   {
@@ -170,7 +184,8 @@ function pageScrape(config: IConfig, task: IConfigTask, done: (err: Error, resul
   {
     let episodeCount = 0;
     my_request.get(config, task.address, (err, result) => {
-      if (err) {
+      if (err)
+      {
         return done(err);
       }