Class: GamesAndRpgParadise::MagicTheGathering::DownloadFromSpielraum

Inherits:
Base
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/games/magic_the_gathering/classes/download_from_spielraum.rb

Overview

GamesAndRpgParadise::MagicTheGathering::DownloadFromSpielraum

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
REMOTE_BASE_URL =
#

REMOTE_BASE_URL

#
'https://www.spielraum.co.at/'
DEFAULT_REMOTE_URL =
#

DEFAULT_REMOTE_URL

Example URL how it may look like:

https://www.spielraum.co.at/de/magic/alliances/alliances.aspx
https://www.spielraum.co.at/de/magic/battle-for-zendikar/battle-for-zendikar-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/betrayers-of-kamigawa/betrayers-of-kamigawa.aspx
https://www.spielraum.co.at/de/magic/coldsnap/coldsnap.aspx
https://www.spielraum.co.at/de/magic/planeshift/planeshift.aspx
https://www.spielraum.co.at/de/magic/planar-chaos/planar-chaos.aspx
https://www.spielraum.co.at/de/magic/scourge/scourge.aspx
https://www.spielraum.co.at/de/magic/saviors-of-kamigawa/saviors-of-kamigawa.aspx
https://www.spielraum.co.at/de/magic/oath-of-the-gatewatch/oath-of-the-gatewatch-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/weatherlight/weatherlight.aspx
https://www.spielraum.co.at/de/magic/shadowmoor/shadowmoor.aspx
https://www.spielraum.co.at/de/magic/battle-for-zendikar/battle-for-zendikar-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/time-spiral/time-spiral.aspx
https://www.spielraum.co.at/de/magic/betrayers-of-kamigawa/betrayers-of-kamigawa.aspx
https://www.spielraum.co.at/de/magic/champions-of-kamigawa/champions-of-kamigawa.aspx
https://www.spielraum.co.at/de/magic/darksteel/darksteel.aspx
https://www.spielraum.co.at/de/magic/dragons-of-tarkir/dragons-of-tarkir-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/born-of-the-gods/born-of-the-gods-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/eventide/eventide.aspx
https://www.spielraum.co.at/de/magic/fate-reforged/fate-reforged-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/gatecrash/gatecrash-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/future-sight/future-sight.aspx
https://www.spielraum.co.at/de/magic/homelands/homelands.aspx
https://www.spielraum.co.at/de/magic/zendikar/zendikar.aspx
https://www.spielraum.co.at/de/magic/zendikar-rising/zendikar-rising-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/tempest/tempest.aspx
https://www.spielraum.co.at/de/magic/shadows-over-innistrad/shadows-over-innistrad-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/nemesis/nemesis.aspx
https://www.spielraum.co.at/de/magic/scars-of-mirrodin/scars-of-mirrodin.aspx
https://www.spielraum.co.at/de/magic/shards-of-alara/shards-of-alara.aspx
https://www.spielraum.co.at/de/magic/theros-beyond-death/theros-beyond-death-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/torment/torment.aspx
https://www.spielraum.co.at/de/magic/rise-of-the-eldrazi/rise-of-the-eldrazi.aspx
https://www.spielraum.co.at/de/magic/throne-of-eldraine/throne-of-eldraine-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/stronghold/stronghold.aspx
https://www.spielraum.co.at/de/magic/ice-age-block/ice-age-block.aspx
https://www.spielraum.co.at/de/magic/kaldheim/kaldheim-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/invasion/invasion.aspx
https://www.spielraum.co.at/de/magic/legends/legends-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/judgment/judgment.aspx
https://www.spielraum.co.at/de/magic/lorwyn/lorwyn.aspx
https://www.spielraum.co.at/de/magic/journey-into-nyx/journey-into-nyx-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/khans-of-tarkir/khans-of-tarkir-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/new-phyrexia/new-phyrexia-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/prophecy/prophecy.aspx
https://www.spielraum.co.at/de/magic/visions/visions.aspx
https://www.spielraum.co.at/de/magic/mirrodin/mirrodin.aspx
https://www.spielraum.co.at/de/magic/mirrodin-besieged/mirrodin-besieged-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/mirage/mirage.aspx
https://www.spielraum.co.at/de/magic/rivals-of-ixalan/rivals-of-ixalan-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/revised/revised-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/urzas-destiny/urzas-destiny.aspx
https://www.spielraum.co.at/de/magic/ravnica/ravnica.aspx
https://www.spielraum.co.at/de/magic/amonkhet/amonkhet-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/innistrad/innistrad-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/innistrad-midnight-hunt/innistrad-midnight-hunt-magic-the-gathering-einzelkarten.aspx
https://www.spielraum.co.at/de/magic/mercadian-masques/mercadian-masques.aspx
#
"#{REMOTE_BASE_URL}de/magic/mercadian-masques/mercadian-masques.aspx"

Constants inherited from Base

Base::LOG_DIR

