Module: CockatriceFeeder

Defined in:
lib/cockatrice_feeder.rb

Constant Summary collapse

@@app_dir =
Dir.pwd+"/"
@@deck_dir =
@@app_dir+"decks/"
@@meta_dir =
@@app_dir+"meta/"

Class Method Summary collapse

Class Method Details

.app_dirObject



16
17
18
# File 'lib/cockatrice_feeder.rb', line 16

def self.app_dir
  @@app_dir
end

.bannedObject



145
146
147
# File 'lib/cockatrice_feeder.rb', line 145

def self.banned
  JSON.parse(File.read(@@meta_dir+"banned.json"))
end

.commandersObject



141
142
143
# File 'lib/cockatrice_feeder.rb', line 141

def self.commanders
  JSON.parse(File.read(@@meta_dir+"commanders.json"))
end

.deck_dirObject



24
25
26
# File 'lib/cockatrice_feeder.rb', line 24

def self.deck_dir
  @@deck_dir
end

.deckstats_deckObject



287
288
289
# File 'lib/cockatrice_feeder.rb', line 287

def self.deckstats_deck

end

.deckstats_decklistObject



283
284
285
# File 'lib/cockatrice_feeder.rb', line 283

def self.deckstats_decklist

end

.edhrecavg_deck(deck) ⇒ Object



276
277
278
279
280
281
# File 'lib/cockatrice_feeder.rb', line 276

def self.edhrecavg_deck(deck)
  deck[:cardlist] = JSON.parse(HTTParty.get(deck[:link]).body)["description"].
    split("</a>").last.split("\n").select{|s| s != ""}

  output_cod(deck,"edhrecavg")
end

.edhrecavg_decklistObject



263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/cockatrice_feeder.rb', line 263

def self.edhrecavg_decklist
  commanders.map{|c| c["link"]}.map do |c|
    {
      name: c,
      commanders: [c],
      link: "https://edhrec-json.s3.amazonaws.com/en/decks/#{c}.json",
      price: nil,
      date: nil,
      cardlist: []
    }
  end
end

.gobbleObject



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/cockatrice_feeder.rb', line 337

def self.gobble
  setup()

  total_decks = 0

  puts "Fetching the average deck for every commander on EDHREC"
  decks = CockatriceFeeder.edhrecavg_decklist
  puts "#{decks.length} decks found."
  decks.each {|d|
    CockatriceFeeder.edhrecavg_deck(d)
    total_decks += 1
  }

  puts "Fetching the most recently updated ranked decks from tappedout"
  decks = CockatriceFeeder.tappedout_decklist(1..10)
  puts "#{decks.length} decks found."
  decks.each {|d|
    CockatriceFeeder.tappedout_deck(d)
    total_decks += 1
  }

  puts "Fetching the first 10 pages of decks at https://mtgdecks.net/Commander/decklists/"
  decks = CockatriceFeeder.mtgdecks_decklist(1..10)
  puts "#{decks.length} decks found."
  decks.each {|d|
    CockatriceFeeder.mtgdecks_deck(d)
    total_decks += 1
  }

  puts "#{total_decks} decks created."
  puts "Scraw!"
end

.meta_dirObject



32
33
34
# File 'lib/cockatrice_feeder.rb', line 32

def self.meta_dir
  @@meta_dir
end

.mtgdecks_deck(deck) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/cockatrice_feeder.rb', line 315

def self.mtgdecks_deck(deck)
  puts deck[:link]
  doc = Nokogiri::HTML(HTTParty.get(deck[:link]).body)

  cardlist = []
  doc.css(".cardItem").each do |card|
    cardlist << "#{card.attribute("data-required").value} #{card.attribute("data-card-id").value}"
  end
  deck[:cardlist] = cardlist

  commanders = []
  doc.css(".breadcrumbs a").each do |a|
    href = a.attribute("href").value
    if href.include?("/Commander/")
      commanders << href.split("/").last
    end
  end
  deck[:commanders] = commanders

  output_cod(deck,"mtgdecks")
end

.mtgdecks_decklist(pages = (1..1)) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/cockatrice_feeder.rb', line 291

