Permalink
Browse files

Parsing page result

  • Loading branch information...
1 parent 02a7413 commit 670202cadbad4132962f61d78a1e38702a5ce6dc @sam-wan sam-wan committed Apr 16, 2008
Showing with 15 additions and 1 deletion.
  1. +15 −1 lib/itunes_link_maker.rb
@@ -1,5 +1,7 @@
+require 'rubygems'
require 'open-uri'
require 'cgi'
+require 'hpricot'
require File.join(File.dirname(__FILE__), 'result')
@@ -13,14 +15,26 @@ class ItunesLinkMaker
SEARCH_URL = "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/itmsSearch?WOURLEncoding=ISO8859_1&lang=1&output=lm"
+ TYPE_INDICES = { 0 => :name, 1 => :album, 2 => :artist }
+
def self.search(query, media='music', country='US')
html = get_html(query, media, country)
parse_html(html)
end
private
def self.parse_html(html)
- [Result.new]
+ doc = Hpricot(html)
+
+ result = []
+ (doc/'a.searchResults').each_with_index do |element, i|
+ name = element.inner_text.strip
+ type = TYPE_INDICES[i]
+ url = element['href']
+
+ result << Result.new(name, type, url)
+ end
+ result
end
def self.get_html(query, media, country)

0 comments on commit 670202c

Please sign in to comment.