Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 110 lines (89 sloc) 3.401 kb
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
1 require 'nibble_endpoints'
391b9fb @mislav move TMDB & Netflix clients to Faraday
authored
2 require 'failsafe_store'
efd8083 @mislav extract and improve comparison of movie titles
authored
3 require 'movie_title'
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
4 require 'nokogiri'
9e1e646 @mislav MongoDB storage, Netflix search
authored
5
6 module Netflix
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
7 extend NibbleEndpoints
7d100d0 @mislav compensate for possibly blank user_agent string in API clients
authored
8
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
9 class ParseXml < Struct.new(:app)
10 def call(env)
11 app.call(env).on_complete do
12 env[:body] = Nokogiri::XML env[:body]
13 end
14 end
7d100d0 @mislav compensate for possibly blank user_agent string in API clients
authored
15 end
16
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
17 define_connection 'http://api.netflix.com' do |conn|
18 if user_agent = Movies::Application.config.user_agent
19 conn.headers[:user_agent] = user_agent
20 end
21
58a955e @mislav add v=1.5 parameter to Netflix client
authored
22 # http://developer.netflix.com/docs/REST_API_Conventions#0_pgfId-1009147
23 conn.params[:v] = '1.5'
24
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
25 oauth_config = Movies::Application.config.netflix
26 conn.request :oauth,
27 :consumer_key => oauth_config.consumer_key,
28 :consumer_secret => oauth_config.secret
16073cb @mislav cleanup how external configuration is set
authored
29
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
30 conn.use ParseXml
7b7c85e @mislav add caching for Netflix and TMDB requests
authored
31
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
32 if Movies::Application.config.api_caching
33 conn.response :caching do
34 FailsafeStore.new Rails.root + 'tmp/cache', :namespace => 'netflix', :expires_in => 1.day,
35 :exceptions => ['Faraday::Error::ClientError']
36 end
7b7c85e @mislav add caching for Netflix and TMDB requests
authored
37 end
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
38
39 conn.use :instrumentation
40 conn.response :raise_error
41 conn.adapter :net_http
9e1e646 @mislav MongoDB storage, Netflix search
authored
42 end
391b9fb @mislav move TMDB & Netflix clients to Faraday
authored
43
ef8b664 @mislav Scraper is now "nibbler" gem
authored
44 class Title < Nibbler
efd8083 @mislav extract and improve comparison of movie titles
authored
45 include MovieTitle
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
46
d33beef @mislav refactor and better test Netflix search
authored
47 element :id, :with => lambda { |url_node|
48 url_node.text.scan(/\d+/).last.to_i
49 }
9e1e646 @mislav MongoDB storage, Netflix search
authored
50 element './title/@regular' => :name
51 element './box_art/@small' => :poster_small
52 element './box_art/@medium' => :poster_medium
53 element './box_art/@large' => :poster_large
d33beef @mislav refactor and better test Netflix search
authored
54 element 'release_year' => :year, :with => lambda { |node| node.text.to_i }
55 element :runtime, :with => lambda { |node| node.text.to_i / 60 }
9e1e646 @mislav MongoDB storage, Netflix search
authored
56 element 'synopsis' => :synopsis
57 elements './link[@title="directors"]/people/link/@title' => :directors
58 elements './link[@title="cast"]/people/link/@title' => :cast
d33beef @mislav refactor and better test Netflix search
authored
59 element './/link[@title="web page"]/@href' => :url
9e1e646 @mislav MongoDB storage, Netflix search
authored
60 element './/link[@title="official webpage"]/@href' => :official_url
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
61
680ef8e @mislav normalize Netflix titles in form of "Movie: Special Edition"
authored
62 def name=(value)
afcf976 @mislav more movie title normalization
authored
63 if value.respond_to?(:sub)
64 value = value.sub(/(\s*:)?\s+(the movie|unrated)$/i, '')
65 @special_edition = !!value.sub!(/(\s*:)?\s+(special|collector's) edition$/i, '')
680ef8e @mislav normalize Netflix titles in form of "Movie: Special Edition"
authored
66 end
67 @name = value
68 end
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
69
680ef8e @mislav normalize Netflix titles in form of "Movie: Special Edition"
authored
70 def special_edition?
71 @special_edition
72 end
9e1e646 @mislav MongoDB storage, Netflix search
authored
73 end
391b9fb @mislav move TMDB & Netflix clients to Faraday
authored
74
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
75 endpoint(:search_titles, '/catalog/titles?{-join|&|term,max_results,start_index,expand}') do
9e1e646 @mislav MongoDB storage, Netflix search
authored
76 elements 'catalog_title' => :titles, :with => Title
77 element 'number_of_results' => :total_entries
78 element 'results_per_page' => :per_page
79 element 'start_index' => :offset
80 end
391b9fb @mislav move TMDB & Netflix clients to Faraday
authored
81
82 def self.search(query, options = {})
83 page = options[:page] || 1
84 per_page = options[:per_page] || 5
85 offset = per_page * (page.to_i - 1)
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
86
391b9fb @mislav move TMDB & Netflix clients to Faraday
authored
87 params = {:term => query, :max_results => per_page, :start_index => offset}
052922b @mislav new admin feature: link movie to Netflix
authored
88 params[:expand] = Array(options[:expand]).join(',') if options[:expand]
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
89
90 get(:search_titles, params)
391b9fb @mislav move TMDB & Netflix clients to Faraday
authored
91 end
92
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
93 endpoint(:title_details, '/catalog/titles/movies/{title_id}?{-join|&|expand}') do
052922b @mislav new admin feature: link movie to Netflix
authored
94 element 'catalog_title' => :title, :with => Title
95 end
96
97 def self.movie_info(movie_id, options = {})
98 fields = Array(options.fetch(:expand, 'synopsis'))
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
99 get(:title_details, title_id: movie_id, expand: fields.join(',')).title
052922b @mislav new admin feature: link movie to Netflix
authored
100 end
101
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
102 endpoint(:autocomplete_titles, '/catalog/titles/autocomplete?term={term}') do
9e1e646 @mislav MongoDB storage, Netflix search
authored
103 elements './/autocomplete_item/title/@short' => :titles
104 end
dddd511 @mislav refactor Netflix and Tmdb clients to use URI templates
authored
105
391b9fb @mislav move TMDB & Netflix clients to Faraday
authored
106 def self.autocomplete(term)
3bacf5b @mislav upgrade Faraday + middleware, stop using FaradayStack
authored
107 get(:autocomplete_titles, :term => term)
391b9fb @mislav move TMDB & Netflix clients to Faraday
authored
108 end
9e1e646 @mislav MongoDB storage, Netflix search
authored
109 end
Something went wrong with that request. Please try again.