#!/usr/bin/python3
# media_namer.py - Renames passed media files in a folder (and subfolders) using
#                   OMDB for movies and theTVDB for TV shows


import os
import sys
import logging
from distutils.util import strtobool

import tqdm

import videoLister
import scrapeOMDB
import scrapeTVDB
import regSplit

logger = logging.getLogger(__name__)
logging.basicConfig(filename='media_namer.log',level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

in_path = ' '.join(sys.argv[1:])

JWT = scrapeTVDB.auth()

for item in tqdm.tqdm(videoLister.videoDir(in_path)):
    logger.info(("Working on: " + os.path.abspath(item)))
    reg_dict = regSplit.Split(item)
    ext = os.path.splitext(item)[1]

    if reg_dict['type'] == 'tv':
        med_info = scrapeTVDB.theTVDB(reg_dict['title'], reg_dict['season'],
                                    reg_dict['episode'], JWT)

        new_name = (reg_dict['title'] + ' S' + reg_dict['season'].zfill(2) +
                        'E' + reg_dict['episode'].zfill(2) + ' - ' +
                        med_info['data'][0]['episodeName'] + ext)

        logger.info("Renaming: %s, as: %s" % (item, new_name))
        os.rename(item, (os.path.join(os.path.dirname(item), new_name)))

    else:
        logger.warning(("File not renamed: "+ os.path.abspath(item)))

    if reg_dict['type'] == 'movie':
        med_info = scrapeOMDB.OMDBmovie(reg_dict['title'], reg_dict['year'])

        if strtobool(med_info['Response']):
            new_name = (reg_dict['title'] + ' (' + med_info['Year'] + ')' + ext)
            logger.info("Renaming: %s, as: %s" % (item, new_name))
            os.rename(item, (os.path.join(os.path.dirname(item), new_name)))
        else:
            logger.warning(("File not renamed: "+ os.path.abspath(item)))

@@ -159,19 +159,8 @@
 ws%0A%0A
-%0Aimport os%0A
 impo
@@ -184,45 +184,8 @@
 ging
-%0Afrom distutils.util import strtobool
 %0A%0Aim
@@ -227,18 +227,18 @@
 t scrape
-OM
+TV
 DB%0Aimpor
@@ -243,36 +243,38 @@
 ort 
-scrapeTVDB%0Aimport regSplit%0A%0A
+media_rename%0A%0Adef main():%0A    
 logg
@@ -306,16 +306,20 @@
 name__)%0A
+    
 logging.
@@ -398,16 +398,20 @@
         
+    
 format='
@@ -465,17 +465,20 @@
 age)s')%0A
-%0A
+    
 in_path 
@@ -502,17 +502,20 @@
 gv%5B1:%5D)%0A
-%0A
+    
 JWT = sc
@@ -531,16 +531,20 @@
 auth()%0A%0A
+    
 for item
@@ -597,1206 +597,79 @@
     
-logger.info((%22Working on: %22 + os.path.abspath(item)))%0A    reg_dict = regSplit.Split(item)%0A    ext = os.path.splitext(item)%5B1%5D%0A%0A    if reg_dict%5B'type'%5D == 'tv':%0A        med_info = scrapeTVDB.theTVDB(reg_dict%5B'title'%5D, reg_dict%5B'season'%5D,%0A                                    reg_dict%5B'episode'%5D, JWT)%0A%0A        new_name = (reg_dict%5B'title'%5D + ' S' + reg_dict%5B'season'%5D.zfill(2) +%0A                        'E' + reg_dict%5B'episode'%5D.zfill(2) + ' - ' +%0A                        med_info%5B'data'%5D%5B0%5D%5B'episodeName'%5D + ext)%0A%0A        logger.info(%22Renaming: %25s, as: %25s%22 %25 (item, new_name))%0A        os.rename(item, (os.path.join(os.path.dirname(item), new_name)))%0A%0A    else:%0A        logger.warning((%22File not renamed: %22+ os.path.abspath(item)))%0A%0A    if reg_dict%5B'type'%5D == 'movie':%0A        med_info = scrapeOMDB.OMDBmovie(reg_dict%5B'title'%5D, reg_dict%5B'year'%5D)%0A%0A        if strtobool(med_info%5B'Response'%5D):%0A            new_name = (reg_dict%5B'title'%5D + ' (' + med_info%5B'Year'%5D + ')' + ext)%0A            logger.info(%22Renaming: %25s, as: %25s%22 %25 (item, new_name))%0A            os.rename(item, (os.path.join(os.path.dirname(item), new_name)))%0A        else:%0A            logger.warning((%22File not renamed: %22+ os.path.abspath(item))
+    media_rename.rename(item, JWT)%0A%0A%0Aif __name__ == '__main__':%0A    main(
 )%0A

refactor rename function