From af1525a2c35402ab13ef4544ff21ecf24eddf4da Mon Sep 17 00:00:00 2001
From: hygienic-books <hygienic-books@tentic.net>
Date: Wed, 23 Mar 2022 23:42:22 +0100
Subject: [PATCH] Handle malformed source file names

---
 kodi-nfo-feeder.py | 39 ++++++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 13 deletions(-)

diff --git a/kodi-nfo-feeder.py b/kodi-nfo-feeder.py
index a0274dd..46b4952 100644
--- a/kodi-nfo-feeder.py
+++ b/kodi-nfo-feeder.py
@@ -17,6 +17,8 @@ import requests
 from requests.auth import HTTPBasicAuth
 
 
+# TODO Process files that already exist on start
+
 # Exit codes
 # 1: Config file invalid, it has no sections
 # 2: Config file invalid, sections must define at least CONST.CFG_MANDATORY
@@ -232,20 +234,28 @@ def get_season_and_episode(
     ext = file_name_ext_split[1]
     season_episode = re.split("[S|E]", season_ep_str[0])
     season = f"Season {season_episode[1]}"
-    title = season_ep_str[1]
-    basic_cleaned_title = get_basic_cleaned_title(csection_name, config_obj, title)
+    try:
+        title = season_ep_str[1]
+    except IndexError:
+        log.warning(f"File name '{raw_file_name}' is not following expected format. The excepted format "
+                    f"is a season-and-episode string followed by space-slash-space and and arbitrary sequence "
+                    f"of characters suffixed with a file extension e.g. "
+                    f"'S2022E2022032001 - This is a Title.mp4'. Skipping further file processing ...")
+        return {}
+    else:
+        basic_cleaned_title = get_basic_cleaned_title(csection_name, config_obj, title)
 
-    got_season_and_episode = {
-        "season_str": season,
-        "title_str": basic_cleaned_title,
-        "season_ep_list": season_ep_str,
-        "ext": ext
-    }
-    log.debug(f"""Identified {got_season_and_episode["season_str"]}, """
-              f"""title "{got_season_and_episode["title_str"]}" """ 
-              f"and episode object {season_ep_str} "
-              f"with extension '{ext}'.")
-    return got_season_and_episode
+        got_season_and_episode = {
+            "season_str": season,
+            "title_str": basic_cleaned_title,
+            "season_ep_list": season_ep_str,
+            "ext": ext
+        }
+        log.debug(f"""Identified {got_season_and_episode["season_str"]}, """
+                  f"""title "{got_season_and_episode["title_str"]}" """ 
+                  f"and episode object {season_ep_str} "
+                  f"with extension '{ext}'.")
+        return got_season_and_episode
 
 
 def get_target_file_list(
@@ -422,6 +432,9 @@ if __name__ == '__main__':
                          f"""'{watch_dir}', processing ...""")
 
                 season_and_episode = get_season_and_episode(section_name, config, file_name)
+                if not season_and_episode:
+                    break
+
                 if config.getboolean(section_name, "do_seasons"):
                     season_str = season_and_episode["season_str"]
                     log.debug(f"Changing output to season-specific dir '{season_str}' ...")