Class: Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_tcg_finder/scraper.rb

Constant Summary collapse

SEARCH_URL =
"https://scryfall.com/search?q="
SITE_URL =
"https://scryfall.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Scraper

Returns a new instance of Scraper.



8
9
10
# File 'lib/magic_tcg_finder/scraper.rb', line 8

def initialize(query)
	@query = query
end

Instance Attribute Details

#queryObject

Returns the value of attribute query.



6
7
8
# File 'lib/magic_tcg_finder/scraper.rb', line 6

def query
  @query
end

Class Method Details

.color_finder(cost) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/magic_tcg_finder/scraper.rb', line 51

def self.color_finder(cost)
  colors = {'{W}' =>'White'.colorize(:light_yellow),'{U}' => 'Blue'.colorize(:light_cyan),'{B}' => 'Black'.colorize(:magenta),'{R}' => 'Red'.colorize(:red),'{G}' => 'Green'.colorize(:green)}
  matches = 0
  color_identity = "Colorless".colorize(:light_black)
  #Not all cards have a casting cost
  return color_identity if cost.nil?

  colors.each do |symbol, value|
    if cost.include?(symbol)
      matches += 1
      color_identity = value
    end
  end


  if matches > 1
    "Multicolor".colorize(:yellow)
  else
    color_identity
  end
end

.scrape_card_page(profile_url) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/magic_tcg_finder/scraper.rb', line 74

def self.scrape_card_page(profile_url)
  card_hash = {}
  card_profile = Nokogiri::HTML(open(profile_url))
  title_text = card_profile.css('h1.card-text-title').text.strip.gsub(/\n/, '||').split("||")
  card_hash[:name] = title_text.first
  #Not all cards have a casting cost, such as a land... .gsub on nil makes ruby sad.
  card_hash[:cost] = title_text[1].gsub(/\s/,"") unless title_text[1] == nil
  card_hash[:rarity] = card_profile.css('span.prints-current-set-details').text.strip.split(', ')[1]
  card_hash[:sets] = card_profile.css('tbody tr').collect {|element|element.css('td').text.strip[/^.*(?=(\n))/]}.delete_if{|element|element.nil?}
  card_hash[:price] = card_profile.css('span.price.currency-usd').text.split('$')[1]
  card_hash[:color] = self.color_finder(card_hash[:cost])
  card_hash[:purchase_url] = card_profile.css('#stores > ul > li:nth-child(1) > a').attr('href').text
  card_hash[:card_type] = card_profile.css('p.card-text-type-line').text.strip
  card_hash[:combat_stats] = card_profile.css('div.card-text-stats').text.strip
  card_hash[:rules_text] = card_profile.css('div.card-text-oracle').text.strip
  card_hash[:flavor_text] = card_profile.css('div.card-text-flavor').text.strip
  card_hash.delete_if {|key, value| value == "" || value == nil}
  card_hash
end

Instance Method Details

#input_parserObject



45
46
47
48
49
# File 'lib/magic_tcg_finder/scraper.rb', line 45

def input_parser
	input = self.query.downcase.split.collect{|string|string.scan(/[a-z]/)}
	input = input.collect {|arr|arr.join("")}.join('+')
	SEARCH_URL + input
end

#scrape_search_page(url = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/magic_tcg_finder/scraper.rb', line 12

def scrape_search_page(url = nil)
	url =  self.input_parser if url.nil?
  begin 
	 card_index = Nokogiri::HTML(open(url))
  rescue
    puts "No cards found"
    return []
  end

	cards = card_index.css('a.card-grid-item')
	if cards.empty?
		[{name: 'placeholder', url: url}]
	else
		card_collection = []
		cards.each do |card|
			card_hash = {}
			card_hash[:name] = card.css('img.card').attr('alt').text.gsub(/\s\(...\)/, "")
			card_hash[:url] = SITE_URL + card['href']
			card_collection << card_hash
		end
    if card_index.css("a.button-primary.js-paginate-forward").any?
      #looks for more pages
      new_url = SITE_URL + card_index.css("a.button-primary.js-paginate-forward").attr('href').text
      card_collection << scrape_search_page(new_url)
      #Recurses if it finds more pages
      card_collection.flatten
    else
      card_collection
    end
	end

end