Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The recommendation engine is an external app to which we talk over HTTP. The resulting movie IDs are filtered down to: - movies that you haven't ignored (deleted from recommendations); - movies that you haven't watched yet; - movies that you haven't added in your to-watch list. Recommendations are cached for 24 hours. Mad props to @norbert for making the recommendation engine!
- Loading branch information
Showing
17 changed files
with
221 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'fickle' | ||
require 'forwardable' | ||
|
||
Recommendations = Struct.new(:user) do | ||
extend Forwardable | ||
def_delegators :movies, :any?, :empty?, :size | ||
|
||
def load_movies | ||
Movie.find(recommended_movie_ids) | ||
end | ||
|
||
def recommended_movie_ids | ||
@movie_ids ||= fetch_recommendations[0].map {|item| BSON::ObjectId[item[0]] } | ||
end | ||
|
||
def movies | ||
@movies ||= load_movies.reject { |movie| | ||
ignored_ids.include?(movie.id) || | ||
to_watch_ids.include?(movie.id) || | ||
watched_ids.include?(movie.id) | ||
} | ||
end | ||
|
||
def ignored_ids | ||
user.ignored_recommendations | ||
end | ||
|
||
def ignore_movie(movie) | ||
user.ignored_recommendations << movie.id | ||
end | ||
|
||
def watched_ids | ||
@watched ||= user.watched(filter_association). | ||
link_documents.map {|doc| doc['movie_id'] } | ||
end | ||
|
||
def to_watch_ids | ||
@to_watch ||= user.to_watch(filter_association). | ||
link_documents.map {|doc| doc['movie_id'] } | ||
end | ||
|
||
def filter_association | ||
{ :movie_id => { '$in' => recommended_movie_ids } } | ||
end | ||
|
||
# response: [ [ [id, rating], [id, rating], ... ] ] | ||
def fetch_recommendations | ||
Rails.cache.fetch("recommendations/#{user.id}", expires_in: 1.day) do | ||
fickle = Fickle::Client.new(fickle_url, fickle_key) | ||
fickle.recommend([user.id.to_s]) | ||
end | ||
end | ||
|
||
def fickle_url | ||
Movies::Application.config.fickle.url | ||
end | ||
|
||
def fickle_key | ||
Movies::Application.config.fickle.api_key | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<%= movie_poster(movie, :medium) %> | ||
|
||
<header> | ||
<%= render 'movies/title', :movie => movie %> | ||
<% if movie.directors.present? %> | ||
<p class="directors">by <strong><%= movie.directors.map { |name| | ||
link_to(name, director_path(name)) | ||
}.to_sentence.html_safe %></strong></p> | ||
<% end %> | ||
<% if movie.countries.present? %> | ||
<p class="countries">in <%= movie.countries.to_sentence %></p> | ||
<% end %> | ||
<%= movie_runtime(movie) %> | ||
</header> | ||
|
||
<% if movie.chosen_plot.present? %> | ||
<p class="plot"><%= movie_plot movie %></p> | ||
<% end %> | ||
<% if elsewhere = movie_elsewhere(movie).presence %> | ||
<p class="elsewhere"> | ||
Elsewhere: | ||
<%= raw elsewhere.map { |label, url, title| | ||
link_to(label, url, title: title) | ||
}.join(separator) %> | ||
</p> | ||
<% end %> | ||
<%= movie_actions(movie) %> | ||
<% if logged_in? and (stats = current_user.friends_who_watched(movie)).present? %> | ||
<div class="friends"> | ||
<span class=label>Social</span> | ||
<% for rating, label in [ [true, 'liked'], [nil, 'meh'], [false, 'disliked'] ] -%> | ||
<% people = stats.people_who_rated(rating) -%> | ||
<% names = people.map {|p| screen_name(p) } -%> | ||
<span class=<%= label %> rel=tooltip title="<%= label %>: <%= people.any?? names.to_sentence : 'none' %>"> | ||
<%= people.size %> | ||
<% if rating == true %>♥<% end %> | ||
</span> | ||
<% end -%> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<% if my_page? && @recommended && @recommended.any? %> | ||
<p class="flash notice"> | ||
Hey <%= user_friendly_name(@user) %>, we've got | ||
<%= link_to pluralize(@recommended.size, 'recommendation'), movie_recommendations_path(@user) %> | ||
for you! | ||
</p> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<% body_class 'movie-show movie-recommendations' %> | ||
|
||
<article> | ||
<header> | ||
<h1>Recommended for you</h1> | ||
</header> | ||
|
||
<% for movie in @recommended.movies %> | ||
<article class="movie"> | ||
<%= render 'movies/full_movie', movie: movie %> | ||
|
||
<p class=actions> | ||
<%= link_to "× Ignore this recommendation", [:ignore_recommendation, movie], :remote => true, :method => :put, :class => 'ignore' %> | ||
</p> | ||
</article> | ||
<% end %> | ||
|
||
<p<%= @recommended.any?? ' class=blank' : '' %>> | ||
No new recommendations as this time. Check back some other day! | ||
<br> | ||
See what the <%= link_to "people you follow are watching", timeline_path %> | ||
</p> | ||
|
||
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
3841d30
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1