Guest User

Untitled

a guest
Mar 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #!/usr/bin/env python2.6
  2. """Simple script to names TV episode by first aired date.
  3.  
  4. Files must be organised as follows:
  5.  
  6. ./ShowName/20091130-anything.ext
  7.  
  8. For example:
  9.  
  10. ./Scrubs/20050118.avi
  11. """
  12.  
  13. import re
  14. import os
  15. import sys
  16.  
  17. from tvdb_api import Tvdb
  18.  
  19.  
  20. def confirm(question):
  21. while 1:
  22. ans = raw_input(question)
  23. if ans in ['y', 'yes']:
  24. return True
  25. elif ans in ['n', 'no']:
  26. return False
  27. elif ans in ['q', 'quit']:
  28. sys.exit(1)
  29.  
  30.  
  31. def nameByDate(showname, date):
  32. """Searches for episode with correct firstaired data.
  33. Date format must be "YYYY-MM-DD"
  34. """
  35. show = Tvdb(interactive=True)[showname]
  36. sr = show.search(date, key='firstaired')
  37. assert len(sr) == 1, "Got multiple search results for %s aired on %s, not sure what to do." % (showname, date)
  38. return show['seriesname'], sr[0]
  39.  
  40.  
  41. def main():
  42. for fname in sys.argv[1:]:
  43. # Get absolute path to file
  44. fullpath = os.path.abspath(fname)
  45.  
  46. # Get directory where file is, and extension
  47. dirname = os.path.split(fullpath)[0]
  48. ext = os.path.splitext(fname)[1]
  49.  
  50. # Pattern to match show name, and first-aired date against path
  51. m = re.match('^.*/(.+?)/(\d\d\d\d)(\d\d)(\d\d)[^\d]*$', fullpath)
  52. if m:
  53. # Show name is first group
  54. showname = m.group(1)
  55. # Construct into yyyy-mm-dd format
  56. date = "-".join(m.groups()[1:])
  57.  
  58. corrected_showname, ep = nameByDate(showname, date)
  59.  
  60. # Construct new episode name
  61. newname = "%s - [%02dx%02d] - %s" % (
  62. corrected_showname,
  63. int(ep['seasonnumber']),
  64. int(ep['episodenumber']),
  65. ep['episodename']
  66. )
  67.  
  68. # Get full path to new episode, for renaming
  69. newfullpath = os.path.join(dirname, newname) + ext
  70.  
  71. # if the user confirms, rename the file
  72. print "Old filename:", fullpath
  73. print "New filename:", newfullpath
  74. if confirm("Rename? (y/n/q) "):
  75. os.rename(fullpath, newfullpath)
  76.  
  77.  
  78. if __name__ == '__main__':
  79. main()
Add Comment
Please, Sign In to add comment