Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

handle movies deleted from TMDB

  • Loading branch information...
commit c08ecfd59c030d5fe2576292084d3c7e7c6f11a7 1 parent 555605e
@mislav authored
View
5 app/controllers/movies_controller.rb
@@ -41,6 +41,11 @@ def show
redirect_to movie_url(@movie.permalink), status: 301
elsif stale? etag: session_cache_key(@movie)
@movie.ensure_extended_info unless Movies.offline?
+
+ if @movie.invalid?
+ @message = "This movie is now unavailable."
+ render 'shared/not_found', :status => 410
+ end
end
end
View
73 app/models/movie.rb
@@ -16,6 +16,7 @@ class Movie < Mingo
property :tmdb_id
property :tmdb_url
property :tmdb_version
+ property :tmdb_updated_at
collection.ensure_index :tmdb_id
@@ -44,6 +45,11 @@ def normalized_title
@normalized_title ||= ::MovieTitle::normalize_title(title)
end
+ # avoid showing invalid movies to users
+ def invalid?
+ tmdb_id.nil?
+ end
+
# warning: db-heavy
def self.find_duplicate_titles
hash = Hash.new { |h,k| h[k] = [] }
@@ -96,27 +102,47 @@ def self.last_watch_created_at
def self.directors_of_movies(movies)
movies.map { |m| m['directors'] }.compact.flatten.histogram.to_a.sort_by(&:last).reverse
end
-
+
def tmdb_movie=(movie)
- self.tmdb_id = movie.id
- self.tmdb_url = movie.url
- self.tmdb_version = movie.version
- self.imdb_id = movie.imdb_id if movie.imdb_id.present? and self.imdb_id.nil?
-
- # renamed properties
- set_unless_locked(:title, movie.name)
- set_unless_locked(:original_title, movie.original_name)
- set_unless_locked(:poster_small_url, movie.poster_thumb)
- set_unless_locked(:poster_medium_url, movie.poster_cover)
- set_unless_locked(:plot, movie.synopsis)
-
- # same name properties
- [:year, :runtime, :countries, :directors, :homepage].each do |property|
- value = movie.send(property)
- set_unless_locked(property, value) if value.present?
+ if movie
+ self.tmdb_id = movie.id
+ self.tmdb_url = movie.url
+ self.tmdb_version = movie.version
+ self.imdb_id = movie.imdb_id if movie.imdb_id.present? and self.imdb_id.nil?
+
+ # renamed properties
+ set_unless_locked(:title, movie.name)
+ set_unless_locked(:original_title, movie.original_name)
+ set_unless_locked(:poster_small_url, movie.poster_thumb)
+ set_unless_locked(:poster_medium_url, movie.poster_cover)
+ set_unless_locked(:plot, movie.synopsis)
+
+ # same name properties
+ [:year, :runtime, :countries, :directors, :homepage].each do |property|
+ value = movie.send(property)
+ set_unless_locked(property, value) if value.present?
+ end
+ else
+ self.tmdb_id = self.tmdb_url = nil
end
+
+ self.tmdb_updated_at = Time.now
end
-
+
+ def tmdb_info_stale?
+ tmdb_updated_at.nil? or tmdb_updated_at < 1.week.ago
+ end
+
+ def update_tmdb_movie
+ if self.tmdb_id
+ self.tmdb_movie = Tmdb.movie_details(self.tmdb_id)
+ if tmdb_id.nil?
+ # this record got deleted from TMDB
+ # TODO: merge with other records if this had watches
+ end
+ end
+ end
+
def netflix_title=(netflix)
self.netflix_id = netflix.id
self.netflix_url = netflix.url
@@ -179,14 +205,9 @@ def update_rotten_movie
EXTENDED = [:runtime, :countries, :directors]
def ensure_extended_info
- if extended_info_missing? and self.tmdb_id
- self.tmdb_movie = Tmdb.movie_details(self.tmdb_id)
- self.save
- end
- if rotten_info_stale?
- update_rotten_movie
- self.save
- end
+ update_tmdb_movie if extended_info_missing? or tmdb_info_stale?
+ update_rotten_movie if rotten_info_stale?
+ self.save
rescue Net::HTTPExceptions, Faraday::Error::ClientError, Timeout::Error
NeverForget.log($!, tmdb_id: self.tmdb_id)
Rails.logger.warn "An HTTP error occured while trying to get data for TMDB movie #{self.tmdb_id}"
View
11 features/support/movies.rb
@@ -19,9 +19,16 @@ def find_movie(title, options = {})
# hack: make `ensure_extended_info` a no-op
def stub_extended_info(selector)
+ updated_at = 5.minutes.ago.utc
+
Movie.collection.update(selector, {
- '$set' => {rotten_tomatoes: {updated_at: 5.minutes.ago.utc}},
- '$unset' => {:tmdb_id => 1}
+ '$set' => {
+ rotten_tomatoes: {updated_at: updated_at},
+ tmdb_updated_at: updated_at,
+ runtime: 95,
+ countries: %w[Sweden],
+ directors: ["Stanley Kubrick"]
+ }
}, :multi => true, :safe => true)
end
}
View
21 spec/models/movie_spec.rb
@@ -56,6 +56,7 @@ def collection
it "movie with complete info" do
movie = Movie.create :runtime => 95, :tmdb_id => 1234,
+ :tmdb_updated_at => 1.day.ago.utc,
:countries => [], :directors => [], :homepage => "" do |m|
m['rotten_tomatoes'] = rotten_values
end
@@ -64,7 +65,25 @@ def collection
movie.ensure_extended_info
attributes.should == movie
end
-
+
+ it "movie with stale info" do
+ stub_request(:get, 'api.themoviedb.org/2.1/Movie.getInfo/en/json/TEST/1234').
+ to_return(
+ :body => read_fixture('tmdb-an_education.json'),
+ :status => 200,
+ :headers => {'content-type' => 'application/json'}
+ )
+
+ movie = Movie.create :runtime => 95, :tmdb_id => 1234,
+ :tmdb_updated_at => 2.weeks.ago.utc,
+ :countries => [], :directors => [], :homepage => "" do |m|
+ m['rotten_tomatoes'] = rotten_values
+ end
+
+ movie.ensure_extended_info
+ movie.tmdb_updated_at.should be_within(1).of(Time.now)
+ end
+
it "movie with missing info fills the blanks" do
stub_request(:get, 'api.themoviedb.org/2.1/Movie.getInfo/en/json/TEST/1234').
to_return(
Please sign in to comment.
Something went wrong with that request. Please try again.