rebhichaouki / sagetv-addons

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

Add Season Number and Episode Number to SJQ4 Metadata objects #323

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
It would be useful to have these Iems added to the Show Metadata objects to 
facilitate tasks like renaming files that can be read by other software like 
Plex and XBMC.

Original issue reported on code.google.com by wayne.ko...@gmail.com on 1 Jan 2012 at 2:46

GoogleCodeExporter commented 9 years ago
Are you using groovy to do your file renaming?  If so, you don't need these 
added to the metadata object.  It's just as easy to directly access the values 
in your groovy script:

def mediaFile = 
MediaFileAPI.GetMediaFileForID(SJQ4_METADATA['SJQ4_ID'].toInteger())
def seasonNum = ShowAPI.GetShowSeasonNumber(mediaFile)
def episodeNum = ShowAPI.GetShowEpisodeNumber(mediaFile)
if(seasonNum > 0 && episodeNum > 0)
    println String.format('S%02dE%02d', seasonNum, episodeNum)
else
    println 'No S/E data available for this media file!'

I may add the env vars for non-groovy users, but unless someone actually 
specifically requests it, I probably won't since most (remaining) users tend to 
just script everything in Groovy.

Original comment by de...@battams.ca on 1 Jan 2012 at 4:38

GoogleCodeExporter commented 9 years ago

Original comment by de...@battams.ca on 1 Jan 2012 at 4:38

GoogleCodeExporter commented 9 years ago
I will just use Groovy to do this scripting but there might be reasons to want 
to do some stuff in Batch files.  In fact I am in the process of writing a 
Groovy script to rename a file adding these characteristics as this will allow 
Plex to recognize these files.

I just figured since pretty much everything else is available in ENV then these 
items should be as well.

By the way can I use that string format line to define the variable for the 
filename?

In other words do something like:
NewFileName=ShowName+"-"+String.format('S%02dE%02d', seasonNum, 
episodeNum)+EpisodeName+"-"+SageID+"-"+segment+"."+FileExtension

Trapping for Episode numbers will be tricky since TVDB has instances of Episode 
0s for some seasons as people have used these for Specials.

Original comment by wayne.ko...@gmail.com on 1 Jan 2012 at 4:47

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Wow that was fast.  I have been working on this for hours but my solution was 
certainly a lot less elegant than yours due to my lack of skills in Groovy and 
Java.  I will try to use this in my code and to add the renaming of artifacts 
like edl files just as you do in your move file script.

I was also going to skip multi-segment files since it seemed like too much work 
to do the code and they are fairly rare - I figured I would do them manually.

Would it make sense to make sure that the show is a TV show or will that get 
caught by checking for valid Season-Episode nums?

Original comment by wayne.ko...@gmail.com on 1 Jan 2012 at 5:55

GoogleCodeExporter commented 9 years ago
Had to delete my original script... it had a bug in it. :(  I'll post a new one 
shortly.

Original comment by de...@battams.ca on 1 Jan 2012 at 5:57

GoogleCodeExporter commented 9 years ago
Much better...

Changes:

* Fix bug where sanitize function didn't work right
* Replace illegal chars in file name with '.' instead of '', adjust as desired

Enjoy...

This code only examines tv recordings.  If you wanted to skip movies recorded 
from tv, for example, then you'd have to tweak it a little (though the S/E 
check should skip movies anyway).  No more tweaking from me today, however, the 
couch and NFL football is calling!  I'm in my fantasy league championship 
today!! :D

===== START =====

import org.apache.commons.io.FilenameUtils

static class Data {
    static final List ILLEGAL_CHARS = [34, 60, 62, 124, 47, 58, 42, 63, 92]
}

def sanitize(def input) {
    def output = input
    for(def i = 0; i < input.length(); ++i) {
        def val = (input[i] as char) as int
        if(val < 32 || val > 126 || Data.ILLEGAL_CHARS.contains(val))
            output = output.replace(input[i], '.')
    }
    return output
}

MediaFileAPI.GetMediaFiles('T').each {
    def season = ShowAPI.GetShowSeasonNumber(it)
    def episode = ShowAPI.GetShowEpisodeNumber(it)
    if(season > 0 && episode > 0) {
        (0 .. MediaFileAPI.GetNumberOfSegments(it) - 1).each { i ->
            def segFile = MediaFileAPI.GetFileForSegment(it, i)
            def newName = sanitize(String.format('%s-S%02dE%02d-%s-%d-%d.%s', MediaFileAPI.GetMediaTitle(it), ShowAPI.GetShowSeasonNumber(it), ShowAPI.GetShowEpisodeNumber(it), ShowAPI.GetShowEpisode(it), AiringAPI.GetAiringID(it), i, FilenameUtils.getExtension(segFile.getName())))
            println "$segFile >> $newName"
        }
    }
}
return 0

=====  END  =====

===== SNIPPET OF OUTPUT FROM MY SYSTEM ======

D:\tv\CallMeFitz-WhattheFisaBeaverMoon-11229065-0.ts >> Call Me 
Fitz-S02E13-What the F... is a Beaver Moon-11229065-0.ts
D:\tv\BoardwalkEmpire-TotheLost-11229067-0.ts >> Boardwalk Empire-S02E12-To the 
Lost-11229067-0.ts
D:\tv\MythBusters-ToiletBomb-11240339-0.ts >> MythBusters-S09E22-Toilet 
Bomb-11240339-0.ts
\\nas\tv\FamilyGuy-GrumpyOldMan-11227212-0.ts >> Family Guy-S10E09-Grumpy Old 
Man-11227212-0.ts
\\nas\tv\TheSimpsons-DonnieFatso-4505254-0.ts >> The Simpsons-S22E09-Donnie 
Fatso-4505254-0.ts
D:\tv\PersonofInterest-NumberCrunch-11263405-0.ts >> Person of 
Interest-S01E10-Number Crunch-11263405-0.ts
D:\tv\247FlyersRangersRoadtotheNHLWinterClassic-11438174-0.ts >> 24.7 
Flyers.Rangers. Road to the NHL Winter Classic-S01E03--11438174-0.ts

=====               END                ======

Original comment by de...@battams.ca on 1 Jan 2012 at 6:08