Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More

TV Renamer 2 Atom Feed 0

In Brief Renames TV episode files to a customizable standardized format (i.e. Show Name - 1x01). On OS X, uses AppleScript to set the old name of the file to the comments section (if comments is blank) and hides the file extension.... more
# 's
  1#!/usr/bin/env python
2
3# Copyright 2009 David Isaacson
4# New BSD License
5
6import os
7import re
8import shutil
9import sys
10
11# Disable if you don't have applescript
12applescript = True
13
14# Do the work
15
16for path in sys.argv[1:]:
17
18 path = os.path.abspath(path)
19 file = os.path.basename(path)
20
21 # Try to figure out the filename components
22
23 # Try S01E01, S1E01 formats
24 match = re.search(r'''(?P<title> .+? )
25 [\.\-_\ ]?
26 S0? (?P<season> [0-9]+ )
27 E (?P<episode> [0-9]+ )
28 .*
29 \. (?P<extension> .{2,4} )''', file, re.I | re.X)
30
31 if not match:
32 # Try 1x01, 01x01 formats
33 match = re.search(r'''(?P<title> .+?)
34 [\.-_\ ]
35 0? (?P<season> [0-9]+ )
36 x (?P<episode> [0-9]+ )
37 .*
38 \.(?P<extension> .{2,4} )''', file, re.I | re.X)
39
40 if not match:
41 # Try 101, 0101 formats
42 # This is a separate regex because we only want to do this if the above doesn't work
43 match = re.search(r'''(?P<title> .+?)
44 [\.-_\ ]
45 0? (?P<season> [0-9]+ )
46 (?P<episode> [0-9]{2,} )
47 .*
48 \.(?P<extension> .{2,4} )''', file, re.I | re.X)
49
50 if not match:
51 print "Can't parse filename '%s', sorry." % path
52 continue
53
54 groups = match.groupdict()
55
56 title = groups['title']
57 season = groups['season']
58 episode = groups['episode']
59 extension = groups['extension']
60
61 # Mess with the title a bit
62
63 # Change periods, underbars to spaces
64 title = re.sub('[\._]', ' ', title)
65 # If you *want* periods, try this:
66 # title = re.sub('[ _]', '.', title)
67
68 # Remove more than one dash (in retrospect I don't know why this is here)
69 title = re.sub('(- )+', '- ', title)
70
71 # Remove multiple spaces
72 title = re.sub(' +', ' ', title)
73
74 # title case
75 title = title.title()
76
77 # strip extra spaces
78 title = title.strip()
79
80 # Reassemble file name like 1x01
81 newfile = '%s - %sx%s.%s' % (title, season, episode, extension)
82 # Reassemble file name like s01e01 - only do one of these : )
83 #newfile = '%s - s%02ie%02i.%s' % (title, int(season), int(episode), extension)
84
85 newpath = os.path.join(os.path.dirname(path), newfile)
86
87 shutil.move(path, newpath)
88
89 # Applescript goodness
90 if applescript:
91 # Set Comments to Old Path
92 script = '''
93 set newpath to "%s"
94 set macpath to (POSIX file newpath) as string
95 tell application "Finder"
96 if the comment of file macpath is "" then
97 set the comment of file macpath to "%s"
98 end if
99 set extension hidden of file macpath to true
100 end tell
101 ''' % (newpath, file)
102
103 os.system('/usr/bin/osascript -sh > /dev/null <<END\n%s\nEND' % script)
104
105
106

Renames TV episode files to a customizable standardized format (i.e. Show Name - 1x01). On OS X, uses AppleScript to set the old name of the file to the comments section (if comments is blank) and hides the file extension.

This is a rewrite, in Python, of an older perl version: TV Renamer. This version should be more customizable and easier to understand. Hopefully. :-D

Comments

over 7 years ago (13 Jan 2009 at 12:17 PM) by Stou S.
Isn't there an automatic way to detect if applescript exists on the platform?
over 7 years ago (13 Jan 2009 at 04:16 PM) by David Isaacson
Yeah, but it requires running 'osalang' and checking if 'AppleScript' is one of the returned languages; I was too lazy to figure out how to redirect stdout and check it.

Does it work for you?
over 7 years ago (13 Jan 2009 at 04:17 PM) by David Isaacson
Oops I meant to post that as a reply : ) If only there were some sort of 'delete comments' feature :-D
over 7 years ago (13 Jan 2009 at 05:16 PM) by Stou S.
What would be uber-sweet is if you can drag your comment under someone else's comment and make it a reply.... in those rare cases when you forget to click 'reply'... we don't want to be wasting sequence numbers (i.e. comments.id)
over 7 years ago (13 Jan 2009 at 08:37 PM) by David Isaacson
hehe yeah