Constants included from Base::Extensions::Colours

Base::Extensions::Colours::ARRAY_AVAILABLE_KONSOLE_COLOURS

Constants included from CommonExtensions

CommonExtensions::CONTROL_C_CODE, CommonExtensions::N

Instance Method Summary collapse

Methods inherited from Base

[], #convert, #ensure_that_the_log_directory_exists, #esystem, #is_png_file?, #lightgreen, #log_dir?, #orange, #palegoldenrod, #pngopt, #rev, #slateblue, #springgreen, #turquoise, #write_what_into

Methods included from Base::Extensions::Colours

ecomment, #efancy, #eparse, #forestgreen, #gold, #grey, #lightblue, #mediumseagreen, #mediumslateblue, #peru, #rev, sdir, sfancy, #sfile, simp, #teal, #yellow

Methods included from Base::Extensions::CommandlineArguments

#commandline_arguments?, #filter_away_commandline_arguments, #first_argument?, #first_non_hyphened_argument?, #set_commandline_arguments

Methods included from CommonExtensions

#cat, #cd, #cliner, #copy_file, #delete, #dirname_but_retains_the_trailing_slash, #disable_colours, #ensure_that_the_log_directory_exists, #esystem, #get_user_input, #infer_the_namespace, #is_on_roebe?, #log_dir?, #mkdir, #mkdir_then_cd_into_it, #mv, #namespace?, #opne, #project_base_directory?, #project_image_directory?, #project_yaml_directory?, #rds, #register_sigint, #remove_this_directory, #rename_file, #reset_the_internal_hash, #return_pwd, #return_today, #touch_file, #wrap, #write_what_into

Constructor Details

#initialize(i = ARGV.first, run_already = true) ⇒ DownloadFromSpielraum

#

initialize

#


167
168
169
170
171
172
173
174
# File 'lib/games_and_rpg_paradise/games/magic_the_gathering/classes/download_from_spielraum.rb', line 167

def initialize(
    i           = ARGV.first,
    run_already = true
  )
  reset
  set_remote_base_directory(i)
  run if run_already
end

Instance Method Details

#cd_into_the_log_directoryObject

#

cd_into_the_log_directory

#


214
215
216
# File 'lib/games_and_rpg_paradise/games/magic_the_gathering/classes/download_from_spielraum.rb', line 214

def cd_into_the_log_directory
  cd log_directory?
end

#download_this_remote_image(i) ⇒ Object

#

download_this_remote_image

This may use wget - or we may use ruby to download binary data. For now this will use open-uri, as this seems to work on windows as well.

#


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
369
370
371
372
373
374
375
376
377
378
# File 'lib/games_and_rpg_paradise/games/magic_the_gathering/classes/download_from_spielraum.rb', line 279

def download_this_remote_image(i)
  e 'Downloading '+::Colours.tomato(i)+' next ...'
  local_filename = File.basename(i)
  URI.open(i) { |image|
    File.open(local_filename, 'wb') { |file|
      file.write(image.read)
    }
  }
  # ======================================================================= #
  # Handle useless file names such as "5DN-002-Armed_Response.png".
  # ======================================================================= #
  if local_filename.start_with?(
      '5DN','AER','ARB',
      'ALL','AKH','ATQ',
      'APC','ARN','BFZ',
      'BOK','BNG','CSP',
      'PLS','PLC','SCG',
      'SOK','OGW','WTH',
      'SHM','MOR','LGN',
      'DIS','DRK','TSP',
      'CHK','ONS','GPT',
      'ZEN','DST','DOM',
      'AVR','CON','DKA',
      'DGM','DTK','EMN',
      'EVE','EXO','FEM',
      'FRF','GTC','GRN',
      'FUT','HML','HOU',
      'ICE','IKO','ZNR',
      'WWK','TMP','SOI',
      'SOM','MID','NEM',
      'ROE','ALA','THB',
      'ELD','TOR','WAR',
      'STH','KHM','ISD',
      'INV','LEG','STA',
      'XLN','JUD','LRW',
      'JOU','KTK','NPH',
      'PCY','VIS','MMQ',
      'MIR','MRD','MBS',
      'MMA','9ED','ODY',
      'RNA','RTR','KLD',
      'USG','ULG','THS',
      'RIX','RAV','3ED',
      'UDS',
      'zne','mid'
    )
    old_filename = local_filename.dup
    %w(
      AKH 5DN AER
      ARB ALL ATQ
      APC ARN BFZ
      BOK BNG CSP
      PLS PLC SCG
      SOK OGW WTH
      SHM MOR LGN
      DIS DRK TSP
      CHK ONS GPT
      ZEN DST DOM
      AVR CON DKA
      DGM DTK EMN
      EVE EXO FEM
      FRF GTC GRN
      FUT HML HOU
      ICE IKO ZNR
      WWK TMP SOI
      SOM MID NEM
      ROE ALA THB
      ELD TOR WAR
      STH KHM ISD
      INV LEG STA
      XLN JUD LRW
      JOU KTK NPH
      PCY VIS MMQ
      MIR MRD MBS
      MMA 9ED ODY
      RNA RTR KLD
      USG ULG THS
      RIX RAV 3ED
      UDS
      zne mid
    ).each {|this_substring_is_to_be_removed|
      # =================================================================== #
      # Note that 'zne' was found in "Zendikar Rising". No clue why the images
      # are named that way on the remote server.
      # =================================================================== #
      local_filename = local_filename.sub(/^#{this_substring_is_to_be_removed}-\d{1,3}-/,'')
    }
    unless File.exist? local_filename
      # =================================================================== #
      # Do a rename operation next.
      # =================================================================== #
      move_file(old_filename, local_filename)
    end
  end
  if File.exist? local_filename
    e 'Next optiziming the image file via pngopt, from the current '\
      'working directory called `'+sdir(return_pwd)+'`.'
    pngopt(local_filename)
    ImageParadise::Optimizer.new(local_filename)
  end
end

#opnnObject

#

opnn

#


207
208
209
# File 'lib/games_and_rpg_paradise/games/magic_the_gathering/classes/download_from_spielraum.rb', line 207

def opnn
  super(NAMESPACE)
end

#resetObject

#

ensure

#


200
201
202
# File 'lib/games_and_rpg_paradise/games/magic_the_gathering/classes/download_from_spielraum.rb', line 200

def reset
  ensure_that_the_log_directory_exists
end

#runObject

#

run

#


221
222
223
224
225
226
227
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
262
263
264
265
266
267
268
# File 'lib/games_and_rpg_paradise/games/magic_the_gathering/classes/download_from_spielraum.rb', line 221

def run
  cd_into_the_log_directory
  _ = @remote_base_directory
  into = 'downloaded_magic_cards_from_spielraum.html'
  dataset = URI.open(_).read
  e ::Colours.rev+'Working on the following remote webpage '\
    'next: '+::Colours.royalblue(_)
  e 'First storing the remote .html dump into '+sfile(into)+'.'
  write_what_into(dataset, into)
  new_directory = File.basename(File.dirname(_)).tr('-','_') # This will now be "fifth-dawn" or similar.
  new_directory << '/' unless new_directory.end_with?('/')
  new_directory = File.absolute_path(new_directory)+'/'
  # ======================================================================= #
  # Ad-hoc fix the ice age block next:
  # ======================================================================= #
  if new_directory.end_with? '_block' # This has to be done due to the remote URL being faulty.
    new_directory.delete_suffix!('_block')
  end
  mkdir(new_directory) unless File.directory?(new_directory)
  cd new_directory
  e 'Entering the directory `'+sdir(new_directory)+'` next.'
  first_regex = /<td><a href="(.+)">\d{1,3}/ # See: https://rubular.com/r/jCap10wR7gsTDF
  scanned = dataset.scan(first_regex).flatten
  second_regex = /<img src="([A-Za-z0-9\-_.\/]+)"\s*alt=/ # See: https://rubular.com/r/77aollIvNi5Lqh
  # ======================================================================= #
  # Now we have to get the remote URLs for each individual image.
  # ======================================================================= #
  scanned.each {|this_individual_URL|
    if this_individual_URL.start_with? '/'
      this_individual_URL[0,1] = ''
    end
    new_URL = REMOTE_BASE_URL+this_individual_URL
    new_dataset = URI.open(new_URL).read
    scanned_result = new_dataset.scan(second_regex).flatten.first
    # ===================================================================== #
    # scanned_result is now a String such as:
    #
    #   "/Images/Einzelkarten/Magic/5DN/5DN-011-Loxodon_Stalwart.png"
    #
    # ===================================================================== #
    scanned_result[0,1] = '' if scanned_result.start_with?('/')
    final_URL_to_the_remote_image = REMOTE_BASE_URL+scanned_result
    download_this_remote_image(final_URL_to_the_remote_image)
    duration = 3 + (1..10).to_a.sample
    e 'Sleeping for '+duration.to_f.to_s+' seconds next ...'
    sleep(duration) # Random sleeping so it isn't too spammy for the remote host.
  }
end

#set_remote_base_directory(i = DEFAULT_REMOTE_URL) ⇒ Object

#

set_remote_base_directory

#


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/games_and_rpg_paradise/games/magic_the_gathering/classes/download_from_spielraum.rb', line 179

def set_remote_base_directory(
    i = DEFAULT_REMOTE_URL
  )
  if i.is_a? Array
    if i.empty?
      i = DEFAULT_REMOTE_URL
    else
      i = i.join(' ').strip
    end
  end
  case i
  when nil,
       :default
    i = 'https://www.spielraum.co.at/de/magic/fifth-dawn/fifth-dawn.aspx' 
  end
  @remote_base_directory = i
end