/****************************************************************************** Move a SageTV Media File to a Folder based on the show name Last Modified: 02 Jan 2012 Author: Derek Battams Modified by wayner to move to Show Name folder Use this groovy script to move a SageTV media file object from one location to another. The location is the BaseDirectory + the Show Tile some of the show title is Seinfeld and the Base Directory is \server1\TV Shows then the destination directory is \server1\TV Shows\Seinfeld\. Note that you have to use double slashes due to Groovy/Java The media file to be moved is defined by the SJQ4_ID environment variable; the SJQ4_TYPE variable must also be "MediaFile". The destination is given as the first command line argument to this script. The destination is a directory. Note that after moving all of your files you should perform a rescan in Sage. I have removed that from Slugger's original script since I wanted to run this many times before doing a rescan. ******************************************************************************/ boolean testMode = false; // Don't actually move any files, just print out what would be done // Uncomment and fill in these vars to run this script outside of SJQ //def SJQ4_METADATA = ["SJQ4_TYPE":"MediaFile", "SJQ4_ID":"74420011"]; //def SJQ4_ARGS = ["\\\\nas\\tv"]; /******************* DO NOT EDIT BELOW THIS LINE ************************/ // But if you do then send me your bug fix patches! ;) import org.apache.commons.io.FileUtils; import static groovy.io.FileType.*; import com.google.code.sagetvaddons.sjq.network.ServerClient; String type = SJQ4_METADATA.get("SJQ4_TYPE"); String id = SJQ4_METADATA.get("SJQ4_ID"); //Enter your base directory here: String BaseDirectory = "\\\\server1\\TV Shows\\"; Object mediaFile = MediaFileAPI.GetMediaFileForID(id.toInteger()); String ShowName= MediaFileAPI.GetMediaTitle(mediaFile); ShowName = (ShowName =~ /[?*\\\\/:<>|]/).replaceAll("") println("Show ShowName is: "+ShowName); File dest = new File(BaseDirectory+ShowName); println("Dest is: "+BaseDirectory+ShowName); if(!dest.isDirectory() && !dest.mkdirs()) { println("Destination directory is invalid! [" + dest.getAbsolutePath() + "]"); return 1; } if(!"MediaFile".equals(type) || id == null || !id.matches("\\d+")) { println("No media file attributes provided!"); return 1; } if(mediaFile == null) { println("No media file for id " + id); return 1; } MediaFileAPI.SetMediaFileMetadata(mediaFile, "SJQ4_ARCHIVED", null); if(MediaFileAPI.GetNumberOfSegments(mediaFile) == 0) { println("Zero file segments for media file; nothing to move!"); return 1; } println("Moving " + Arrays.toString(MediaFileAPI.GetSegmentFiles(mediaFile)) + " to destination: " + dest.getAbsolutePath()); // Copy all segments then delete the originals if more than one segment otherwise just do a filesystem move op if(MediaFileAPI.GetNumberOfSegments(mediaFile) == 1) { try { if(!testMode) FileUtils.moveFileToDirectory(MediaFileAPI.GetFileForSegment(mediaFile, 0), dest, false); else println("Would move: " + MediaFileAPI.GetFileForSegment(mediaFile, 0)); } catch(IOException e) { println("Failed to move file to destination!"); e.printStackTrace(); return 1; } } else { def copied = []; // Copy the files to their new home for(File segment : MediaFileAPI.GetSegmentFiles(MediaFileAPI.GetMediaFileForID(id.toInteger()))) { try { if(!testMode) { FileUtils.copyFileToDirectory(segment, dest, true); copied.add(new File(dest, segment.getName())); println("\tCopy of '" + segment.getAbsolutePath() + "' completed..."); } else { println("\tWould move: " + segment.getAbsolutePath()); } } catch(IOException e) { println("Error copying segment '" + segment.getAbsolutePath() + "' to destination!"); e.printStackTrace(); copied.each { if(!it.delete()) println("Failed to delete '" + it.getAbsolutePath() + "'"); } return 1; } } } println("Moving artifacts..."); mvArtifacts(id.toInteger(), dest, testMode); return 0; def mvArtifacts(int mfId, File dest, boolean testMode) { for(File segment : MediaFileAPI.GetSegmentFiles(MediaFileAPI.GetMediaFileForID(mfId))) { String prefix = segment.getName().substring(0, segment.getName().lastIndexOf('.')); File baseDir = new File(segment.getParent()); baseDir.eachFileMatch FILES, {!new File(baseDir, it.toString()).equals(segment) && it.toString().startsWith(prefix)}, { File artifact = new File(baseDir, it.getName()); if(!testMode) { FileUtils.moveFileToDirectory artifact, dest, false; } else println("Would move artifact: " + artifact.getAbsolutePath()); } } }