Skip to content

Instantly share code, notes, and snippets.

@seki
Created May 5, 2010 16:24
Show Gist options
  • Save seki/391043 to your computer and use it in GitHub Desktop.
Save seki/391043 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'hpricot'
require 'open-uri'
require 'erb'
class PokemonCardCom
def host
'http://www.pokemon-card.com'
end
def search_by_name(name)
url = host + '/card/index.php' +
'?mode=imagelist&sm_and_keyword=true&keyword=' + ERB::Util.u(name)
open(url)
end
def card_detail(path)
open(host + path)
end
def card_summary(path)
doc = Hpricot(card_detail(path))
div = doc.at('div[@class="specData"]')
return nil unless div
spec = {}
spec[:name] = div.at('h2').inner_text
basic_data = div.at('dl[@class="basicData"]/dt')
if basic_data && basic_data.inner_text == 'LV.'
spec[:lv] = div.at('dl[@class="basicData"]/dd').inner_text
else
spec[:lv] = 'n/a'
end
spec[:series] = div.at('dl[@class="clearFix collectNo"]/dt/img')['alt']
spec[:series_name] = doc.search('ul[@class="linkList01"]/li').collect {|e|
e.inner_text
}
spec[:id] = div.at('dl[@class="clearFix collectNo"]/dt/img').next.to_html.gsub(/\&nbsp\;/, ' ').gsub(/\s/, '')
spec[:path] = path
spec
end
end
if __FILE__ == $0
name = ARGV.shift
there = PokemonCardCom.new
doc = Hpricot(there.search_by_name(name))
doc.search('ul[@class="clearFix cardList"]/li/a').each do |e|
img = e.at('img')
p there.card_summary(e['href'])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment