#!/usr/bin/python3
# regSplit.py -

import re, os

def Split(media_item):
    if not os.path.isfile(media_item):
        raise Exception('Not a file.')
    # Make TV regex, 2 groups all chars before Season/Ep and Season/Ep
    tv_regex = re.compile(r'(.*)([Ss]\d\d[Ee]\d\d)')
    # Make Movie regex, 2 groups all chars before (year) and (year)
    # seem to need to match an additional backslash in the regex based on being
    # passed a string literal
    movie_regex = re.compile(r'(.*)(\\?\(\d{4}\\?\))')

    # First attempt to match and exract movie
    media_match = movie_regex.search(os.path.basename(media_item))

    if media_match:
        title = media_match.group(1).replace('\\', '').strip()
        year = media_match.group(2).replace('\\', '').replace('(', '').replace(')', '').strip()
        return{'type': 'movie', 'title': title, 'year': year}

    # If no valid movie_regex is found, try tv_regex
    media_match = tv_regex.search(os.path.basename(media_item))

    # If neither match, raise exception as invalid formatted
    if media_match:
        title = media_match.group(1).replace('\\', '').strip()
        season = media_match.group(2).upper().split('E')[0].replace('S','')
        episode = media_match.group(2).upper().split('E')[1]
        return{'type': 'tv', 'title': title, 'season': season, 'episode': episode}
    else:
        raise Exception('Item not formatted as a Movie or TV show')

    # TODO: return as a dict 'title', 'Year' (if movie), 'Season' (if TV),
    # 'Episode' (if TV), type ('tv' or 'movie')

@@ -63,24 +63,68 @@
 edia_item):%0A
+    # Test if the parameter is a valid file%0A
     if not o
@@ -189,16 +189,17 @@
 file.')%0A
+%0A
     # Ma
@@ -212,32 +212,33 @@
 regex, 2 groups 
+%5B
 all chars before
@@ -247,21 +247,23 @@
 eason/Ep
+%5D
  and 
+%5B
 Season/E
@@ -263,16 +263,17 @@
 eason/Ep
+%5D
 %0A    tv_
@@ -318,16 +318,17 @@
 %5Cd%5Cd)')%0A
+%0A
     # Ma
@@ -330,17 +330,17 @@
  # Make 
-M
+m
 ovie reg
@@ -352,16 +352,17 @@
  groups 
+%5B
 all char
@@ -380,19 +380,22 @@
 ear)
+%5D
  and 
+%5B
 (year)
+%5D
 %0A   
@@ -409,24 +409,35 @@
  to need to 
+optionally 
 match an add
@@ -466,16 +466,22 @@
 he regex
+%0A    #
  based o
@@ -487,22 +487,16 @@
 on being
-%0A    #
  passed 
@@ -603,16 +603,17 @@
 h and ex
+t
 ract mov
@@ -853,32 +853,108 @@
 ')', '').strip()
+%0A%0A        # Return a dict w/ 'title', 'year' (movie), type ('tv' or 'movie')
 %0A        return%7B
@@ -1123,69 +1123,8 @@
 ))%0A%0A
-    # If neither match, raise exception as invalid formatted%0A
     
@@ -1338,16 +1338,107 @@
 ('E')%5B1%5D
+%0A%0A        # Return a dict w/ 'title', 'season' (TV), 'episode' (TV), type ('tv' or 'movie')
 %0A       
@@ -1513,16 +1513,17 @@
 pisode%7D%0A
+%0A
     else
@@ -1536,188 +1536,152 @@
     
-raise Exception('Item not formatted as a Movie or TV show')%0A%0A    # TODO: return as a dict 'title', 'Year' (if movie), 'Season' (if TV),%0A    # 'Episode' (if TV), type ('tv' or 'movie
+# If neither matches throw an exception that the file is not properly formatted%0A        raise Exception('Item not formatted as a Movie or TV show
 ')%0A

Clean up comments