def self.mtgdecks_decklist(pages = (1..1))
  decks = []
  pages.each do |page|
    puts "https://mtgdecks.net/Commander/decklists/page:#{page}"
    doc = Nokogiri::HTML(HTTParty.get("https://mtgdecks.net/Commander/decklists/page:#{page}").body)
    doc.css(".decks tr.previewable").each do |r|
      if r.css("td")[0].css(".label-danger").length == 0
        decks << {
          name: "", # r.css("td")[1].css("a")[0].content,
          link: "https://mtgdecks.net"+r.css("td")[1].css("a")[0].attribute("href").value,
          date: r.css("td")[6].css("strong")[0].content.
            gsub("<span class=\"hidden-xs\">","").
            gsub("</span>","").gsub(/\s+/, ""),
          price: r.css("td")[7].css("span.paper")[0].content.gsub("$","").gsub(/\s+/, ""),
          commanders: [],
          cardlist: []
        }
      end
    end
  end

  decks
end

.output_cod(deck, subfolder) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/cockatrice_feeder.rb', line 154

def self.output_cod(deck, subfolder)
  comments = [
    deck[:name],
    deck[:link],
    deck[:commanders].join("\n"),
    deck[:price]
  ].join("\n")


  filename = [
    deck[:commanders].join("-and-"),
    deck[:name],
    deck[:price],
    subfolder
  ].compact.reject(&:empty?).uniq.join('_')

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.cockatrice_deck(:version => "1"){
      xml.deckname {
        xml.text(deck[:name])
      }
      xml.comments {
        xml.text(comments)
      }
      xml.zone(:name => "main") {
        deck[:cardlist].each do |crd|
          xml.card(
            :number => crd.split(" ").first,
            :name => crd.split(" ")[1..-1].join(" ")
          )
        end
      }
    }
  end

  File.open(@@deck_dir+"#{subfolder}/#{filename}.cod", "wb") do |file|
    file.write(builder.to_xml)
  end

  puts "created deck at #{@@deck_dir+"#{subfolder}/#{filename}.cod"}"
end

.set_app_dir(dir) ⇒ Object



10
11
12
13
14
# File 'lib/cockatrice_feeder.rb', line 10

def self.set_app_dir(dir)
  @@app_dir = (dir + (dir[-1] != "/" ? "/" : ""))
  @@deck_dir = @@app_dir+"decks/"
  @@meta_dir = @@app_dir+"meta/"
end

.set_deck_dir(dir) ⇒ Object



20
21
22
# File 'lib/cockatrice_feeder.rb', line 20

def self.set_deck_dir(dir)
  @@deck_dir = (dir + (dir[-1] != "/" ? "/" : ""))
end

.set_meta_dir(dir) ⇒ Object



28
29
30
# File 'lib/cockatrice_feeder.rb', line 28

def self.set_meta_dir(dir)
  @@meta_dir = (dir + (dir[-1] != "/" ? "/" : ""))
end

.setupObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cockatrice_feeder.rb', line 36

def self.setup
  unless File.directory?(@@meta_dir)
    Dir.mkdir(@@meta_dir)
    puts "Creating a folder at '#{@@meta_dir}' for storing meta data."
    puts "Fetching meta data."
    update_commanders()
    update_banned()
    update_commander_tiers()
  end

  unless File.directory?(@@deck_dir)
    Dir.mkdir(@@deck_dir)
    puts "Creating a folder at '#{@@deck_dir}' for generated decks."

    folders = %w(edhrecavg mtgdecks tappedout deckstats)

    folders.each do |folder|
      unless File.directory?(@@deck_dir+folder)
        Dir.mkdir(@@deck_dir+folder)
        puts "Creating a deck subfolder at '#{@@deck_dir+folder}'."
      end
    end
  end

  puts "Ready to scraw!"
end

.tappedout_deck(deck) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/cockatrice_feeder.rb', line 228

def self.tappedout_deck(deck)
  doc2 = Nokogiri::HTML(HTTParty.get(deck[:link]).body)

  deck[:cardlist] = doc2.css("#mtga-textarea").first.content.
    split("\n").select{|c| c != ""}.
    map{|c| c.split("(").first.strip}

  commanders = []
  doc2.css(".board-container .board-col").each do |col|
    col.css("h3").each_with_index do |h,i|
      if h.content.include?("Commander")
        col.css("ul")[i].css("a.card-hover").each do |a|
          if a.css("img.commander-img").length > 0
            commanders << a.attribute("href").value.split("/").last.gsub("-foil","")
          end
        end
      end
    end
  end
  deck[:commanders] = commanders

  price = nil
  doc2.css("form").each do |f|
    fname = f.attribute("name")
    if !fname.nil? && fname.value == "ck_checkout"
      price = f.css("span.pull-right").first.content.split(" - ").first.
        strip.gsub("$","").split(".").first
    end
  end
  deck[:price] = price

  subfolder = "tappedout"
  output_cod(deck, subfolder)
end

.tappedout_decklist(pages = (1..10), order_by = "-date_updated", price_min = "", price_max = "") ⇒ Object

ordering -rating -date_updated -competitive_score



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/cockatrice_feeder.rb', line 201

def self.tappedout_decklist(pages = (1..10), order_by = "-date_updated", price_min = "", price_max = "")
  decks = []
  pages.each do |page|
    doc = Nokogiri::HTML(
      HTTParty.get(
        "https://tappedout.net/mtg-decks/search/?q=&format=edh&is_top=on&price_min=#{price_min}&price_max=#{price_max}&o=#{order_by}&submit=Filter+results&p=#{page}&page=#{page}"
      ).body
    )

    doc.css(".deck-wide-header a").each do |a|
      link = a.attribute("href").value
      if link.include?("/mtg-decks/")
        decks << {
          name: link.split("/").last,
          commanders: [],
          link: "https://tappedout.net"+link,
          date: nil,
          price: nil,
          cardlist: []
        }
      end
    end
  end

  decks
end

.tiersObject

names = banned.map{|c| c}.uniq.sort



150
151
152
# File 'lib/cockatrice_feeder.rb', line 150

def self.tiers
  JSON.parse(File.read(@@meta_dir+"tiers.json"))
end

.update_bannedObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cockatrice_feeder.rb', line 96

def self.update_banned
  puts "Downloading the current EDH banned card list."
  banned_list = []

  more_pages = true
  page = 1
  while(more_pages)
    url = "https://api.magicthegathering.io/v1/cards?gameFormat=Commander&legality=Banned&page=#{page}"
    cards = JSON.parse(HTTParty.get(url).body)["cards"]
    banned_list.concat(cards)
    page += 1
    if cards.length == 0
      more_pages = false
    end
  end

  File.open(@@meta_dir+"banned.json", "wb") do |file|
    file.write(banned_list.to_json)
  end
end

.update_commander_tiersObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cockatrice_feeder.rb', line 117

def self.update_commander_tiers
  puts "Downloading a tappedout deck that ranks EDG commanders into tiers."
  doc = Nokogiri::HTML(HTTParty.get("https://tappedout.net/mtg-decks/list-multiplayer-edh-generals-by-tier/").body)

  tiers = {}
  doc.css(".board-container .board-col").each do |col|
    col.css("h3").each_with_index do |h,i|
      tier = h.content.split(")").first.gsub("(","")

      tiers[tier] = []
      col.css("ul")[i].css("a.card-hover").each do |a|
        tiers[tier] << a.attribute("href").value.split("/").last.gsub("-foil","").downcase
      end

    end
  end

  File.open(@@meta_dir+"tiers.json", "wb") do |file|
    file.write(tiers.to_json)
  end

  tiers
end

.update_commandersObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cockatrice_feeder.rb', line 63

def self.update_commanders
  puts "Downloading a list of all commanderse from EDHREC."
  commander_cids = %w(
    w g r u b
    wu ub br rg gw wb ur bg rw gu
    wub ubr brg rgw gwu wbg urw bgu rwb gur
    wubr ubrg brgw rgwu gwub
    wubrg
  )

  commanders = []

  commander_cids.each do |cid|
    url = "https://edhrec-json.s3.amazonaws.com/en/commanders/#{cid}.json"
    commanders.concat(
      JSON.parse(
        HTTParty.get(url).body
      )["container"]["json_dict"]["cardlists"].first["cardviews"].map{|c|
        {
          link: c["sanitized"],
          name: c["names"].first,
          color_identity: cid,
          deckstats_uri: c["deckstats_uri"]
        }
      }
    )
  end

  File.open(@@meta_dir+"commanders.json", "wb") do |file|
    file.write(commanders.to_json)
  end
end