Module: ImageUrlLoader
- Defined in:
- lib/asker/loader/image_url_loader.rb
Overview
Search URL images on Internet Methods:
-
load
- Accept String or an Array with the desired search -
sanitize_string
- Clean URL string -
sanitize_array
- Clean URL Array
Class Method Summary collapse
-
.load(input = []) ⇒ Object
Search “input” images on Google and return URL.
- .sanitize_array(input) ⇒ Object
- .sanitize_string(input) ⇒ Object
Class Method Details
.load(input = []) ⇒ Object
Search “input” images on Google and return URL
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/asker/loader/image_url_loader.rb', line 14 def self.load(input = []) filters = [] if input.class == String filters += sanitize_string(input.clone) elsif input.class == Array filters = sanitize_array(input.clone) else raise "[ERROR] ImageUrlLoader: Unkown type #{input.class}" end # Search Image URLs from Google site, selected by <filters> search_url = 'https://www.google.es/search?q=' search_url << filters.flatten.join('+').to_s search_url << '&source=lnms&tbm=isch&sa=X&ved=2ahUKEwj_g8Wfst7nAhWpzoUKHf_wDbsQ_AUoAnoECBMQBA&biw=1280&bih=591' image_urls = [] begin uri = URI.parse(search_url) response = Net::HTTP.get_response(uri) r = response.body.split(' ') r.each do |line| if line.include? 'src="https' image_urls << line.delete('"')[4, line.size] end end rescue Logger.verboseln '[ERROR] ImageUrlLoader' Logger.verboseln " => #{search_url}" Logger.verboseln ' => Check Internet connections' Logger.verboseln ' => Ensure URL is well formed' end image_urls end |
.sanitize_array(input) ⇒ Object
56 57 58 |
# File 'lib/asker/loader/image_url_loader.rb', line 56 def self.sanitize_array(input) input.map { |i| sanitize_string(i) } end |
.sanitize_string(input) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/asker/loader/image_url_loader.rb', line 47 def self.sanitize_string(input) text = input.dup r = [ ['á', 'a'], ['é', 'e'], ['í', 'i'], ['ó', 'o'], ['ú', 'u'], ['ñ', 'n'], ['Á', 'A'], ['É', 'E'], ['Í', 'I'], ['Ó', 'O'], ['Ú', 'U'], ['Ñ', 'N']] r.each { |item| text.gsub!(item[0], item[1]) } r = ['-', '_', ',', '"'] r.each { |item| text.gsub!(item, ' ') } text.split(' ') end |