Advertisement
gregwa

FCM 71 - tvfilesearch.py

Mar 10th, 2013
440
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. from os.path import join, getsize, exists
  3. import sys
  4. import apsw
  5. import re
  6.  
  7. #=========================================
  8. def GetSeasonEpisode(filename):
  9.     filename = filename.upper()
  10.     resp = re.search(r'(.*).S\d\dE\d\d(\.*)', filename, re.M|re.I)  
  11.     if resp:
  12.         showname = resp.group(1)
  13.         shownamelength = len(showname) + 1
  14.         se = filename[shownamelength:shownamelength+6]
  15.         season = se[1:3]
  16.         episode = se[4:6]
  17.         showname = showname.replace("."," ")
  18.         ret = [showname,season,episode]
  19.         return True,ret
  20.     else:
  21.         ret = ["",-1,-1]
  22.         return False,ret
  23.  
  24. #=========================================
  25. def MakeDataBase():
  26.     # IF the table does not exist, this will create the table.  
  27.     # Otherwise, this will be ignored due to the 'IF NOT EXISTS' clause
  28.     sql = 'CREATE TABLE IF NOT EXISTS TvShows (pkID INTEGER PRIMARY KEY, Series TEXT, RootPath TEXT, Filename TEXT, Season TEXT, EPISODE TEXT);'
  29.     cursor.execute(sql)
  30.  
  31. #=========================================    
  32. def WalkThePath(filepath):
  33.  
  34.     showname = ""
  35.     # Open the error log file
  36.     efile = open('errors.log',"w")
  37.     for root, dirs, files in os.walk(filepath,topdown=True):
  38.         for file in [f for f in files if f.endswith (('.avi','mkv','mp4','m4v'))]:
  39.             # Combine path and filename to create a single variable.
  40.             fn = join(root,file)  
  41.             OriginalFilename,ext = os.path.splitext(file)
  42.             fl = file
  43.             isok,data = GetSeasonEpisode(fl)
  44.             if isok:
  45.                 showname = data[0]
  46.                 season = data[1]
  47.                 episode = data[2]
  48.                 print("Season {0} Episode {1}".format(season,episode))
  49.             else:
  50.                 print("No Season/EPisode")
  51.                 efile.writelines('---------------------------\n')
  52.                 efile.writelines('{0} has no series/episode informaiton\n'.format(file))
  53.                 efile.writelines('---------------------------\n\n')
  54.            
  55.             sqlquery = 'SELECT count(pkid) as rowcount from TvShows where Filename = "%s";' % fl
  56.             print(sqlquery)
  57.             try:
  58.                 for x in cursor.execute(sqlquery):
  59.                     rcntr = x[0]
  60.                 if rcntr == 0:  # It's not there, so add it
  61.                     try:
  62.                         sql = 'INSERT INTO TvShows (Series,RootPath,Filename,Season,Episode) VALUES (?,?,?,?,?)'
  63.                         cursor.execute(sql,(showname,root,fl,season,episode))
  64.                     except:
  65.                         print("Error")
  66.                         efile.writelines('---------------------------\n')
  67.                         efile.writelines('Error writing to database...\n')
  68.                         efile.writelines('Filename = {0}\n'.format(file))
  69.                         efile.writelines('---------------------------\n\n')
  70.             except:
  71.                 print("Error")
  72.             print('Series - {0} File - {1}'.format(showname,file))
  73.  
  74.         # Close the log file
  75.         efile.close
  76.     # End of WalkThePath  
  77.    
  78. #=========================================    
  79. def main():
  80.     global connection
  81.     global cursor  
  82.  
  83.     # Create the connection and cursor.
  84.     connection = apsw.Connection("TvShows.db3")
  85.     cursor = connection.cursor()
  86.     MakeDataBase()
  87.     #=========================================
  88.     # Set your video media paths
  89.     #=========================================
  90.     startfolder = ["/extramedia/tv_files","/media/freeagnt/tv_files_2"]
  91.     for cntr in range(0,2):
  92.         WalkThePath(startfolder[cntr])    
  93.     # Close the cursor and the database
  94.     cursor.close()
  95.     connection.close()
  96.  
  97.     print("Finished")
  98.  
  99. #=======================================
  100. if __name__ == '__main__':
  101.     main()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement