Module: MtgDb
- Included in:
- CLI
- Defined in:
- lib/mtg_db.rb,
lib/mtg_db/cli.rb,
lib/mtg_db/models.rb,
lib/mtg_db/parsers.rb,
lib/mtg_db/version.rb,
lib/mtg_db/constants.rb,
lib/mtg_db/downloaders.rb
Overview
require ‘pry’
Defined Under Namespace
Modules: Constants, Downloaders, Models, Parsers Classes: CLI
Constant Summary collapse
- TMP_DIR =
File.join(Dir.tmpdir, 'mtg')
- ALL_CARDS_DIR =
'standard'
- DOUBLE_FACED_DIR =
'double-faced'
- SCHEMA_FILENAME =
File.join(__dir__, '..', 'sql', 'db.schema.sql')
- SQLITE3_HEADER_STRING_LENGTH =
15
- SQLITE3_HEADER_STRING =
'SQLite format 3'
- VERSION =
'1.0.3'
Class Attribute Summary collapse
-
.standard_files_downloaded ⇒ Object
Returns the value of attribute standard_files_downloaded.
Class Method Summary collapse
- .add_all_cards_to_db(db_filename, tmp_dir) ⇒ Object
-
.add_double_faced_cards_to_db(db_filename, tmp_dir) ⇒ Object
Adding Downloaded Double-Faced Cards to Db.
- .create_db(name) ⇒ Object
-
.download_all_cards(tmp_dir) ⇒ Object
Downloading NOTE: The download directory must be empty for downloads to start.
-
.download_double_faced_cards(db_filename, tmp_dir) ⇒ Object
Downloading Double-Faced Cards.
-
.is_sqlite3?(db_filename) ⇒ Boolean
Use this to test if a given filename is an sqlite3 db.
-
.mangle(db_filename) ⇒ Object
Mangle the header of an SQLite3 file.
Class Attribute Details
.standard_files_downloaded ⇒ Object
Returns the value of attribute standard_files_downloaded.
19 20 21 |
# File 'lib/mtg_db.rb', line 19 def standard_files_downloaded @standard_files_downloaded end |
Class Method Details
.add_all_cards_to_db(db_filename, tmp_dir) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mtg_db.rb', line 41 def self.add_all_cards_to_db(db_filename, tmp_dir) # Connect to db db = Sequel.sqlite(db_filename) # db.loggers << Logger.new($stdout) db.run 'PRAGMA synchronous = 0' require 'mtg_db/models' files = @standard_files_downloaded if files.nil? or files.empty? tmp_dir ||= TMP_DIR tmp_dir = File.join(tmp_dir, ALL_CARDS_DIR) files = Dir.glob(File.join(tmp_dir, '*.html')).sort end # Parsing agent = Mechanize.new agent.pluggable_parser.html = MtgDb::Parsers::GathererParser files.each do |file| filepath = File.absolute_path file uri = "file://#{filepath}" page = agent.get(uri) # uses our pluggable parser # insert each card into the db, creating records in associated tables if necessary page.cards.each do |card| puts "\tProcessing Card: #{card[:name]}" card_model = MtgDb::Models::Card.new card_model.set_fields(card, %i[name mana_cost cmc supertype subtype rules power toughness]) card_model.save # Set/Rarity a.k.a Set Version card[:set_versions].each do |set_version| set = MtgDb::Models::CardSet.find_or_create( name: set_version[:set], abbreviation: set_version[:set_abbreviation] ) rarity = MtgDb::Models::Rarity.find_or_create( name: set_version[:rarity], abbreviation: set_version[:rarity][0] ) MtgDb::Models::SetVersion.create( card: card_model, multiverse_id: set_version[:multiverse_id], card_set: set, rarity: rarity ) end # Planeswalker if card_model.is_planeswalker? MtgDb::Models::Planeswalker.create(card: card_model, loyalty: card[:loyalty]) end # Vanguard if card_model.is_vanguard? MtgDb::Models::Vanguard.create( card: card_model, hand_modifier: card[:hand_modifier], life_modifier: card[:life_modifier] ) end end end db.disconnect end |
.add_double_faced_cards_to_db(db_filename, tmp_dir) ⇒ Object
Adding Downloaded Double-Faced Cards to Db
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/mtg_db.rb', line 133 def self.add_double_faced_cards_to_db(db_filename, tmp_dir) tmp_dir ||= TMP_DIR tmp_dir = File.join(tmp_dir, DOUBLE_FACED_DIR) files = Dir.glob(File.join(tmp_dir, '*.html')).sort agent = Mechanize.new agent.pluggable_parser.html = MtgDb::Parsers::DoubleFacedCardDetailsParser # Connect to db db = Sequel.sqlite(db_filename) db.run 'PRAGMA synchronous = 0' # db.loggers << Logger.new($stdout) require 'mtg_db/models' files.each do |file| filepath = File.absolute_path file uri = "file://#{filepath}" puts uri page = agent.get(uri) # uses our pluggable parser # insert each card into the db, creating records in associated tables if necessary if page.cards.size == 2 faceup_card = MtgDb::Models::Card.where(name: page.faceup_card_name).first facedown_card = MtgDb::Models::Card.where(name: page.facedown_card_name).first puts "\tProcessing Double-Faced Card: #{faceup_card.name} <=> #{facedown_card.name}" model = MtgDb::Models::DoubleFaced.find_or_create(faceup_card: faceup_card, facedown_card: facedown_card) end end db.disconnect end |
.create_db(name) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/mtg_db.rb', line 22 def self.create_db(name) schema = File.new(SCHEMA_FILENAME).readlines.join db = Sequel.sqlite(name) db.run(schema) return db end |
.download_all_cards(tmp_dir) ⇒ Object
Downloading NOTE: The download directory must be empty for downloads to start
32 33 34 35 36 37 38 39 |
# File 'lib/mtg_db.rb', line 32 def self.download_all_cards(tmp_dir) tmp_dir ||= TMP_DIR tmp_dir = File.join(tmp_dir, ALL_CARDS_DIR) downloader = MtgDb::Downloaders::AllCardsStandardDownloader.new(output_dir: tmp_dir) downloader.start if downloader.is_empty? @standard_files_downloaded = downloader.files end |
.download_double_faced_cards(db_filename, tmp_dir) ⇒ Object
Downloading Double-Faced Cards
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/mtg_db.rb', line 111 def self.download_double_faced_cards(db_filename, tmp_dir) tmp_dir ||= TMP_DIR tmp_dir = File.join(tmp_dir, DOUBLE_FACED_DIR) downloader = MtgDb::Downloaders::CardDetailsDownloader.new(output_dir: tmp_dir) # Connect to db db = Sequel.sqlite(db_filename) # db.loggers << Logger.new($stdout) require 'mtg_db/models' cards = MtgDb::Models::Card.where(Sequel.ilike(:rules, '%transform%')).all cards.each do |card| multiverse_id = card.set_versions.first.multiverse_id puts "#{card.name}, #{multiverse_id}" downloader.start(card.name.parameterize, multiverse_id) end db.disconnect end |
.is_sqlite3?(db_filename) ⇒ Boolean
Use this to test if a given filename is an sqlite3 db
168 169 170 171 |
# File 'lib/mtg_db.rb', line 168 def self.is_sqlite3?(db_filename) header = IO.binread(db_filename, SQLITE3_HEADER_STRING_LENGTH) return (header == SQLITE3_HEADER_STRING) end |
.mangle(db_filename) ⇒ Object
Mangle the header of an SQLite3 file
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/mtg_db.rb', line 174 def self.mangle(db_filename) header = '' SQLITE3_HEADER_STRING_LENGTH.times do header += (Random.rand(26) + 48).chr end File.open(db_filename, 'r+b') do |file| file.seek(0, IO::SEEK_SET) file.print(header) end end |