/* Media File Scanner/Task Queuer Last Modified: 15 Feb 2011 Author: Derek Battams Use this script to periodically scan your media objects and check to see if any need to have tasks queued on them. Basically, set up your media mask and then modify the needsProcessing() function to do the checks you want against each object. If needsProcessing() returns true for an object then it will queue up each task listed in the taskIds list for that object. Typically, you would run this script periodically via the SJQv4 crontab. PLEASE RUN THIS SCRIPT WITH testMode = true BEFORE ALLOWING IT TO ACTUALLY QUEUE UP TASKS!! */ /***** CONFIGURE BELOW *****/ def testMode = false // If true, only print out which media files would be queued up, don't actually add the tasks to the queue def mediaMask = "T" // What types of media should be scanned? (T = TV, M = Music, V = Imported Video, D = DVD, B = BluRay, P = Pictures; TMV = TV + Music + Imports, etc.) def taskIds = ["COMSKIP"] // Multiple tasks can be listed, separated by commas /* Returns true if the argument needs to be queued or false if it should be skipped Modify this function to determine which media files get queued up and which don't */ def needsProcessing(Object mediaFile) { // This function could be written in a much more condensed manner, but I'm breaking it up for the sake of readability // So let's skip queuing this media file if not on the NAS server // String filePath = MediaFileAPI.GetFileForSegment(mediaFile, 0).getAbsolutePath(); // filePath = filePath.toLowerCase(); // println "$filePath"; //String FilePath = SJQ4_METADATA.get("SJQ4_PATH"); //if ( (FilePath.toUpperCase()) =~ "\\\\TOWER" ) { // return true //} // So let's skip queuing this media file if it's live tv or an IR recording if(AiringAPI.IsNotManualOrFavorite(mediaFile)) return false // Personally, I don't comskip until the recording is done, so don't queue up recordings in progress if(MediaFileAPI.IsFileCurrentlyRecording(mediaFile)) return false // Let's also skip it if it's from a channel known not to have commercials (adjust the regex accordingly) if(AiringAPI.GetAiringChannelName(mediaFile) =~ /HBO.*|ST.*|ENC.*|SHO.*|MAX.*|TMC.*|MOMAX|TCM|WNSC|WUNC|WUNG/) return false // Let's also skip it if there is already an edl file for the media file; adjust the extensions, if necessary if(hasArtifacts(mediaFile, ["edl"])) // This function is defined at the bottom of the file return false // All our tests have passed so return true return true } /***** END CONFIG BLOCK *****/ /***** DO NOT MODIFY BELOW THIS LINE *****/ import com.google.code.sagetvaddons.sjq.network.ServerClient import com.google.code.sagetvaddons.metadata.Factory import org.apache.commons.io.FilenameUtils def sc = !testMode ? new ServerClient() : null MediaFileAPI.GetMediaFiles(mediaMask).each { mf -> if(needsProcessing(mf)) { if(!testMode) taskIds.each { id -> sc.addTask(id, Factory.getMap(mf)) println "'${MediaFileAPI.GetMediaTitle(mf)}' (${MediaFileAPI.GetMediaFileID(mf)}); queued for Comskip" } else println "Would queue up '${MediaFileAPI.GetMediaTitle(mf)}' (${MediaFileAPI.GetMediaFileID(mf)}); skipped because test mode is TRUE" } } if(sc != null) sc.close() return 0 // Returns true if any segment of the given media file has at least one artifact of any of the given artifact extensions; false otherwise def hasArtifacts(Object mf, List exts) { for(def it : MediaFileAPI.GetSegmentFiles(mf)) { def absPath = it.getAbsolutePath() def dir = FilenameUtils.getFullPath(absPath) def base = FilenameUtils.getBaseName(absPath) for(def ext : exts) { def artifact = "${dir}${base}.$ext" if(Utility.IsFilePath(artifact)) return true } } return false }