Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2.6
- """Simple script to names TV episode by first aired date.
- Files must be organised as follows:
- ./ShowName/20091130-anything.ext
- For example:
- ./Scrubs/20050118.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 nameByDate(showname, date):
- """Searches for episode with correct firstaired data.
- Date format must be "YYYY-MM-DD"
- """
- show = Tvdb(interactive=True)[showname]
- sr = show.search(date, key='firstaired')
- assert len(sr) == 1, "Got multiple search results for %s aired on %s, not sure what to do." % (showname, date)
- 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]
- ext = os.path.splitext(fname)[1]
- # Pattern to match show name, and first-aired date against path
- m = re.match('^.*/(.+?)/(\d\d\d\d)(\d\d)(\d\d)[^\d]*$', fullpath)
- if m:
- # Show name is first group
- showname = m.group(1)
- # Construct into yyyy-mm-dd format
- date = "-".join(m.groups()[1:])
- corrected_showname, ep = nameByDate(showname, date)
- # 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()
Add Comment
Please, Sign In to add comment