Permalink
Browse files

Merge pull request #1 from GunioRobot/clean

Hi! I cleaned up your code for you!
  • Loading branch information...
2 parents d217520 + 31ccfa0 commit 57b8073596197d1f21ac028c62c4e193933e12e3 @dacort committed Mar 8, 2012
Showing with 33 additions and 33 deletions.
  1. +7 −7 lib/mwhich/amazon.rb
  2. +5 −5 lib/mwhich/client.rb
  3. +6 −6 lib/mwhich/hulu.rb
  4. +7 −7 lib/mwhich/itunes.rb
  5. +8 −8 lib/mwhich/netflix.rb
View
14 lib/mwhich/amazon.rb
@@ -6,20 +6,20 @@ def initialize(options={})
@aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
@aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
end
-
+
def search(title)
results = request(title)
-
+
titles = []
results.css("Item").each do |result|
titles << "#{result.css('ProductGroup').inner_html}: #{result.css('Title').inner_html}"
end
-
+
titles
end
-
+
protected
-
+
def request(title)
params = {
"Operation" => "ItemSearch",
@@ -32,11 +32,11 @@ def request(title)
sorted_params = params.sort_by{|x,y| x}.map{|x,y| "#{x}=#{CGI::escape(y)}"}.join('&')
signature = sign("GET\nwebservices.amazon.com\n/onca/xml\n#{sorted_params}").strip
url = "#{@endpoint_url}?#{sorted_params}&Signature=#{CGI::escape(signature)}"
-
+
response = Net::HTTP.get_response(URI.parse(url))
Nokogiri::XML(response.body)
end
-
+
def sign(string)
hmac = HMAC::SHA256.new(@aws_secret_access_key)
hmac.update(string)
View
10 lib/mwhich/client.rb
@@ -1,6 +1,6 @@
module MWhich
class Client
-
+
VALID_SERVICES = [:amazon,:hulu,:itunes,:netflix]
SERVICE_MAP = {
:amazon => MWhich::Services::Amazon,
@@ -9,24 +9,24 @@ class Client
:netflix => MWhich::Services::Netflix
}
IGNORE_MEDIA = [:clips, :trailers]
-
+
attr_accessor :services, :ignore
-
+
# Arguments (all are optional):
# - :services - One or more of :amazon, :netflix, :hulu, or :itunes. Default is :all
def initialize(options={})
self.services = options[:services] || VALID_SERVICES
self.ignore = options[:ignore] ? IGNORE_MEDIA & options[:ignore] : IGNORE_MEDIA
end
-
+
# Search for a movie/tv show by title
def search(title)
results = {}
self.services.each do |service|
s = SERVICE_MAP[service].new(:ignore => ignore)
results[service] = s.search(title)
end
-
+
results
end
end
View
12 lib/mwhich/hulu.rb
@@ -9,10 +9,10 @@ def initialize(options={})
@ignore_media << 'clip' if options[:ignore].include?:clips
end
end
-
+
def search(title)
results = request(title)
-
+
titles = []
results.xpath("//video").each do |result|
next if @ignore_media.include?result.css('video-type').inner_html
@@ -22,15 +22,15 @@ def search(title)
end
titles << "#{result.css('video-type').inner_html}: #{result.css('title').inner_html}#{append}"
end
-
+
titles
end
-
+
protected
-
+
def request(title)
url = "#{@endpoint_url}/search?dp_identifier=hulu&query=#{URI::escape(title)}&items_per_page=10&page=1"
-
+
response = Net::HTTP.get_response(URI.parse(url))
Nokogiri::XML(response.body)
end
View
14 lib/mwhich/itunes.rb
@@ -4,20 +4,20 @@ class ITunes
def initialize(options={})
@endpoint = "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch"
end
-
+
def search(title)
results = request(title)
-
+
titles = []
results.each do |result|
titles << "#{result['kind']}: #{result['trackName']} ($#{result['trackPrice']})"
end
-
+
titles
end
-
+
protected
-
+
def request(title)
# We'll do searches across both TV and movies and merge the results
results = []
@@ -27,10 +27,10 @@ def request(title)
data = Yajl::Parser.parse(response.body)
results |= data['results']
end
-
+
results
end
-
+
end
end
end
View
16 lib/mwhich/netflix.rb
@@ -1,33 +1,33 @@
module MWhich
module Services
class Netflix
-
+
def initialize(options={})
@endpoint = "http://odata.netflix.com/v1/Catalog/"
@format = "json"
end
-
+
def search(title)
results = request(title)
-
+
titles = []
results['d']['results'].each do |result|
titles << "#{result['Type']}: #{result['Name']}#{result['Instant']['Available'] ? ' - Watch now!' : ''}"
end
-
+
titles
end
-
+
protected
-
+
def request(title)
filter = "Name eq '#{title}'"
url = "#{@endpoint}Titles?$filter=#{URI::escape(filter)}&$format=#{@format}"
-
+
response = Net::HTTP.get_response(URI.parse(url))
Yajl::Parser.parse(response.body)
end
-
+
end
end
end

0 comments on commit 57b8073

Please sign in to comment.