Guest User

Untitled

a guest
Jan 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'highline/import'
  4. require 'sofa'
  5.  
  6. class NoMatchFoundError < StandardError; end;
  7.  
  8. class EpisodeFile
  9.  
  10. attr_accessor :file, :season, :episode, :extension, :high_def, :valid
  11.  
  12. def initialize(file)
  13. @file = file
  14. @extension = File.extname(file)
  15. @valid = true
  16. extract_episode_info(file)
  17. end
  18.  
  19. def rename(new_name)
  20. File.rename(@file, new_name)
  21. end
  22.  
  23. def valid?
  24. @valid
  25. end
  26.  
  27. private
  28. def extract_episode_info(file)
  29. unless file =~ /[sS]([0-9]{2})[eE]([0-9]{2})/
  30. @valid = false
  31. return
  32. end
  33. @season, @episode = $1.to_i, $2.to_i
  34. file =~ /(720p|1080p)/
  35. @high_def = $1
  36. end
  37. end
  38.  
  39. class ShowRenamer
  40.  
  41. attr_accessor :directory, :folder_name, :episode_files, :tvrage_show, :seasons
  42.  
  43. def initialize(directory)
  44. @season = {}
  45.  
  46. @directory = directory
  47. @folder_name = directory.split('/').last
  48. Dir.chdir(directory)
  49. @episode_files = load_episode_files
  50. @show = find_show_on_tvrage
  51. end
  52.  
  53. def load_episode_files
  54. files = Dir.glob(File.join("**", "*.mkv"))
  55. files = files.map{ |f| EpisodeFile.new(f) }
  56. files.reject{ |f| !f.valid? }
  57. end
  58.  
  59. # Find the correct show on TV Rage
  60. def find_show_on_tvrage
  61. show = nil
  62. query = @folder_name
  63. until show
  64. puts "Searching for #{query}..."
  65. show = Sofa::TVRage::Show.by_name(query)
  66.  
  67. if show.nil?
  68. query = ask("Sorry but we couldn't find the show. Please enter the show name:")
  69. else
  70. display_show_summary(show)
  71. unless agree("Is this the correct show? y/n")
  72. show = nil
  73. query = ask("Please enter the show name:")
  74. end
  75. end
  76. end
  77. show
  78. end
  79.  
  80. def get_season_data_from_tvrage(season_number)
  81. puts "Retrieving data from TVRage...Please wait"
  82. @season[season_number] = @show.season_list[season_number - 1]
  83. end
  84.  
  85. def new_name(season, ep_file)
  86. episode = season.episodes[ep_file.episode - 1]
  87. name = %Q(#{@folder_name} #{season.no}#{episode.num_in_season} - #{episode.title})
  88. name << " #{ep_file.high_def}" if ep_file.high_def
  89. name << ep_file.extension
  90. end
  91.  
  92. def rename_all
  93. @episode_files.each do |ep_file|
  94.  
  95. get_season_data_from_tvrage(ep_file.season) unless @season[ep_file.season]
  96.  
  97. current_season = @season[ep_file.season]
  98. formatted_name = new_name(current_season, ep_file)
  99.  
  100. # if agree "[RENAME] #{ep_file.file} => #{formatted_name} (y/n)"
  101. # ep_file.rename(formatted_name)
  102. # end
  103. puts "[RENAMING] #{ep_file.file} => #{formatted_name}"
  104. ep_file.rename(formatted_name)
  105.  
  106. end
  107. end
  108.  
  109. private
  110.  
  111. # Display the summary of a TVRage show
  112. def display_show_summary(show)
  113. puts "#{show.name} (#{show.started} - #{show.ended})"
  114. end
  115.  
  116. end
  117.  
  118. # Get the given path otherwise use the current path
  119. directory = ARGV[0] || Dir.pwd
  120.  
  121. renamer = ShowRenamer.new(directory)
  122. renamer.rename_all
Add Comment
Please, Sign In to add comment