Advertisement
gregwa

FCM 77 - Programming in Python # 47

Sep 8th, 2013
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. #=========================================    
  2. def WalkThePath(filepath):
  3.     showname = ""
  4.     # Open the error log file
  5.     efile = open('errors.log',"w")
  6.     lastroot = ''
  7.     elist = []
  8.     currentshow = ''
  9.     currentseason = ''
  10.     for root, dirs, files in os.walk(filepath,topdown=True):
  11.         for file in [f for f in files if f.endswith (('.avi','mkv','mp4','m4v'))]:
  12.             # Combine path and filename to create a single variable.
  13.             if lastroot != root:
  14.                 lastroot = root
  15.                 if len(elist) > 0:
  16.                     Missing(elist,max(elist),currentseason,currentshow)
  17.                 elist = []
  18.                 currentshow = ''
  19.                 currentseason = ''
  20.             fn = join(root,file)  
  21.             OriginalFilename,ext = os.path.splitext(file)
  22.             fl = file
  23.             isok,data = GetSeasonEpisode(fl)
  24.             if isok:
  25.                 currentshow = showname = data[0]
  26.                 currentseason = season = data[1]
  27.                 episode = data[2]
  28.                 elist.append(int(episode))
  29.             else:
  30.                 print(root,file)
  31.                 print("No Season/Episode - {0}".format(file))
  32.                 efile.writelines('---------------------------\n')
  33.                 efile.writelines('{0} has no series/episode informaiton\n'.format(file))
  34.                 efile.writelines('---------------------------\n\n')
  35.             #--------------------------------
  36.             #if root
  37.             #--------------------------------
  38.             sqlquery = 'SELECT count(pkid) as rowcount from TvShows where Filename = "%s";' % fl
  39.             try:
  40.                 for x in cursor.execute(sqlquery):
  41.                     rcntr = x[0]
  42.                 if rcntr == 0:  # It's not there, so add it
  43.                     try:
  44.                         sql = 'INSERT INTO TvShows (Series,RootPath,Filename,Season,Episode,tvrageid) VALUES (?,?,?,?,?,?)'
  45.                         cursor.execute(sql,(showname,root,fl,season,episode,-1))
  46.                     except:
  47.                         print("Error")
  48.                         print('Filename = {0}\n'.format(file))
  49.                         efile.writelines('---------------------------\n')
  50.                         efile.writelines('Error writing to database...\n')
  51.                         efile.writelines('Filename = {0}\n'.format(file))                        
  52.                         efile.writelines('---------------------------\n\n')
  53.             except:
  54.                 print("Error")
  55.                 print(root,file)
  56.         # Close the log file
  57.         efile.close
  58.     # End of WalkThePath  
  59.  
  60. #----------------------------------
  61. def Missing(eplist,shouldhave,season,showname):
  62.     #----------------------------------
  63.     # eplist is a list
  64.     # shouldhave is an integer
  65.     # season is an integer
  66.     # showname is a string
  67.     #----------------------------------
  68.     temp = set(range(1,shouldhave+1))
  69.     ret = list(temp-set(eplist))
  70.     if len(ret) > 0:
  71.         print('Missing Episodes for {0} Season {1} - {2}'.format(showname,season,ret))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement