Advertisement
Guest User

Eyetv Rename

a guest
Sep 11th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- function to get the correct extension for a preset
  2. to getPresetExtension(preset)
  3.     tell application "EyeTV"
  4.         -- if you want to change the extension for a preset, make sure to use the exact same names as in the allowedPresets list below
  5.         if preset is in {QuickTime, eMail, Web, HDV720p, HDV1080i} then
  6.             return ".mov"
  7.         else if preset is in {MPEGPS, «constant EtypTost», «constant EtypVCD », «constant EtypSVCD», «constant EtypDVD », «constant EtypTost», "SymLink"} then
  8.             return ".mpg"
  9.         else if preset is in {MPEGES, DVDStudioPro} then
  10.             return ".mpv"
  11.         else if preset is in {AVI, DivX, «constant EtypDvxd»} then
  12.             return ".avi"
  13.         else if preset is in {ThreeG} then
  14.             return ".3gp"
  15.         else if preset is in {iPod, AppleTV} then
  16.             return ".m4v"
  17.         else
  18.             -- all other presets default to .mp4
  19.             return ".mp4"
  20.         end if
  21.     end tell
  22. end getPresetExtension
  23.  
  24. -- main script
  25. on run
  26.     tell application "EyeTV" to activate
  27.    
  28.     -- Presets you can use (if you want to modify the extension used with a preset, edit the getPresetExtension function above):
  29.     tell application "EyeTV" to set allowedPresets to {QuickTime, «constant EtypVCD », «constant EtypSVCD», «constant EtypDVD », «constant EtypDvxd», «constant EtypTost», eMail, Web, iMovie, iMovieHD, iDVD, DVDStudioPro, «constant EtypTost», MPEGPS, MPEGES, DV, DV169, Movie, AVI, MPEG4, ThreeG, DivX, HDV720p, HDV1080i, iTunes, AppleLossless, PSP, PSPH264, iPod, iPodMP4, AppleTV, iPhone, Turbo, «constant EtypBDMV», iPad, HD720p, HD1080p, "SymLink"}
  30.    
  31.     -- start modifying settings here ---------------------------------------------------------
  32.    
  33.     -- set to false to test folder creation only
  34.     set exportingEnabled to true
  35.     --set whether error dialogs should be shown
  36.     set silentErrors to true
  37.    
  38.     -- set to a path like "Macintosh HD:Exported Movies:"
  39.     set defaultOutputFolder to "Macintosh HD:Users:MacMini:Recordings:"
  40.    
  41.     -- set this to true to enable movie detection (very crude, just checks the duration)
  42.     -- if a recording is detected as being a movie (see movieDetectionDuration below) is placed in the movieOutputFolder instead of the defaultOutputFolder
  43.     set movieDetectionEnabled to true
  44.     set movieOutputFolder to "Macintosh HD:Users:MacMini:Movie Recordings:"
  45.     -- recordings with a scehduled duration (in seconds 5400 = 90 minutes) greater than this will be considered movies instead of tv shows
  46.     set movieDetectionDuration to 5399
  47.     -- set this to true to enable tv show detection
  48.     -- if a recording is not long enough to be a movie, and has a scrapeable name, it is placed in the televisionOutputFolder instead of the defaultOutputFolder
  49.     set televisionDetectionEnabled to true
  50.     set televisionOutputFolder to "Macintosh HD:Users:MacMini:AeroFS:Daytime TV:"
  51.    
  52.     -- set to true to only process selected recordings, set to false to process all recordings
  53.     set onlyExportSelectedRecordings to false
  54.     -- set to true to only export if the file will be scrapeable by Plex, set to false to export all files regardless of scrapeability
  55.     set onlyExportScrapeableRecordings to false
  56.     -- set this to true to enable fixing of shows that did not export because of onlyExportScrapeableRecordings by going to "Edit Info..." in EyeTV and adding the proper SXXEXX to the begining of the episode title. This will let the file export even if onlyExportScrapeableRecordings is true in that situation.
  57.     -- Also if you use the useSeasonFolder setting, as long as the beginning of the renamed episode title is in the form (s|S)[0-9]+(e|E)[0-9]+ (i.e. S01E01 or s1e1 etc.) the season will be parsed out and the folder created. If you plan to rename shows in EyeTV and not use the SXXEXX prefix set this to false, otherwise you may get unexpected results.
  58.     set treatRenamedRecordingsAsScrapeable to true
  59.    
  60.     -- add to this list any shows which you want to add the year in the title (usually because the name has been reused by multiple shows)
  61.     -- this makes sure Plex scrapes the right show
  62.     set duplicateNameShows to {"That Show"}
  63.     -- list shows that are known to scrape properly by air date  (include year in parentheses "()" if show is also in duplicateNameShows above)
  64.     set airDateScrapableShows to {"General Hospital", "The View", "The Bold and the Beautiful", "The Ellen DeGeneres Show", "The Young and the Restless"}
  65.    
  66.     -- set to true to create show and season sub folders, set both to false to place all files in root export folder
  67.     -- set useShowFolders to true and useSeasonFolders to false to place all seasons in the root show folder
  68.     set useShowFolders to true
  69.     -- set this to true to create season sub folders, only works if useShowFolders is set to true
  70.     set useSeasonFolders to false
  71.     -- set to true if you want movies to be put in a folder (it will have the same name as the movie file)
  72.     set useMovieFolders to false
  73.     -- set to true use underscores instead of spaces when creating folders
  74.     set useUnderscoresInFolderNames to false
  75.     -- set to true use underscores instead of spaces when naming exported files
  76.     set useUnderscoresInFileNames to false
  77.     -- set this to true to add the preset as a suffix to the exported file name
  78.     set addPresetSuffix to false
  79.    
  80.     -- set the fallback preset if the type of content cannot be identified
  81.     tell application "EyeTV" to set defaultPreset to iPod
  82.     -- set this to true to use a different preset for movies
  83.     set useMoviePreset to false
  84.     -- set the default preset for movies from any of the ones in allowedPresets
  85.     tell application "EyeTV" to set moviePreset to iPad
  86.     -- set this to true to use the override preset for certain tv shows
  87.     set useOverridePreset to false
  88.     -- set the override preset from any of the ones in allowedPresets
  89.     tell application "EyeTV" to set overridePreset to HD720p
  90.     -- list the shows you want to use the override preset (include year in parentheses "()" if the show is also in duplicateNameShows above)
  91.     set overridePresetShows to {"Supernatural", "Smallville", "Fringe", "Chuck", "The Cape (2011)", "Castle (2009)", "House"}
  92.    
  93.     -- set to true to clear out all SymLinks when creating new ones
  94.     -- make sure this is set to false if you are using SymLinks already in your plex folders so they don't get deleted
  95.     set deleteAllSymLinksIfAddingNewSymLink to false
  96.    
  97.     -- end modifying settings here ---------------------------------------------------------
  98.    
  99.     -- validate the presets
  100.     tell application "EyeTV"
  101.         if defaultPreset is not in allowedPresets or moviePreset is not in allowedPresets or (useOverridePreset is true and overridePreset is not in allowedPresets) then
  102.             if not silentErrors then display dialog "exiting: one of your presets is invalid"
  103.             return
  104.         end if
  105.     end tell
  106.    
  107.     -- validate the output directories
  108.     if not finderItemExists(defaultOutputFolder) or (movieDetectionEnabled is true and not finderItemExists(movieOutputFolder)) or (televisionDetectionEnabled is true and not finderItemExists(televisionOutputFolder)) then
  109.         if not silentErrors then display alert "exiting: one of your output folders does not exist"
  110.         return
  111.     end if
  112.    
  113.     -- initialize lists for generating output file names
  114.     set recordingsToExport to {}
  115.     set outputFolderNames to {}
  116.     set outputShowFolderNames to {}
  117.     set outputSeasonFolderNames to {}
  118.     set outputFileNames to {}
  119.     set outputPresets to {}
  120.     set outputFilePaths to {}
  121.    
  122.     -- get recordingList from EyeTV
  123.     tell application "EyeTV"
  124.         if onlyExportSelectedRecordings then
  125.             set recordingList to (selection of programs window)
  126.         else
  127.             set recordingList to recordings
  128.         end if
  129.         if (length of recordingList = 0) then
  130.             if not silentErrors then display alert "exiting: no recordings to export"
  131.             return
  132.         end if
  133.     end tell
  134.    
  135.     repeat with currentRecording in recordingList
  136.         tell application "EyeTV"
  137.             -- check if recording is busy
  138.             set recordingIsBusy to busy of currentRecording
  139.         end tell
  140.         if not recordingIsBusy then
  141.             tell application "EyeTV"
  142.                 -- get the location of the recording
  143.                 set recordingLocation to location of currentRecording
  144.                
  145.                 -- get the recording quality
  146.                 -- set showRecordingQuality to quality of currentRecording
  147.                
  148.                 -- get the episode title in EyeTV in case it was modified by the user
  149.                 set showEpisodeTitleFromEyeTV to episode of currentRecording
  150.                
  151.                 -- get the scheduled duration to try and detect if the recording is a movie
  152.                 set showScheduledDuration to duration of currentRecording
  153.                 -- display dialog "showScheduledDuration = " & showScheduledDuration
  154.                 -- determine if recording is a movie
  155.                 if movieDetectionEnabled is true and showScheduledDuration is greater than movieDetectionDuration then
  156.                     set recordingIsMovie to true
  157.                 else
  158.                     set recordingIsMovie to false
  159.                 end if
  160.                
  161.                 -- get the air date
  162.                 set {year:y, month:m, day:d} to (the start time of the currentRecording)
  163.                 set showAirDate to (m & "-" & d)
  164.             end tell
  165.            
  166.             -- locate the .eyetvp file which has the property list data about the episode
  167.             tell application "Finder"
  168.                 set recordingFolder to container of file recordingLocation as alias
  169.                 set recordingPropertiesFile to item 1 of (every paragraph of (do shell script "ls -1 " & (quoted form of POSIX path of recordingFolder) & "/*.eyetvp" as string))
  170.             end tell
  171.            
  172.             -- gather data from .plist file
  173.             tell application "System Events"
  174.                 set showTitle to value of property list item "TITLE" of property list item "epg info" of property list file recordingPropertiesFile
  175.                 set showYear to value of property list item "YEAR" of property list item "epg info" of property list file recordingPropertiesFile
  176.                 set showSeasonNumber to value of property list item "SEASONID" of property list item "epg info" of property list file recordingPropertiesFile
  177.                 set showEpisodeNumber to value of property list item "EPISODENUM" of property list item "epg info" of property list file recordingPropertiesFile
  178.                 set showEpisodeAbstract to value of property list item "ABSTRACT" of property list item "epg info" of property list file recordingPropertiesFile
  179.             end tell
  180.            
  181.             -- add year on movies or shows that have had their name reused so Plex picks the right one
  182.             if showTitle is in duplicateNameShows or recordingIsMovie is true then set showTitle to showTitle & " (" & showYear & ")"
  183.            
  184.             -- remove illegal characters from the show title           
  185.             set showTitleClean to my cleanText(showTitle)
  186.            
  187.             -- initialize nameIsScrapeable
  188.             set nameIsScrapeable to false
  189.            
  190.             -- determine the output file name
  191.             if recordingIsMovie then
  192.                 -- if it is a movie just name it with the title
  193.                 set outputFileName to showTitleClean
  194.                 set nameIsScrapeable to true
  195.             else if showSeasonNumber is not "" and showEpisodeNumber is not "" then
  196.                 -- determine if we need leading zeroes
  197.                 set seasonLeadingZero to ""
  198.                 set episodeLeadingZero to ""
  199.                 if showSeasonNumber as number is less than 10 then set seasonLeadingZero to "0"
  200.                 if showEpisodeNumber as number is less than 10 then set episodeLeadingZero to "0"
  201.                 -- if we have valid season and episode then set the scrapeable name
  202.                 set outputFileName to showTitleClean & " S" & seasonLeadingZero & showSeasonNumber & "E" & episodeLeadingZero & showEpisodeNumber
  203.                 set nameIsScrapeable to true
  204.             else if showTitle is not in airDateScrapableShows and (showEpisodeTitleFromEyeTV is not "" or showEpisodeAbstract is not "") then
  205.                 -- If we have an episode title in the Abstract or in EyeTV, use the episode title to help you figure out the episode when renaming later
  206.                 if showEpisodeTitleFromEyeTV is not equal to showEpisodeAbstract then
  207.                     -- user has modified the episode title in EyeTV, so we use the EyeTV title. This allows you to put SXXEXX in the beginning of the episode title manually if you know which episode it is
  208.                     set outputFileName to showTitleClean & " " & my cleanText(showEpisodeTitleFromEyeTV)
  209.                     ignoring case
  210.                         -- check if we should treat the renamed recording as scrapeable
  211.                         if treatRenamedRecordingsAsScrapeable is true and first character of showEpisodeTitleFromEyeTV is "S" then
  212.                             -- we need to make a season folder, try to extract it from the modified name
  213.                             -- I am assuming that since you have modified the episode title from the original one and it now starts with an S or s, it is in the SXXEXX format
  214.                             -- So I will try to extract the season number from that SXXEXX
  215.                             -- get position of the first "e" or "E"
  216.                             set ePosition to offset of "E" in showEpisodeTitleFromEyeTV
  217.                             if ePosition is greater than 2 then
  218.                                 -- there was an "E" or "e" somewhere, so extract the text between the "S" and the "E"
  219.                                 set extractedSeasonText to listToText(text 2 thru (ePosition - 1) of characters of showEpisodeTitleFromEyeTV, "")
  220.                                 -- convert it to a number in case it has leading zeroes
  221.                                 set extractedSeasonNumber to extractedSeasonText as number
  222.                                 -- check if we got a valid number out of it
  223.                                 if extractedSeasonNumber is greater than -1 then
  224.                                     -- we got a good number, so set it as the season number and mark the recording as scrapeable
  225.                                     set showSeasonNumber to extractedSeasonNumber as text
  226.                                     set nameIsScrapeable to true
  227.                                 end if
  228.                             end if
  229.                         end if
  230.                     end ignoring
  231.                 else
  232.                     -- just use the original episode title from the abstract, not likely to be scrapeable
  233.                     set outputFileName to showTitleClean & " - " & my cleanText(showEpisodeAbstract)
  234.                 end if
  235.             else
  236.                 -- no episode title available or it is an air date scrapeable show so add air date
  237.                 set outputFileName to showTitleClean & " " & showAirDate
  238.                 if showTitle is in airDateScrapableShows then
  239.                     -- show is known to be scrapeable by air date, so set recording as scrapeable
  240.                     set nameIsScrapeable to true
  241.                 end if
  242.             end if
  243.             if outputFileName is in outputFileNames then
  244.                 -- if we have a duplicate file name, we probably recorded 2 episodes of the same show on the same day and the show does not have guide data
  245.                 set theTimeStamp to (do shell script "date +%H%M%S")
  246.                 -- add a timestamp so there is no name conflict
  247.                 set outputFileName to outputFileName & " " & theTimeStamp
  248.                 -- wait one second (so any other recordings get a different timestamp suffix
  249.                 delay 1
  250.             end if
  251.            
  252.             -- determine if recording should be exported
  253.             set okToExport to (not onlyExportScrapeableRecordings or (onlyExportScrapeableRecordings and nameIsScrapeable)) as boolean
  254.            
  255.             -- if our filename is not empty and recording should be exported, proceed
  256.             if outputFileName is not "" and okToExport is true then
  257.                 tell application "EyeTV"
  258.                     -- choose correct preset
  259.                     if useOverridePreset is true and showTitle is in overridePresetShows then
  260.                         set chosenPreset to overridePreset
  261.                     else if useMoviePreset is true and recordingIsMovie is true then
  262.                         set chosenPreset to moviePreset
  263.                     else
  264.                         set chosenPreset to defaultPreset
  265.                     end if
  266.                     -- get the preset extension
  267.                     set chosenPresetExtension to my getPresetExtension(chosenPreset)
  268.                 end tell
  269.                
  270.                 -- choose correct output folder
  271.                 set chosenOutputFolder to defaultOutputFolder
  272.                 if movieDetectionEnabled is true and recordingIsMovie is true then
  273.                     set chosenOutputFolder to movieOutputFolder
  274.                 else if televisionDetectionEnabled is true and nameIsScrapeable is true then
  275.                     set chosenOutputFolder to televisionOutputFolder
  276.                 end if
  277.                
  278.                 -- initialize subfolder names
  279.                 set showFolderName to ""
  280.                 set seasonFolderName to ""
  281.                 set outputFolderPath to chosenOutputFolder
  282.                
  283.                 -- check if we are making show folders
  284.                 if nameIsScrapeable then
  285.                     if recordingIsMovie is true then
  286.                         if useMovieFolders is true then
  287.                             set showFolderName to showTitleClean
  288.                             -- add underscores to the folder name, if the feature is enabled
  289.                             if useUnderscoresInFolderNames is true then set showFolderName to changeSpacesToUnderscores(showFolderName)
  290.                             -- add folder to output path
  291.                             set outputFolderPath to outputFolderPath & showFolderName & ":"
  292.                         end if
  293.                     else if useShowFolders is true and showTitleClean is not "" then
  294.                         -- set show sub folder name
  295.                         set showFolderName to showTitleClean
  296.                         -- add underscores to the folder name, if the feature is enabled
  297.                         if useUnderscoresInFolderNames is true then set showFolderName to changeSpacesToUnderscores(showFolderName)
  298.                         -- add folder to output path
  299.                         set outputFolderPath to outputFolderPath & showFolderName & ":"
  300.                         -- check if we are making season folders
  301.                         if useSeasonFolders is true and showSeasonNumber is not "" then
  302.                             -- set season sub folder name
  303.                             set seasonFolderName to "Season " & showSeasonNumber
  304.                             -- add underscores to the folder name, if the feature is enabled
  305.                             if useUnderscoresInFolderNames is true then set seasonFolderName to changeSpacesToUnderscores(seasonFolderName)
  306.                             -- add folder to output path
  307.                             set outputFolderPath to outputFolderPath & seasonFolderName & ":"
  308.                         end if
  309.                     end if
  310.                 end if
  311.                
  312.                 -- add preset suffix if specified
  313.                 if addPresetSuffix is true then tell application "EyeTV" to set outputFileName to outputFileName & " " & (chosenPreset as text)
  314.                
  315.                 -- add underscores to file name if specified
  316.                 if useUnderscoresInFileNames is true then set outputFileName to changeSpacesToUnderscores(outputFileName)
  317.                
  318.                 -- build the output file path
  319.                 set outputFilePath to outputFolderPath & outputFileName & chosenPresetExtension
  320.                
  321.                 copy currentRecording to the end of recordingsToExport
  322.                 copy chosenOutputFolder to the end of outputFolderNames
  323.                 copy showFolderName to the end of outputShowFolderNames
  324.                 copy seasonFolderName to the end of outputSeasonFolderNames
  325.                 copy outputFileName to the end of outputFileNames
  326.                 tell application "EyeTV" to copy chosenPreset to the end of outputPresets
  327.                 copy outputFilePath to the end of outputFilePaths
  328.             end if
  329.         end if
  330.     end repeat
  331.    
  332.     -- use for debugging
  333.     -- display dialog my listToText(item 1 of recordingsToExport, return) with title "Recordings To Export"
  334.     -- display dialog my listToText(outputFileNames, return) with title "Output File Names"
  335.     -- display dialog my listToText(outputFilePaths, return) with title "Output File Paths"
  336.     -- display dialog my listToText(outputPresets, return) with title "Output Presets"
  337.    
  338.     -- exit if there are no recordings to export
  339.     set recordingsToExportCount to length of recordingsToExport
  340.     if recordingsToExportCount = 0 then
  341.         if not silentErrors then display alert "exiting: no recordings to export"
  342.         return
  343.     end if
  344.    
  345.     -- clear out any broken symlinks
  346.     if deleteAllSymLinksIfAddingNewSymLink and "SymLink" is in outputPresets then
  347.         do shell script "find -L " & (quoted form of POSIX path of defaultOutputFolder) & " -type l -delete"
  348.         if movieDetectionEnabled then do shell script "find -L " & (quoted form of POSIX path of movieOutputFolder) & " -type l -delete"
  349.         if televisionDetectionEnabled then do shell script "find -L " & (quoted form of POSIX path of televisionOutputFolder) & " -type l -delete"
  350.     end if
  351.    
  352.     -- export recordings
  353.     repeat with recordingIndex from 1 to recordingsToExportCount
  354.         -- get the details of the recording to export from the lists
  355.         set theRecording to item recordingIndex of recordingsToExport
  356.         set theOutputFolder to item recordingIndex of outputFolderNames
  357.         set theShowFolderName to item recordingIndex of outputShowFolderNames
  358.         set theSeasonFolderName to item recordingIndex of outputSeasonFolderNames
  359.         tell application "EyeTV" to set theChosenPreset to item recordingIndex of outputPresets
  360.         set theOutputFilePath to item recordingIndex of outputFilePaths
  361.        
  362.         -- create subfolders if needed
  363.         if (useShowFolders is true or useMovieFolders is true) and theShowFolderName is not "" then
  364.             tell application "Finder"
  365.                 set theShowFolderPath to (theOutputFolder & theShowFolderName) as text
  366.                 if not (exists folder theShowFolderPath) then
  367.                     make new folder at alias theOutputFolder with properties {name:theShowFolderName}
  368.                 end if
  369.                 if useSeasonFolders and theSeasonFolderName is not "" then
  370.                     set theSeasonFolderPath to (theShowFolderPath & ":" & theSeasonFolderName) as text
  371.                     if not (exists folder theSeasonFolderPath) then
  372.                         make new folder at alias theShowFolderPath with properties {name:theSeasonFolderName}
  373.                     end if
  374.                 end if
  375.             end tell
  376.         end if
  377.        
  378.         -- if exporting is enabled, export the recording
  379.         if exportingEnabled then
  380.             if theChosenPreset is not "SymLink" then
  381.                 try
  382.                     tell application "EyeTV" to export from theRecording to theOutputFilePath as theChosenPreset replacing no
  383.                 on error
  384.                     if not silentErrors then display alert "error exporting the file " & theOutputFilePath & " as " & (theChosenPreset as text)
  385.                 end try
  386.             else
  387.                 tell application "EyeTV" to set theRecordingLocation to location of theRecording
  388.                 tell application "Finder"
  389.                     set theRecordingFolder to container of file theRecordingLocation as alias
  390.                     set theRecordingRawFile to item 1 of (every paragraph of (do shell script "ls -1 " & (quoted form of POSIX path of theRecordingFolder) & "/*.mpg" as string))
  391.                 end tell
  392.                 do shell script "ln -s " & (quoted form of POSIX path of theRecordingRawFile) & " " & (quoted form of POSIX path of theOutputFilePath)
  393.             end if
  394.         end if
  395.     end repeat
  396. end run
  397.  
  398. on finderItemExists(thePath)
  399.     try
  400.         set thePath to thePath as alias
  401.     on error
  402.         return false
  403.     end try
  404.     return true
  405. end finderItemExists
  406.  
  407. to listToText(theList, delimiter)
  408.     set astid to AppleScript's text item delimiters
  409.     set AppleScript's text item delimiters to delimiter
  410.     set textResult to (theList as string)
  411.     set AppleScript's text item delimiters to astid
  412.     return textResult
  413. end listToText
  414.  
  415. to textToList(theText, delimiter)
  416.     set astid to AppleScript's text item delimiters
  417.     set AppleScript's text item delimiters to delimiter
  418.     set listResult to text items of theText
  419.     set AppleScript's text item delimiters to astid
  420.     return listResult
  421. end textToList
  422.  
  423. to cleanText(theText)
  424.     set theCleanTextList to {}
  425.     set illegalCharacters to {(ASCII character of 60), (ASCII character of 62), (ASCII character of 58), (ASCII character of 34), (ASCII character of 47), (ASCII character of 92), (ASCII character of 124)}
  426.     repeat with currentCharacter in the characters of theText
  427.         set theCharacter to currentCharacter as text
  428.         if theCharacter is not in illegalCharacters then
  429.             set the end of theCleanTextList to theCharacter
  430.         end if
  431.     end repeat
  432.     return my listToText(theCleanTextList, "")
  433. end cleanText
  434.  
  435. to changeSpacesToUnderscores(theText)
  436.     set theNewTextList to {}
  437.     repeat with currentCharacter in the characters of theText
  438.         set theCharacter to currentCharacter as text
  439.         if theCharacter is " " then
  440.             set the end of theNewTextList to "_"
  441.         else
  442.             set the end of theNewTextList to theCharacter
  443.         end if
  444.     end repeat
  445.     return my listToText(theNewTextList, "")
  446. end changeSpacesToUnderscores
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement