Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import os
- from os.path import join, getsize, exists
- import sys
- import apsw
- import re
- from xml.etree import ElementTree as ET
- import urllib
- import string
- from TvRage import TvRage
- #=========================================
- def GetSeasonEpisode(filename):
- GoOn = False
- filename = filename.upper()
- #Should catch multi episode .S01E01.S01E02 type filenames
- resp = re.search(r'(.*)\.s(\d{1,2})e(\d{1,2})\.s(\d{1,2})e(\d{1,2})',filename, re.I)
- if resp:
- showname = resp.group(1)
- GoOn = True
- else:
- # Should catch SddEdd or sddedd
- resp = re.search(r'(.*).S(\d\d?)E(\d\d?)(\.*)', filename, re.I)
- if resp:
- showname = resp.group(1)
- GoOn = True
- else:
- #check for ddxdd
- #resp = re.search(r'(.*).(\d\d?)x(\d\d?)(\.*)', filename, re.I)
- resp = re.search(r'(.*)\.(\d{1,2})x(\d{1,2})(.*)', filename, re.I)
- if resp:
- showname = resp.group(1)
- GoOn = True
- else:
- #check for Sdddd
- resp = re.search(r'(.*).S(\d\d)(.\d\d?)' , filename, re.I)
- if resp:
- showname = resp.group(1)
- GoOn = True
- else:
- # Should catch xxx
- resp = re.search(r'(.*)(\d)(.\d\d?)',filename,re.I)
- if resp:
- showname = resp.group(1)
- GoOn = True
- if GoOn:
- shownamelength = len(showname) + 1
- showname = showname.replace("."," ")
- season = resp.group(2)
- if len(season) == 1:
- season = "0" + season
- episode = resp.group(3)
- ret = [showname,season,episode]
- return True,ret
- else:
- ret = ["",-1,-1]
- return False,ret
- #=========================================
- def MakeDataBase():
- # IF the table does not exist, this will create the table.
- # Otherwise, this will be ignored due to the 'IF NOT EXISTS' clause
- sql = 'CREATE TABLE IF NOT EXISTS TvShows (pkID INTEGER PRIMARY KEY, Series TEXT, RootPath TEXT, Filename TEXT, Season TEXT, Episode TEXT, tvrageid TEXT,status TEXT);'
- cursor.execute(sql)
- #=========================================
- def WalkThePath(filepath):
- showname = ""
- # Open the error log file
- efile = open('errors.log',"w")
- for root, dirs, files in os.walk(filepath,topdown=True):
- for file in [f for f in files if f.endswith (('.avi','mkv','mp4','m4v'))]:
- # Combine path and filename to create a single variable.
- fn = join(root,file)
- OriginalFilename,ext = os.path.splitext(file)
- fl = file
- isok,data = GetSeasonEpisode(fl)
- if isok:
- showname = data[0]
- season = data[1]
- episode = data[2]
- print("Season {0} Episode {1}".format(season,episode))
- else:
- print("No Season/EPisode")
- efile.writelines('---------------------------\n')
- efile.writelines('{0} has no series/episode informaiton\n'.format(file))
- efile.writelines('---------------------------\n\n')
- #--------------------------------
- sqlquery = 'SELECT count(pkid) as rowcount from TvShows where Filename = "%s";' % fl
- try:
- for x in cursor.execute(sqlquery):
- rcntr = x[0]
- if rcntr == 0: # It's not there, so add it
- try:
- sql = 'INSERT INTO TvShows (Series,RootPath,Filename,Season,Episode,tvrageid) VALUES (?,?,?,?,?,?)'
- cursor.execute(sql,(showname,root,fl,season,episode,-1))
- except:
- print("Error")
- efile.writelines('---------------------------\n')
- efile.writelines('Error writing to database...\n')
- efile.writelines('Filename = {0}\n'.format(file))
- efile.writelines('---------------------------\n\n')
- except:
- print("Error")
- print('Series - {0} File - {1}'.format(showname,file))
- # Close the log file
- efile.close
- # End of WalkThePath
- def WalkTheDatabase():
- tr = TvRage()
- SeriesCursor = connection.cursor()
- sqlstring = "SELECT DISTINCT series FROM TvShows WHERE tvrageid = -1"
- for x in SeriesCursor.execute(sqlstring):
- seriesname = x[0]
- searchname = string.capwords(x[0]," ")
- print("Requesting information on " + searchname)
- sl = tr.FindIdByName(searchname)
- which = tr.DisplayShowResult(sl)
- if which == 0:
- print("Nothing found for %s" % seriesname)
- else:
- option = int(which)-1
- id = sl[option]['ID']
- UpdateDatabase(seriesname,id)
- GetShowStatus(seriesname,id)
- def UpdateDatabase(seriesname,id):
- idcursor = connection.cursor()
- sqlstring = 'UPDATE tvshows SET tvrageid = ' + id + ' WHERE series = "' + seriesname + '"'
- try:
- idcursor.execute(sqlstring)
- except:
- print "error"
- def GetShowStatus(seriesname,id):
- tr = TvRage()
- idcursor = connection.cursor()
- dict = tr.GetShowInfo(id)
- status = dict['Status']
- sqlstring = 'UPDATE tvshows SET status = "' + status + '" WHERE series = "' + seriesname + '"'
- try:
- idcursor.execute(sqlstring)
- except:
- print "Error"
- #=========================================
- def main():
- global connection
- global cursor
- # Create the connection and cursor.
- connection = apsw.Connection("TvShows.db3")
- cursor = connection.cursor()
- MakeDataBase()
- #=========================================
- # Set your video media paths
- #=========================================
- startfolder = ["/extramedia/tv_files","/media/freeagnt/tv_files_2"]
- #for cntr in range(0,2):
- #WalkThePath(startfolder[cntr])
- WalkTheDatabase()
- # Close the cursor and the database
- cursor.close()
- connection.close()
- print("Finished")
- #=======================================
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement