Class: Arty
- Inherits:
-
Object
- Object
- Arty
- Defined in:
- lib/arty.rb
Instance Method Summary collapse
- #fetch_images ⇒ Object
-
#find_artwork ⇒ Object
Use the iTunes API to fetch the artwork images for the @artists provided.
- #generate_montage(output_file_path = nil) ⇒ Object
-
#initialize(names) ⇒ Arty
constructor
Provide the names of the artists to render upon initialisation.
Constructor Details
#initialize(names) ⇒ Arty
Provide the names of the artists to render upon initialisation
9 10 11 12 13 |
# File 'lib/arty.rb', line 9 def initialize(names) @artists = names @artwork_urls = [] @artwork_images = [] end |
Instance Method Details
#fetch_images ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/arty.rb', line 53 def fetch_images for image_url in @artwork_urls File.open("./tmp/#{Digest::MD5.hexdigest(image_url)}.png", "wb") do |f| f.write HTTParty.get(image_url).body end end end |
#find_artwork ⇒ Object
Use the iTunes API to fetch the artwork images for the @artists provided. If an artist could not be found It will simply omitted from the resulting @artwork_urls array
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 46 47 48 49 50 51 |
# File 'lib/arty.rb', line 18 def find_artwork for artist in @artists response = HTTParty.get(URI.encode("https://itunes.apple.com/search?term=#{artist}")) response_object = JSON.parse(response.body) for object in response_object["results"] if JaroWinkler.distance(object["artistName"], artist) > 0.95 && object["collectionArtistName"] == nil && object["primaryGenreName"] != "Soundtrack" url = object["artworkUrl100"] url = url.sub("100x100", "600x600") @artwork_urls.push(url) break end end end while @artwork_urls.count < 4 random_artist = @artists.sample response = HTTParty.get(URI.encode("https://itunes.apple.com/search?term=#{random_artist}")) response_object = JSON.parse(response.body) for object in response_object["results"] if JaroWinkler.distance(object["artistName"], random_artist) > 0.95 && object["collectionArtistName"] == nil && object["primaryGenreName"] != "Soundtrack" url = object["artworkUrl100"] url = url.sub("100x100", "600x600") if @artwork_urls.include?(url) == false @artwork_urls.push(url) break end end end end end |
#generate_montage(output_file_path = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/arty.rb', line 61 def generate_montage(output_file_path = nil) self.find_artwork() self.fetch_images() i = Magick::ImageList.new for image_url in @artwork_urls i.read("./tmp/#{Digest::MD5.hexdigest(image_url)}.png") end image = i.montage do |mont| mont.geometry = "400x400" end width = image.columns degrees = 7 image.rotate!(degrees) radians = degrees * Math::PI / 180 trim = Math.sin(radians) * width image.shave!(trim, trim) if output_file_path.nil? output_file_path = "./tmp/output.jpeg" end image.write(output_file_path) { self.quality = 100 self.format = 'JPEG' } end |