1
Users / Re: Need testers - updated 2-18
« on: March 11, 2010, 11:03:09 am »
I have actually done something similar to what you're doing (I have a ruby script that automatically renames episodes downloaded to be in the same format).
The way I did it was first I grab the series and episode marker from the episode. If you did that and then assumed that everything that came before was the series that should work for a very high percentile of files (I don't think I've ever encountered anything it doesn't work for). The only problematic files are the ones which use three digits to represent series and episode (I just wrote a special function called seperate to do this) and the ones that don't split into series and episode just use numbering. With a bit of tweaking you should be able to deal with these cases as well (or at least have a reasonable guess). I used the following regex's to rename the file - I think the ones at the bottom are just putting in uniform spacing but I didn't comment them so not so sure....
The way I did it was first I grab the series and episode marker from the episode. If you did that and then assumed that everything that came before was the series that should work for a very high percentile of files (I don't think I've ever encountered anything it doesn't work for). The only problematic files are the ones which use three digits to represent series and episode (I just wrote a special function called seperate to do this) and the ones that don't split into series and episode just use numbering. With a bit of tweaking you should be able to deal with these cases as well (or at least have a reasonable guess). I used the following regex's to rename the file - I think the ones at the bottom are just putting in uniform spacing but I didn't comment them so not so sure....
Code: [Select]
filename = filename.gsub(/ S([0-9][0-9])E([0-9][0-9][ \.])/i, " \\1x\\2")
filename = filename.gsub(/ S([0-9][0-9]) E([0-9][0-9][ \.])/i, " \\1x\\2")
filename = filename.gsub(/ S([0-9])E([0-9][0-9][ \.])/i, " \\1x\\2")
# Examples: 'S3E04' = '3x04', 'S07E05' = '7x05', 'S07E03-' = '7x03'
filename = filename.gsub(/([\- ][0-9])x([0-9][\- \.])/i, "\\1x0\\2")
# Examples: '3x4-' = '3x04', '6x8 ' = '6x08'
filename = filename.gsub(/0([0-9]x[0-9][0-9])/i, "\\1")
# Examples: '03x12' = '3x12', '01x03' = '1x03'
filename = filename.gsub(/\s*\[([1-9]?[0-9]x[0-9][0-9])\]\s*/i, " \\1 ")
# Examples: ' [14x34] ' = ' 14x34 ' 'Stargate[10x23] ' = 'Stargate 10x23 '
filename = filename.gsub(/([A-Z0-9])\s+([1-9]?[0-9]x[0-9][0-9])/i, "\\1 - \\2")
filename = filename.gsub(/([1-9]?[0-9]x[0-9][0-9])\s+([A-Z0-9])/i, "\\1 - \\2")
filename = filename.gsub(/\s*-\s*([1-9]?[0-9]x[0-9][0-9])/i, " - \\1")
filename = filename.gsub(/([1-9]?[0-9]x[0-9][0-9])\s*-\s*/i, "\\1 - ")