<commit_before>require 'rubygems'
require 'paperclip'
require 'active_record'
require 'action_view'
require 'google-search'
require 'mechanize'
require 'open-uri'


Paperclip.configure
Paperclip::Railtie.insert

module SexItUp

  class SexItUpImage < ActiveRecord::Base
    RESULT_LIMIT = 10

    scope :random, lambda {
      max_id  = connection.select_value("select max(id) from #{self.table_name}")
      where("id >= #{rand(max_id)}")
    }

    attr_reader :sizes
    has_attached_file :image, :styles => Proc.new { |i| i.instance.attachment_sizes }

    # Want to give each user a unique avatar?  Assign their profile image to the image returned here.
    def self.find_all(term)
      query = "site:commons.wikimedia.org \"is in the public domain\" #{term}"
      cache_search(query, term)
    end

    def set_attachment_sizes(sizes = {:thumb => ["100x100"]})
      @sizes = sizes
    end

    def attachment_sizes
      @sizes||={:thumb => ["100x100"]}
    end

    private
    def self.cache_search(query, term)
      images      = []
      num_results = 0

      Google::Search::Web.new(:query => query).each do |result|
        page        = agent.get(result.uri)
        image       = find_image_link(page)

        images << cache(term, image.href) unless image.nil?

        num_results+=1
        break if num_results == RESULT_LIMIT
      end

      images
    end

    def self.find_image_link(page)
      page.links.detect {|link| link.href =~ /http:\/\/upload.wikimedia.org\/wikipedia\/commons\/\w\// }
    end

    def self.agent
      @agent||= Mechanize.new
      @agent.max_history = 1
      @agent.user_agent_alias = 'Mac Safari'
      @agent
    end

    def self.cache(search_term, img_url)
      # No need to re-retrieve file if already done so
      image = find_by_image_original_url(img_url)
      return image unless image.nil?

      #image = open img_url
      image = open(URI.parse(img_url))

      # The original_filename passed in from a file open is unintelligible;
      #   change it to something better. Feels 'dirty' but admittance is the first step.
      def image.original_filename; base_uri.path.split('/').last; end
      image.original_filename.blank? ? nil : image

      SexItUpImage.create!(:image_original_url => img_url,
                           :image_search_term => search_term,
                           :image => image)
    end

  end


  module SexItUpHelper
    # Pass in a search term (e.g. 'dogs') and the resolution you
    #  want the image to be displayed at.
    def sexy_image(term, opts = {:width => 100, :height => 100})

      # The loving is a mess, what happened to all of the feeling?
      # I thought it was for real; babies, rings and fools kneeling
      # And words of pledging trust and lifetimes stretching forever
      # So what went wrong? It was a lie, it crumbled apart<commit_msg>Fixed a bug with the retrieval/setting of the downloaded image's name.<commit_after>require 'rubygems'
require 'paperclip'
require 'active_record'
require 'action_view'
require 'google-search'
require 'mechanize'
require 'open-uri'


Paperclip.configure
Paperclip::Railtie.insert

module SexItUp

  class SexItUpImage < ActiveRecord::Base
    RESULT_LIMIT = 10

    scope :random, lambda {
      max_id  = connection.select_value("select max(id) from #{self.table_name}")
      where("id >= #{rand(max_id)}")
    }

    attr_reader :sizes
    has_attached_file :image, :styles => Proc.new { |i| i.instance.attachment_sizes }

    # Want to give each user a unique avatar?  Assign their profile image to the image returned here.
    def self.find_all(term)
      query = "site:commons.wikimedia.org \"is in the public domain\" #{term}"
      cache_search(query, term)
    end

    def set_attachment_sizes(sizes = {:thumb => ["100x100"]})
      @sizes = sizes
    end

    def attachment_sizes
      @sizes||={:thumb => ["100x100"]}
    end

    private
    def self.cache_search(query, term)
      images      = []
      num_results = 0

      Google::Search::Web.new(:query => query).each do |result|
        page        = agent.get(result.uri)
        image       = find_image_link(page)

        images << cache(term, image.href) unless image.nil?

        num_results+=1
        break if num_results == RESULT_LIMIT
      end

      images
    end

    def self.find_image_link(page)
      page.links.detect {|link| link.href =~ /http:\/\/upload.wikimedia.org\/wikipedia\/commons\/\w\// }
    end

    def self.agent
      @agent||= Mechanize.new
      @agent.max_history = 1
      @agent.user_agent_alias = 'Mac Safari'
      @agent
    end

    def self.cache(search_term, img_url)
      # No need to re-retrieve file if already done so
      image = find_by_image_original_url(img_url)
      return image unless image.nil?

      #image = open img_url
      image = open(URI.parse(img_url))

      # The original_filename passed in from a file open is unintelligible;
      #   change it to something better. Feels 'dirty' but admittance is the first step.
      def image.original_filename; CGI.unescape(base_uri.path.split('/').last); end
      image.original_filename.blank? ? nil : image

      SexItUpImage.create!(:image_original_url => img_url,
                           :image_search_term => search_term,
                           :image => image)
    end

  end


  module SexItUpHelper
    # Pass in a search term (e.g. 'dogs') and the resolution you
    #  want the image to be displayed at.
    def sexy_image(term, opts = {:width => 100, :height => 100})

      # The loving is a mess, what happened to all of the feeling?
      # I thought it was for real; babies, rings and fools kneeling
      # And words of pledging trust and lifetimes stretching forever
      # So what went wrong? It was a lie, it crumbled apart
{'fraction_non_alphanumeric': 0.10118740320082602, 'fraction_numerical': 0.00791602133884013, 'mean_word_length': 2.6325, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6839312', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1750, 'n_words': 529}