Class: Collection
- Inherits:
-
Object
- Object
- Collection
- Defined in:
- lib/artbase/collection/opensea.rb
Overview
todo/check - change to OpenseaCollection or such - why? why not?
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Class Method Summary collapse
-
.download_images(range, collection, original: false) ⇒ Object
private (static) helpers.
- .download_meta(range, collection) ⇒ Object
Instance Method Summary collapse
- #_do_meta_slugify(meta_slugify, meta, index) ⇒ Object
- #_image_pixelate(img) ⇒ Object
- #_meta_slugify(meta, index) ⇒ Object
- #_meta_slugify_match(regex, meta, index) ⇒ Object
-
#_parse_dimension(str) ⇒ Object
e.g.
- #download(range = (0...@count)) ⇒ Object
- #download_images(range = (0...@count)) ⇒ Object
- #download_meta(range = (0...@count)) ⇒ Object
- #each_meta(range = (0...@count), exclude: true, &blk) ⇒ Object
-
#initialize(slug, count, meta_slugify: nil, image_pixelate: nil, patch: nil, exclude: [], format:, source:) ⇒ Collection
constructor
check: rename count to items or such - why? why not? default format to '24x24' - why? why not?.
- #pixelate(range = (0...@count)) ⇒ Object
Constructor Details
#initialize(slug, count, meta_slugify: nil, image_pixelate: nil, patch: nil, exclude: [], format:, source:) ⇒ Collection
check: rename count to items or such - why? why not?
default format to '24x24' - why? why not?
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/artbase/collection/opensea.rb', line 9 def initialize( slug, count, meta_slugify: nil, image_pixelate: nil, patch: nil, exclude: [], format:, source: ) @slug = slug @count = count @meta_slugify = @image_pixelate = image_pixelate @patch = patch @exclude = exclude @width, @height = _parse_dimension( format ) ## note: allow multiple source formats / dimensions ### e.g. convert 512x512 into [ [512,512] ] ## source = [source] unless source.is_a?( Array ) @sources = source.map { |dimension| _parse_dimension( dimension ) } end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
5 6 7 |
# File 'lib/artbase/collection/opensea.rb', line 5 def count @count end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
5 6 7 |
# File 'lib/artbase/collection/opensea.rb', line 5 def slug @slug end |
Class Method Details
.download_images(range, collection, original: false) ⇒ Object
private (static) helpers
210 211 212 213 214 215 216 217 218 219 220 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 |
# File 'lib/artbase/collection/opensea.rb', line 210 def self.download_images( range, collection, original: false ) start = Time.now delay_in_s = 0.3 range.each do |offset| = OpenSea::Meta.read( "./#{collection}/meta/#{offset}.json" ) puts "==> #{offset}.json - #{.name}" image_src = if original .image_original_url else .image_url end puts " >#{image_src}<" if image_src.nil? puts "!! ERROR - no image url found (use original: #{original}):" pp exit 1 end ## note: use a different directory to avoid size confusion!!! img_slug = if original 'i_org' else 'i' end ## note: will auto-add format file extension (e.g. .png, .jpg) ## depending on http content type!!!!! copy_image( image_src, "./#{collection}/#{img_slug}/#{offset}" ) stop = Time.now diff = stop - start mins = diff / 60 ## todo - use floor or such? secs = diff % 60 puts "up #{mins} mins #{secs} secs (total #{diff} secs)" puts "sleeping #{delay_in_s}s..." sleep( delay_in_s ) end end |
.download_meta(range, collection) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/artbase/collection/opensea.rb', line 257 def self.( range, collection ) start = Time.now delay_in_s = 0.3 range.each do |offset| dest = "./#{collection}/meta/#{offset}.json" = nil puts "==> #{offset} / #{collection} (#{dest})..." data = OpenSea.assets( collection: collection, offset: offset ) = OpenSea::Meta.new( data ) puts " name: >#{.name}<" puts " image_url: >#{.image_url}<" ## make sure path exists dirname = File.dirname( dest ) FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname ) File.open( dest, "w:utf-8" ) do |f| f.write( JSON.pretty_generate( data ) ) end stop = Time.now diff = stop - start mins = diff / 60 ## todo - use floor or such? secs = diff % 60 puts "up #{mins} mins #{secs} secs (total #{diff} secs)" puts " sleeping #{delay_in_s}s..." sleep( delay_in_s ) end end |
Instance Method Details
#_do_meta_slugify(meta_slugify, meta, index) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/artbase/collection/opensea.rb', line 112 def ( , , index ) if .is_a?( Regexp ) ( , , index ) elsif .is_a?( Proc ) .call( , index ) else raise ArgumentError, "meta_slugify - unsupported type: #{.class.name}" end end |
#_image_pixelate(img) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/artbase/collection/opensea.rb', line 42 def _image_pixelate( img ) if @image_pixelate @image_pixelate.call( img ) else @sources.each do |source_width, source_height| if img.width == source_width && img.height == source_height from = "#{source_width}x#{source_height}" to = "#{@width}x#{@height}" steps = (Image::DOwNSAMPLING_STEPS[ to ] || {})[ from ] if steps.nil? puts "!! ERROR - no sampling steps defined for #{from} to #{to}; sorry" exit 1 end return img.pixelate( steps ) end end puts "!! ERROR - unknown image dimension #{img.width}x#{img.height}; sorry" puts " supported source dimensions include: #{@sources.inspect}" exit 1 end end |
#_meta_slugify(meta, index) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/artbase/collection/opensea.rb', line 123 def ( , index ) slug = nil if @meta_slugify.is_a?( Array ) @meta_slugify.each do || slug = ( , , index ) return slug if slug ## note: short-circuit on first match ## use break instead of return - why? why not? end else ## assume object e.g. Regexp, Proc, etc. slug = ( @meta_slugify, , index ) end ## do nothing if slug.nil? puts "!! ERROR - cannot find id in >#{.name}<:" pp exit 1 end slug end |
#_meta_slugify_match(regex, meta, index) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/artbase/collection/opensea.rb', line 86 def ( regex, , index ) if m=regex.match( .name ) captures = m.named_captures ## get named captures in match data as hash (keys as strings) # e.g. #=> {"num"=>"3"} #=> {"num"=>"498", "name"=>"Doge"} pp captures num = captures['num'] ? captures['num'].to_i( 10 ) : nil ## note: add base 10 (e.g. 015=>15) name = captures['name'] ? captures['name'].strip : nil slug = '' if num slug << "%06d" % num ## todo/check: always fill/zero-pad with six 000000's - why? why not? end if name slug << "-" if num ## add separator slug << slugify( name ) end slug else nil ## note: return nil if no match / slug end end |
#_parse_dimension(str) ⇒ Object
e.g. convert dimension (width x height) “24x24” or “24 x 24” to [24,24]
37 38 39 |
# File 'lib/artbase/collection/opensea.rb', line 37 def _parse_dimension( str ) str.split( /x/i ).map { |str| str.strip.to_i } end |
#download(range = (0...@count)) ⇒ Object
77 78 79 80 |
# File 'lib/artbase/collection/opensea.rb', line 77 def download( range=(0...@count) ) ( range ) download_images( range ) end |
#download_images(range = (0...@count)) ⇒ Object
73 74 75 |
# File 'lib/artbase/collection/opensea.rb', line 73 def download_images( range=(0...@count) ) self.class.download_images( range, @slug ) end |
#download_meta(range = (0...@count)) ⇒ Object
69 70 71 |
# File 'lib/artbase/collection/opensea.rb', line 69 def ( range=(0...@count) ) self.class.( range, @slug ) end |
#each_meta(range = (0...@count), exclude: true, &blk) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/artbase/collection/opensea.rb', line 148 def ( range=(0...@count), exclude: true, &blk ) range.each do |id| ## todo/fix: change id to index = OpenSea::Meta.read( "./#{@slug}/meta/#{id}.json" ) #### # filter out/skip if exclude && @exclude.include?( .name ) puts " skipping / exclude #{id} >#{.name}<..." next end blk.call( , id ) end end |
#pixelate(range = (0...@count)) ⇒ Object
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 195 196 197 198 199 200 201 202 |
# File 'lib/artbase/collection/opensea.rb', line 167 def pixelate( range=(0...@count) ) = Hash.new( 0 ) ## deduplicate (auto-add counter if duplicate) ### todo/fix: must read slugs starting at 0 ### to work for deduplicate!!!!!! range.each do |id| = OpenSea::Meta.read( "./#{@slug}/meta/#{id}.json" ) #### # filter out/skip if @exclude.include?( .name ) puts " skipping / exclude #{id} >#{.name}<..." next end puts .name = ( , id ) count = [ ] += 1 = "#{}_(#{count})" if count > 1 img = Image.read( "./#{@slug}/i/#{id}.png" ) pix = _image_pixelate( img ) path = "./#{@slug}/ii/#{}.png" puts " saving to >#{path}<..." pix.save( path ) end end |