<commit_before>require 'net/http'

module Spontaneous::Utils
  class SmushIt
    def self.smush!(path)
      self.new(path).smush!
    end
    attr_reader :src_file

    def initialize(file_path, format = nil)
      @src_file = file_path
      @format = format
    end

    # Smush the source file in-place
    # In development mode the file isn't available under a public url for smushit to download
    # so we just skip it completely
    def smush!
      p src_file
      return if ::Spontaneous.development?
      ext = @format.nil? ? File.extname(src_file) : ".#{@format}"
      id = [Time.now.to_i, Time.now.usec].join('-')
      public_filepath = File.join(public_path, "%s-smush%s" % [id, ext])
      p public_filepath
      FileUtils.cp(src_file, public_filepath)
      filename = File.basename(public_filepath)
      url = Site.public_url(public_url(filename))
      p url
      path = "/ysmush.it/ws.php?img=#{::Rack::Utils.escape(url)}&task=#{id}&id=paste1"
      # resp, data = http.get(path, nil)
      data = Net::HTTP.get('www.smushit.com', path)
      data = Spontaneous::JSON.parse(data)
      p data

      if data[:dest]
        smushed = URI(data[:dest])<commit_msg>Pass the image format to the smush command (as the tempfiles have no extension)<commit_after>require 'net/http'

module Spontaneous::Utils
  class SmushIt
    def self.smush!(path, format = nil)
      self.new(path, format).smush!
    end
    attr_reader :src_file

    def initialize(file_path, format = nil)
      @src_file = file_path
      @format = format
    end

    # Smush the source file in-place
    # In development mode the file isn't available under a public url for smushit to download
    # so we just skip it completely
    def smush!
      p src_file
      return if ::Spontaneous.development?
      ext = @format.nil? ? File.extname(src_file) : ".#{@format}"
      id = [Time.now.to_i, Time.now.usec].join('-')
      public_filepath = File.join(public_path, "%s-smush%s" % [id, ext])
      p public_filepath
      FileUtils.cp(src_file, public_filepath)
      filename = File.basename(public_filepath)
      url = Site.public_url(public_url(filename))
      p url
      path = "/ysmush.it/ws.php?img=#{::Rack::Utils.escape(url)}&task=#{id}&id=paste1"
      # resp, data = http.get(path, nil)
      data = Net::HTTP.get('www.smushit.com', path)
      data = Spontaneous::JSON.parse(data)
      p data

      if data[:dest]
        smushed = URI(data[:dest])
{'fraction_non_alphanumeric': 0.12632006498781478, 'fraction_numerical': 0.0008123476848090983, 'mean_word_length': 2.9095238095238094, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5755113', 'n_tokens_mistral': 840, 'n_tokens_neox': 828, 'n_words': 201}