Created
January 9, 2010 21:21
-
-
Save dbr/273110 to your computer and use it in GitHub Desktop.
Name TV episodes by name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.6 | |
"""Simple script to names TV episode by episode name. | |
Files must be named as follows: | |
./ShowName - Episode Name.ext | |
For example: | |
./Scrubs - My First Day.avi | |
""" | |
import re | |
import os | |
import sys | |
from tvdb_api import Tvdb | |
def confirm(question): | |
while 1: | |
ans = raw_input(question) | |
if ans in ['y', 'yes']: | |
return True | |
elif ans in ['n', 'no']: | |
return False | |
elif ans in ['q', 'quit']: | |
sys.exit(1) | |
def nameByName(showname, episodename): | |
"""Searches for episode with correct name. | |
The tvdb_api search is very simple, along the lines of: | |
if searchterm in current_episodename: | |
..so the episode name must be accurate | |
""" | |
show = Tvdb(interactive=True)[showname] | |
sr = show.search(episodename, key='episodename') | |
assert len(sr) == 1, "Got %d search results for %s episode named %s, not sure what to do." % (len(sr), showname, episodename) | |
return show['seriesname'], sr[0] | |
def main(): | |
for fname in sys.argv[1:]: | |
# Get absolute path to file | |
fullpath = os.path.abspath(fname) | |
# Get directory where file is, and extension | |
dirname = os.path.split(fullpath)[0] | |
fname, ext = os.path.splitext(fname) | |
# Pattern to match show/episode name | |
m = re.match('^(.+?) - (.+?)$', fname) | |
if m: | |
# Show name is first group, episode name is second | |
showname = m.group(1) | |
epname = m.group(2) | |
corrected_showname, ep = nameByName(showname, epname) | |
# Construct new episode name | |
newname = "%s - [%02dx%02d] - %s" % ( | |
corrected_showname, | |
int(ep['seasonnumber']), | |
int(ep['episodenumber']), | |
ep['episodename'] | |
) | |
# Get full path to new episode, for renaming | |
newfullpath = os.path.join(dirname, newname) + ext | |
# if the user confirms, rename the file | |
print "Old filename:", fullpath | |
print "New filename:", newfullpath | |
if confirm("Rename? (y/n/q) "): | |
os.rename(fullpath, newfullpath) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment