2013-07-05 14:15:03 +10:00
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 5 14 : 11 : 31 2013
@author : shanef
"""
2013-07-05 22:40:17 +10:00
import glob
import libtvdatasource as TVData
2013-07-05 21:30:01 +10:00
import os
import shutil
#move this to settings
TVRECORDINGSDIR = " /srv/storage2/videos/TVRecordings/ "
class EncodeData :
inputFile = ' '
show = None
outputFile = ' '
2013-07-05 22:40:17 +10:00
def ToString ( self ) :
return " Input: {0} /tOutput: {2} " . format ( self . inputFile , self . outputFile )
2013-07-05 21:30:01 +10:00
2013-07-06 20:18:52 +10:00
2013-07-05 21:30:01 +10:00
def __GetInputFilesToEncode ( shows ) :
fileList = [ ]
for show in shows :
for r , d , f in os . walk ( show . inputDirectory ) :
for files in f :
if files . endswith ( " .mpg " ) :
data = EncodeData ( )
data . show = show
data . inputFile = os . path . join ( r , files )
fileList . append ( data )
return fileList
def __FindSeason ( path , fileName , readOnly ) :
season = " Season {0} " . format ( fileName [ 1 : 3 ] )
seasonPath = os . path . join ( path , season )
if not readOnly :
if not os . path . exists ( seasonPath ) :
os . makedirs ( seasonPath )
return seasonPath
def __GetEncodeOutputFile ( showData , readOnly ) :
inFile = os . path . basename ( showData . inputFile )
outFilename = inFile [ : - 3 ] + " mkv "
outPath = __FindSeason ( showData . show . outputDirectory , outFilename )
showData . outputFile = os . path . join ( outPath , outFilename )
return showData
def GetEncodingFiles ( shows , readOnly = True ) :
showsData = __GetInputFilesToEncode ( shows )
for showData in showsData :
showsData = __GetEncodeOutputFile ( showData , readOnly )
return showsData
def CheckFileExists ( file ) :
return os . path . isfile ( file )
def __GetRecordingFile ( fileName ) :
return os . path . join ( TVRECORDINGSDIR , os . path . dirname ( fileName ) . split ( " / " ) [ - 1 ] + " .mpg " )
def PerformPostEncodeFileOperations ( inputFileName , outputFileName ) :
shutil . rmtree ( os . path . dirname ( inputFileName ) )
linkAddress = __GetRecordingFile ( inputFileName )
os . remove ( linkAddress )
2013-07-05 22:40:17 +10:00
os . symlink ( outputFileName , linkAddress )
def GetFilesToPrepare ( path , numberofFiles , shows ) :
files = glob . glob ( " {0} *.mpg " . format ( path ) )
files = sorted ( files , key = os . path . getctime )
files = filter ( lambda file : not os . path . islink ( file ) , files )
#files is now a list of unprocessed files, but contains shows other than those we are interested in
2013-07-06 20:18:52 +10:00
showsToProcess = [ ]
2013-07-05 22:40:17 +10:00
i = 0
2013-07-06 21:22:33 +10:00
print " Found {0} potential files " . format ( len ( files ) )
2013-07-05 22:40:17 +10:00
for file in files :
# TODO get these from settings
2013-07-06 20:18:52 +10:00
#if TVData.CheckTitleIsInList('localhost', 'script', 'script', 'mythconverg', file):
2013-07-06 21:10:16 +10:00
showData = TVData . RetrieveEpisodeData ( ' 192.168.0.2 ' , ' script ' , ' script ' , ' mythconverg ' , file , shows , ' 192.168.0.2 ' , ' 8081 ' , ' 3678177136222bf5002be209220ccb20 ' ) # TODO all these settings need to move to settings
2013-07-06 20:18:52 +10:00
if showData :
showsToProcess . append ( showData )
2013-07-05 22:40:17 +10:00
i = i + 1
if i == numberofFiles :
2013-07-06 20:18:52 +10:00
return showsToProcess
2013-07-05 22:40:17 +10:00
2013-07-06 20:18:52 +10:00
return showsToProcess #will reach here if there were less than numberofFiles found
2013-07-05 22:40:17 +10:00