Skip to content

Instantly share code, notes, and snippets.

@ihatov08
Last active July 11, 2019 01:18
Show Gist options
  • Save ihatov08/b29d8df9becb4d36d7f62a89065f7010 to your computer and use it in GitHub Desktop.
Save ihatov08/b29d8df9becb4d36d7f62a89065f7010 to your computer and use it in GitHub Desktop.

Revisions

  1. ihatov08 revised this gist Jul 11, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions singlecolorimage_download.rb
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ def attributes
    end

    def color_6_digest_hex
    [r, g, b].map{|i| i.to_s(16) }.join
    [r, g, b].map{|i| i.to_s(16).rjust(2, '0') }.join.upcase
    end
    end

    @@ -102,7 +102,7 @@ def image_size
    end

    def file_name
    "#{id}.png"
    "#{color_name}.png"
    end

    def file_path
    @@ -114,7 +114,7 @@ def request_uri
    end

    def donwload_dir
    download_path || FileUtils.mkdir_p(File.join("/tmp", "simple_images")).join
    download_path || FileUtils.mkdir_p(File.join("simple_images")).join
    end
    end

  2. ihatov08 revised this gist Jul 10, 2019. 1 changed file with 70 additions and 8 deletions.
    78 changes: 70 additions & 8 deletions singlecolorimage_download.rb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,42 @@
    require 'open-uri'
    require 'csv'
    require 'pry'

    class Color
    attr_reader :id, :color_name, :r, :g, :b

    def initialize(id:, color_name:, r:, g:, b:)
    @id = id
    @color_name = color_name
    @r = r
    @g = g
    @b = b
    end

    def attributes
    {
    id: id,
    color_name: color_name,
    r: r,
    g: g,
    b: b,
    color_6_digest_hex: color_6_digest_hex
    }
    end

    def color_6_digest_hex
    [r, g, b].map{|i| i.to_s(16) }.join
    end
    end

    # ref: http://www.singlecolorimage.com/api.html
    class SingleColorImageApi
    BASE_URL = "http://singlecolorimage.com/get"
    def initialize(color_6_digest_hex:, width:, height:, download_path: nil)

    def initialize(color_6_digest_hex:, color_name:, id:, width: 256, height: 256, download_path: nil)
    @color_6_digest_hex = color_6_digest_hex
    @color_name = color_name
    @id = id
    @width = width
    @height = height
    @download_path = download_path
    @@ -14,14 +45,43 @@ def initialize(color_6_digest_hex:, width:, height:, download_path: nil)
    def self.download(**args)
    new(args).download
    end

    def self.extract_csv(csv_file:)
    header_mapping =
    {
    "ID" => :id,
    "color_library Name" => :color_name,
    "R" => :r,
    "G" => :g,
    "B" => :b
    }

    header_converter = lambda { |h| header_mapping[h] }
    csv_options =
    {
    headers: :first_row,
    header_converters: header_converter,
    converters: :integer,
    skip_blanks: true
    }
    rows = CSV.read(csv_file, **csv_options)
    rows.map{ |row| Color.new(id: row[:id], color_name: row[:color_name], r: row[:r], g: row[:g], b: row[:b]) }
    end

    # CSV header must be [color_6_digest_hex, width, height]
    def self.bulk_download(csv_file:)
    colors = CSV.read(csv_file, headers: true).map{|row| row.to_h.map{|k,v| [k.to_sym, v]}.to_h}
    colors = extract_csv(csv_file: csv_file)
    colors.each do |color|
    puts "START #{color}"
    new(**color).download
    puts "FINISH #{color}"
    puts "START #{color.attributes}"
    new(
    color_6_digest_hex: color.color_6_digest_hex,
    color_name: color.color_name,
    id: color.id
    ).download
    puts "FINISH #{color.attributes}\n"
    rescue => e
    puts e
    puts color
    end
    end

    @@ -35,14 +95,14 @@ def download

    private

    attr_reader :color_6_digest_hex, :width, :height, :download_path
    attr_reader :color_6_digest_hex, :color_name, :id, :width, :height, :download_path

    def image_size
    "#{width}x#{height}"
    end

    def file_name
    "#{color_6_digest_hex}-#{image_size}.png"
    "#{id}.png"
    end

    def file_path
    @@ -61,4 +121,6 @@ def donwload_dir
    # ex
    # SingleColorImageApi.download(color_6_digest_hex: "33fd8f", width: "400", height: "100")

    SingleColorImageApi.bulk_download(csv_file: "color.csv")
    #SingleColorImageApi.bulk_download(csv_file: "color.csv")
    SingleColorImageApi.extract_csv(csv_file: "color_library.csv")
    SingleColorImageApi.download(csv_file: "color_library.csv")
  3. ihatov08 revised this gist Jul 9, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion singlecolorimage_download.rb
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,8 @@ def initialize(color_6_digest_hex:, width:, height:, download_path: nil)
    def self.download(**args)
    new(args).download
    end


    # CSV header must be [color_6_digest_hex, width, height]
    def self.bulk_download(csv_file:)
    colors = CSV.read(csv_file, headers: true).map{|row| row.to_h.map{|k,v| [k.to_sym, v]}.to_h}
    colors.each do |color|
  4. ihatov08 revised this gist Jul 9, 2019. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions singlecolorimage_download.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    require 'open-uri'
    require 'csv'
    require 'pry'
    # ref: http://www.singlecolorimage.com/api.html
    class SingleColorImageApi
    BASE_URL = "http://singlecolorimage.com/get"
    @@ -13,6 +15,15 @@ def self.download(**args)
    new(args).download
    end

    def self.bulk_download(csv_file:)
    colors = CSV.read(csv_file, headers: true).map{|row| row.to_h.map{|k,v| [k.to_sym, v]}.to_h}
    colors.each do |color|
    puts "START #{color}"
    new(**color).download
    puts "FINISH #{color}"
    end
    end

    def download
    open(file_path, 'w') do |f|
    open(request_uri) do |data|
    @@ -48,3 +59,5 @@ def donwload_dir

    # ex
    # SingleColorImageApi.download(color_6_digest_hex: "33fd8f", width: "400", height: "100")

    SingleColorImageApi.bulk_download(csv_file: "color.csv")
  5. ihatov08 created this gist Jul 9, 2019.
    50 changes: 50 additions & 0 deletions singlecolorimage_download.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    require 'open-uri'
    # ref: http://www.singlecolorimage.com/api.html
    class SingleColorImageApi
    BASE_URL = "http://singlecolorimage.com/get"
    def initialize(color_6_digest_hex:, width:, height:, download_path: nil)
    @color_6_digest_hex = color_6_digest_hex
    @width = width
    @height = height
    @download_path = download_path
    end

    def self.download(**args)
    new(args).download
    end

    def download
    open(file_path, 'w') do |f|
    open(request_uri) do |data|
    f.write(data.read)
    end
    end
    end

    private

    attr_reader :color_6_digest_hex, :width, :height, :download_path

    def image_size
    "#{width}x#{height}"
    end

    def file_name
    "#{color_6_digest_hex}-#{image_size}.png"
    end

    def file_path
    File.join(donwload_dir, file_name)
    end

    def request_uri
    URI.join("#{BASE_URL}/", "#{color_6_digest_hex}/", image_size).tap{|t| puts t}
    end

    def donwload_dir
    download_path || FileUtils.mkdir_p(File.join("/tmp", "simple_images")).join
    end
    end

    # ex
    # SingleColorImageApi.download(color_6_digest_hex: "33fd8f", width: "400", height: "100")