Advertisement
Guest User

massrename r2

a guest
Jul 5th, 2012
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // HIT ENTER TO KILL SCRIPT
  2. Thread.startDaemon {
  3.     System.in.read()
  4.     System.exit(0)
  5. }
  6.  
  7.  
  8. def input = args.getFiles{ it.isVideo() || it.isSubtitle() }
  9.  
  10. // ignore clutter files
  11. input = input.findAll{ !(it.path =~ /\b(?i:sample|trailer|extras|deleted.scenes|music.video|scrapbook)\b/) }
  12.  
  13.  
  14.  
  15. def isEpisodeFile(f) {
  16.     parseEpisodeNumber(f.name) || parseDate(f.name) || f.name =~ /\b\d+(?i:of)\d+\b/ || (parseEpisodeNumber(f.name, false) && f.name =~ /\b\p{XDigit}{8}\b/)
  17. }
  18.  
  19.  
  20. def groups = input.groupBy{ f ->
  21.     def match = [movie:null, series:null, anime:null]
  22.        
  23.     // try as movie
  24.     def movie = isEpisodeFile(f) ? null : detectMovie(f, false, Locale.ENGLISH, null, TheMovieDB)
  25.     if (movie) {
  26.         match.movie = movie
  27.     }
  28.    
  29.     // try as tv series
  30.     def seriesName = detectSeriesName(f) ?: detectSeriesName(f.dir.listFiles{ it.isVideo() })
  31.     if (seriesName) {
  32.         def resultSet = TheTVDB.search(seriesName, Locale.ENGLISH)
  33.         if (resultSet.size() > 0) {
  34.             match.series = resultSet
  35.         }
  36.     }
  37.    
  38.     // try as anime
  39.     def animeName = detectSeriesName(f) ?: detectSeriesName(f.dir.listFiles{ it.isVideo() })
  40.     if (animeName) {
  41.         def resultSet = AniDB.search(animeName, Locale.ENGLISH)
  42.         if (resultSet.size() > 0) {
  43.             match.anime = resultSet
  44.         }
  45.     }
  46.    
  47.     println "$f.name => $match"
  48.     return match
  49. }
  50.  
  51. groups.each{ match, files ->
  52.     def score = [movie:0, series:0, anime:0]
  53.    
  54.     files.each{ f ->
  55.         if (parseEpisodeNumber(f.name, true) || parseDate(f.name) || f.name =~ /\b\d+(?i:of)\d+\b/)
  56.             score.series+=1
  57.        
  58.         if (parseEpisodeNumber(f.name, false) || f.name =~ /\b\p{XDigit}{8}\b/)
  59.             score.anime+=1
  60.            
  61.         if (f =~ /\b(?:19|20)\\d{2}\b/)
  62.             score.movie+=1
  63.        
  64.         // boost best match based on filename similarity
  65.         score.entrySet().sort{ match[it.key] ? [[f.name, f.parentFile.name], match[it.key] as List].combinations().collect{ similarity(it[0], it[1]) }.max() : 0 }.last().value+=1
  66.     }
  67.    
  68.     println "$match => $score"
  69.    
  70.     // select best match
  71.     def m = match.entrySet().sort{ score[it.key] }.last()
  72.     switch (m.key) {
  73.         case 'movie' : return rename(file:files, format:'Movies/{n} ({y})/{n} ({y}){" CD$pi"}', db:'TheMovieDB')
  74.         case 'series': return rename(file:files, format:'TV Shows/{n}/{episode.special ? "Special" : "Season "+s}/{n} - {episode.special ? "S00E"+special.pad(2) : s00e00} - {t}', db:'TheTVDB')
  75.         case 'anime' : return rename(file:files, format:'Anime/{n}/{n} - {sxe} - {t}', db:'AniDB')
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement