tel8618217223380 / sagetv-addons

Automatically exported from code.google.com/p/sagetv-addons
0 stars 0 forks source link

Season/episode rename/move script for SJQ #290

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Per the forum, here's the request pasted. I realize what I describe is more 
than a simple rename. I was hoping for a little help with renaming, but will 
gladly accept as much of this as seems doable/reasonable.  

The last question asked was how do I know which season to dump a file in if it 
doesn't have any episode info... short answer is I don't (one of the bad things 
about my process.)  I'm open to suggestions, all I've come up with is just 
moving it to a holding pen for manual sort.
Thanks in advance for ANY help!

If you're able to put something together that would be outstanding!... and I'm 
sure some other sage users do something similar and would benefit.

Here's what I do now:
Test for existing season/episode info, if it exists rename to format:
Series - SxxExx (ex: Dexter - S05E02)

If season/episode info doesn't exist, rename to format:
Series - Episode (ex: Chuck - Chuck vs the Suitcase) I've been doing this by 
stripping the last 10 characters from the file name.
It doesn't work too well with shows like Top Gear that often have no any 
episode title/info... so the work around I've been doing is naming the file 
sequentially so I'm not overwriting by simply doing a test on the name existing 
in archive location, if exist +1 and retest till it fails, that's my new 
name.... crude but it works.  I'm sure there's a better way, but it's all I 
could think of... to be honest it doesn't always work and I never know why, 
which is why I'd rather go with a sjq script....

Then I move it to \\Sage-PC\TV\Series\Season\ folder structure. (ex: 
\\Sage-PC\TV\Dexter\Season 5\Dexter - S05E02.ts ) ---End Quote--- That's a 
little more than a simple rename operation. :)  That's fine, but if you really 
want me to look at this and whip something up then please open a ticket 
(http://code.google.com/p/sagetv-addons/issues/list) with all of these details 
pasted in on my project site.

The scope of this exercise just got a whole lot bigger. :)  It's not a 30 
minute job anymore, but seems like a solid few hours (minimum) to do this and 
test it properly, which is fine, but can't guarantee it'll even be this week 
now, but with a ticket I'll definitely eventually do it.

Something to address in the ticket: If there's no season/episode data available 
then how do you know which season folder to move the file to or does it just go 
under \TV\Series\?

Original issue reported on code.google.com by bikesquid@gmail.com on 18 Mar 2011 at 3:56

GoogleCodeExporter commented 9 years ago
Time permitting, I'll try to whip something up for this; low priority, no rush 
on my part for this one.

The rename is pretty easy with access to the SageAPI.  Just pull all the info 
out of the media file object, figure out what you have and then construct the 
desired file name based on the details available.  In Groovy, to get the info, 
do something like this:

def mf; // This is the media file object we're working with; we got it above 
somehow

def title = ShowAPI.GetShowTitle(mf) // This will always exist
def season = ShowAPI.GetShowSeasonNumber(mf) // Will be 0 if not avaiable
def episode = ShowAPI.GetShowEpisodeNumber(mf) // 0 if not available
def subtitle = ShowAPI.GetShowEpisode(mf) // "" if not available

// This is a long winded approach to computing the file name, but
// should be easier to read this code and follow along

def targetFileName = title // Always start with the title

if(season > 0 && episode > 0) // We have episode data, use it
   targetFileName += " - " + String.format("S%2dE%2d", season, episode)
else if(subtitle != "") // We have a subtitle, use it instead
   targetFileName += " - " + episode
else // We have no info, not sure what you want to do here??
   targetFileName += " - UNKNOWN"
targetFileName += ".ts" // Use commons FilenameUtils to actually figure out the 
ext

// Finally, strip invalid chars from the file name
targetFileName = (targetFileName =~ /[\/\\\*\?<>\":|]+/).replaceAll("")

// Here, I'm just printing the final result, but this is your new file name
println targetFileName

This is a simplistic example, with some corners cut.  I'll fill in the gaps 
when I do the whole thing, but if you wanted to play around yourself, this code 
shows the basic idea on how to compute the desired file name.

NOTE: Code example here may contain slight syntax errors, etc.  Shouldn't be 
much to correct once you get this example inside an IDE.

Original comment by de...@battams.ca on 21 Mar 2011 at 3:56

GoogleCodeExporter commented 9 years ago
Well, I started to 'play around' but I'm way out of my depth here.... what I
did was copy this to a test.groovy script, create a new task for the client
on the server that's got the executable listed as script:rename_test.groovy
and the exec args same as in the comskip example, assuming that's the file
location/name.  The log shows the result as " - UNKNOWN.ts " which I see as
the 'no data available' name.  When I look at the test file in bmt it shows
both season and episode #'s so something's not working in the logic and I've
no idea what I'm doing, much less what I'm doing wrong. Any suggestions?
Thanks in advance.

Original comment by bikesquid@gmail.com on 27 Mar 2011 at 5:18

GoogleCodeExporter commented 9 years ago
Let's see your example script... unfortunately, this ticket really isn't on my 
radar - lots of other Sage things I'm working on when Sage dev time is 
available.  If you show me what you have I'll take a quick look and see if I 
see something obvious.

Original comment by de...@battams.ca on 27 Mar 2011 at 10:02

GoogleCodeExporter commented 9 years ago
I understand it's priority is off the bottom of the list, no worries, any 
suggestions to nudge me in the right direction appreciated.  Recently bmt 
stopped writing .properties files and that's what I was using to pull the info 
via a batch file and a lot of stumbling.  I've tried a few things with your 
provided info as well as the sjq4Metadata info I found 
here:http://code.google.com/p/sagetv-addons/wiki/Sjq4Metadata , but the result 
is I'm unable to get the ShowAPI.GetShow___ to return anything.  Using your 
script example above, I simply cut and pasted into file_rename.groovy, exactly 
as above. Under client in the UI I create a new task called filerename with the 
attached screen prints showing the task details and the resulting log.
When I shorten the script to just: 

def mf; // This is the media file object we're working with; we got it above 
somehow
def title = ShowAPI.GetShowTitle(mf) // This will always exist
println title

I'd expect the log to show the last line being the title, but get nothing.  I'm 
sure I'm doing something wrong and it's blindingly obvious to to someone in the 
know...

Original comment by bikesquid@gmail.com on 28 Mar 2011 at 3:11

Attachments:

GoogleCodeExporter commented 9 years ago
Ok, the problem is you're not actually getting the media file object to work 
with.

Object mf; // We get it from somewhere above

This is my shorthand for "yeah, we need to actually fill in this object, but 
for this code example I'm leaving out the details to simplify the example code."

Leaving everything you have setup exactly as is, replace this:

Object mf;

with this in your 3 line script from above:

def mf = MediaFileAPI.GetMediaFileForID(SJQ4_METADATA["SJQ4_ID"].toInteger())

Now mf actually holds the reference to the media file object and so getting the 
title should now work.  Once you verify that works then add back in the rest of 
the metadata fetching as per my example above.

There is one _very_ important step that _must_ be followed... When you queue up 
these tasks to run, you must do so by going to the media file details screen in 
the UI and then selecting the "Add SJQ task" from that menu.  Doing so means 
the metadata for the task is properly filled in.  If you just manually queue 
the task from the SJQ setup menu then the task will have no metadata and the 
media file lookup will fail.

Let me know if you have anymore questions.

Original comment by de...@battams.ca on 28 Mar 2011 at 3:31

GoogleCodeExporter commented 9 years ago
OK, that's got me in the right direction, now I'm getting "title - S 1 E 7.ts"  
which is close.  I'll try adding another test for >10 vs <10 so formatting of 
numbers will be correct, but how do I actually rename the file?  I've been 
looking through all the script examples and docs I can find and nothing on any 
only thing I can find is DeleteFile...but it couldn't be that easy.... 

Original comment by bikesquid@gmail.com on 28 Mar 2011 at 2:56

GoogleCodeExporter commented 9 years ago
Even better, change this to:

targetFileName += " - " + String.format("S%2dE%2d", season, episode)

this:

targetFileName += " - " + String.format("S%02dE%02d", season, episode)

That will zero pad the numbers, if necessary.  Should do what you want without 
the extra code. :)

Original comment by de...@battams.ca on 28 Mar 2011 at 4:07

GoogleCodeExporter commented 9 years ago
Sweet! Will that pad only to two decimal places, i.e. <10?  

I don't want NOVA - S035E015

How do I tell it to do the rename once I've got the name right?

Original comment by bikesquid@gmail.com on 28 Mar 2011 at 7:40

GoogleCodeExporter commented 9 years ago
It will only pad to 2 digits.

S1E3 will become S01E03, but S10E22 will remain S10E22.

Renaming the file is a little tricky...

First you have to figure out how many segments you have (i.e. Show-0.mpg, 
Show-1.mpg, etc.).  If you assume there can only ever be one then that 
simplifies things (but you may run into trouble if this doesn't always end up 
being the case).

Then for each segment, you need to rename it.  So if your file name ends up 
being:

NOVA - S03E15.ts, but there is more than one segment, you actually want these 
files:

NOVA - S0315-0.ts
NOVA - S0315-1.ts

And so on... I leave this case as an exercise for the reader. :)  I just don't 
have time to sit and think about this case right now.

If there's only one segment then it's really easy to rename the file:

import org.apache.commons.io.FileUtils // Put this line towards top of script

def parentDir = MediaFileAPI.GetParentDirectory(mf)
FileUtils.moveFile(MediaFileAPI.GetSegmentForFile(0), new File(parentDir, 
newTitle))

// I'm assuming newTitle is the variable that holds the new title of the file, 
sub
// as needed

// I'm also assuming you want to keep the file in the same dir; replace 
parentDir
// as needed

Also, you need to determine the proper file extension (unless every recording 
you do is actually .ts).  I'm also cutting a lot of corners and error checking 
with these code examples.  If I handled the error conditions then I might just 
as well write it all now, which I really don't have time for at the moment. :)

Good luck.

Original comment by de...@battams.ca on 28 Mar 2011 at 7:57

GoogleCodeExporter commented 9 years ago
Correction: Replace GetSegmentForFile with GetFileForSegment

Original comment by de...@battams.ca on 28 Mar 2011 at 7:59

GoogleCodeExporter commented 9 years ago
This is what I needed to explore some more.  Thanks.  Don't mean to hijack your 
time, really appreciate the help you've made time to fit in.  I'll think on the 
multiple segments issue but that only happens when things go really pear shaped 
and is way, way over my ability to resolve.  My batch file that worked with 
.properties was just shy of 200 lines so I know error checking is much harder 
to noodle through than it seams.  I was starting to get comfortable with sjq3 
when it all changed on me, so I'll leave it stupid-simple and just get the 
basics working till either I figure out what I'm doing or..., well I'll do my 
best to figure it out without bothering further.
cheers. 

Original comment by bikesquid@gmail.com on 29 Mar 2011 at 4:07

GoogleCodeExporter commented 9 years ago
I think I've got the majority sorted and have an idea of what I need to do, but 
I'm running into an error that I can't sort without a nudge.  

Original comment by bikesquid@gmail.com on 30 Mar 2011 at 6:28

Attachments:

GoogleCodeExporter commented 9 years ago
MediaFileAPI.GetFileForSegment(0) should be:

MediaFileAPI.GetFileForSegment(mf, 0)

Original comment by de...@battams.ca on 30 Mar 2011 at 11:51

GoogleCodeExporter commented 9 years ago
That did it!
Now that the base works I'll work to modify as suits my needs.
Thanks.

Original comment by bikesquid@gmail.com on 30 Mar 2011 at 2:26

GoogleCodeExporter commented 9 years ago
I think you've got through this on your own and don't need me to write this 
anymore?  Marking as WontFix.

Original comment by de...@battams.ca on 3 May 2011 at 12:44