Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 2 contributors
Commits on Oct 28, 2011
Gun.io Whitespace Robot Remove whitespace [Gun.io WhitespaceBot] 31ccfa0
Commits on Mar 08, 2012
Damon P. Cortesi Merge pull request #1 from GunioRobot/clean
Hi! I cleaned up your code for you!
57b8073
14 lib/mwhich/amazon.rb
View
@@ -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)
10 lib/mwhich/client.rb
View
@@ -1,6 +1,6 @@
module MWhich
class Client
-
+
VALID_SERVICES = [:amazon,:hulu,:itunes,:netflix]
SERVICE_MAP = {
:amazon => MWhich::Services::Amazon,
@@ -9,16 +9,16 @@ 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 = {}
@@ -26,7 +26,7 @@ def search(title)
s = SERVICE_MAP[service].new(:ignore => ignore)
results[service] = s.search(title)
end
-
+
results
end
end
12 lib/mwhich/hulu.rb
View
@@ -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
14 lib/mwhich/itunes.rb
View
@@ -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
16 lib/mwhich/netflix.rb
View
@@ -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

No commit comments for this range

Something went wrong with that request. Please try again.