Class: Imba::MovieList
- Inherits:
-
Object
- Object
- Imba::MovieList
- Includes:
- Colors
- Defined in:
- lib/imba/movie_list.rb
Defined Under Namespace
Classes: Diff
Instance Attribute Summary collapse
-
#directory_queue ⇒ Object
Returns the value of attribute directory_queue.
-
#movie_queue ⇒ Object
Returns the value of attribute movie_queue.
-
#movies ⇒ Object
readonly
Returns the value of attribute movies.
Instance Method Summary collapse
-
#initialize ⇒ MovieList
constructor
A new instance of MovieList.
- #movie_dirs ⇒ Object
-
#synch ⇒ Object
TODO: make it work first, refactor later (for even more fun!).
Methods included from Colors
Constructor Details
Instance Attribute Details
#directory_queue ⇒ Object
Returns the value of attribute directory_queue.
9 10 11 |
# File 'lib/imba/movie_list.rb', line 9 def directory_queue @directory_queue end |
#movie_queue ⇒ Object
Returns the value of attribute movie_queue.
9 10 11 |
# File 'lib/imba/movie_list.rb', line 9 def movie_queue @movie_queue end |
#movies ⇒ Object (readonly)
Returns the value of attribute movies.
8 9 10 |
# File 'lib/imba/movie_list.rb', line 8 def movies @movies end |
Instance Method Details
#movie_dirs ⇒ Object
19 20 21 |
# File 'lib/imba/movie_list.rb', line 19 def movie_dirs @movie_dirs ||= Dir['*'].select { |f| File.directory? f } end |
#synch ⇒ Object
TODO: make it work first, refactor later (for even more fun!)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/imba/movie_list.rb', line 26 def synch prompt = '(enter "y" to confirm or anything else to continue)' indexed_movies = Movie.pluck(:uniq_id) p = ProgressBar.create(title: 'Scanning your movies', total: movie_dirs.length) skipped = [] # populate queue movie_dirs.each do |directory_name| FileUtils.cd(directory_name) do if File.exist?('.imba') && File.open('.imba') { |f| indexed_movies.include?(f.read.to_i) } skipped << directory_name p.increment else directory_queue.push(directory_name) end end end # download all the movies until directory_queue.empty? directory_name = directory_queue.pop result = Imdb::Movie.search(directory_name).first movie_title = result.title.gsub(/\(\d+\)|\(.*\)/, '').strip.force_encoding('UTF-8') movie_queue.push Diff.new(directory_name, movie_title, result) p.increment end # puts foundings until movie_queue.empty? diff = movie_queue.pop # update movie name? (folder) if diff.directory_name != diff.movie_title STDOUT.puts "change #{red(diff.directory_name)} => #{green(diff.movie_title)}? \n#{prompt}" FileUtils.mv(diff.directory_name, movie_title) if STDIN.gets.strip.downcase == 'y' end # create movie from result STDOUT.puts "save #{green(diff.movie_title)}? \n#{prompt}" if STDIN.gets.strip.downcase == 'y' movie = Movie.create( uniq_id: diff.result.id, name: diff.movie_title, year: diff.result.year, genres: diff.result.genres.map { |g| g.downcase.to_sym }, rating: Float(diff.result.) ) FileUtils.cd(diff.movie_title) do File.open('.imba', 'w+') { |f| f.write(movie.uniq_id) } end STDOUT.puts cyan("#{movie.to_s}\n") end end skipped.each { |skip| STDOUT.puts yellow("skipped: #{skip}") } STDOUT.puts "\ndone" end |