Advertisement
AncientPC

anime renumber

Apr 17th, 2012
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from optparse import OptionParser
  4. import os
  5. import re
  6. import sys
  7.  
  8. VERSION = "0.1"
  9. #compatible with Python v2.6.6+
  10.  
  11. def set_options():
  12.     """
  13.     parses command line options, checks source and destination directory existence.
  14.     """
  15.     global OPT, ARG, DEST
  16.     usage = "%prog [option] dir1 [dir2] [dir3] [...]"
  17.     version = "%prog " + VERSION
  18.     description = "Creates season-based symlinks based for absolutely numbered animes."
  19.     parser = OptionParser(usage=usage,version=version,description=description)
  20.     parser.set_defaults(recursion=False,run=True,verbose=0)
  21.  
  22.     parser.add_option("-v", "--verbose",
  23.         dest="verbose", action="store_const", const=2,
  24.         help="increases output verbosity")
  25.  
  26.     parser.add_option("-n", "--dry-run",
  27.         dest="run", action="store_false",
  28.         help="simulate actions without making any changes.")
  29.  
  30.     (OPT, ARG) = parser.parse_args()
  31.  
  32.     #checks arguments
  33.     if len(ARG) < 1:
  34.         print "Not enough arguments"
  35.         parser.print_help()
  36.         sys.exit(2)
  37.  
  38.     for dir in ARG:
  39.         if not os.path.exists(dir):
  40.             print "ERROR:", dir, "does not exist."
  41.             sys.exit(2)
  42.  
  43.     if OPT.verbose > 2:
  44.         print "OPT =", OPT
  45.         print "ARG =", ARG
  46.         print
  47.  
  48.  
  49. def abs2sep(series, aep):
  50.     #if OPT.verbose > 1:
  51.         #print "series =",series
  52.         #print "abs ep =",aep
  53.  
  54.     anime = {
  55.         'Bleach': [20, 21, 22, 28, 18, 22, 20, 16, 22, 16, 7, 17, 36, 51, 26, 999],
  56.         'Naruto Shippuuden': [32, 21, 18, 17, 24, 31, 8, 24, 21, 22, 24, 999]
  57.         }
  58.  
  59.     if not(series in anime):
  60.         return (-1,-1)
  61.  
  62.     a = aep
  63.     s = 0
  64.     e = 0
  65.  
  66.     for num in anime[series]:
  67.         if a <= 0:
  68.             break
  69.         if (a-num) <= 0:
  70.             e = a
  71.         a -= num
  72.         s += 1
  73.  
  74.     return (s,e)
  75.  
  76. def create_symlink(source, target_dir, target_ext, series, season, ep):
  77.     link_name = "%s %dx%d.%s" % (series, season, ep, target_ext)
  78.     target_file = os.path.join(target_dir,link_name)
  79.  
  80.     # remove existing symlink conflicts in case paths change
  81.     if (OPT.run == True):
  82.         # only remove symlink if they point to different files
  83.         if os.path.islink(target_file):
  84.             if os.stat(source).st_ino != os.stat(target_file).st_ino:
  85.                 os.remove(target_file)
  86.             else:
  87.                 return
  88.  
  89.         print source,
  90.         print " ==> ",target_file
  91.         os.symlink(source,target_file)
  92.  
  93. def main():
  94.     set_options()
  95.  
  96.     for dir in ARG:
  97.         for dir_path, dir_names, file_names in os.walk(dir):
  98.             for file in file_names:
  99.                 match = re.search(r'\[.+\](.+)-\s+(\d+).*\.([^\.]+)$',str(file))
  100.  
  101.                 if match == None:
  102.                     if OPT.verbose > 1:
  103.                         print "[SKIP]", file
  104.  
  105.                     continue
  106.  
  107.                 series = match.group(1).strip()
  108.                 abs_ep = int(match.group(2))
  109.                 file_ext = match.group(3).strip()
  110.  
  111.                 sep = abs2sep(series,abs_ep)
  112.                 if sep[0] != -1:
  113.                     create_symlink(os.path.join(dir_path,file), dir_path, file_ext, series, sep[0], sep[1])
  114.  
  115. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement