Created
November 19, 2010 02:19
-
-
Save gbissett/706042 to your computer and use it in GitHub Desktop.
automates the album cover game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ____ _ _ ____ _ | |
# | _ \ ___ | |__ ___ | |_ | _ \ ___ ___| | __ | |
# | |_) / _ \| '_ \ / _ \| __| | |_) / _ \ / __| |/ / | |
# | _ < (_) | |_) | (_) | |_ | _ < (_) | (__| < | |
# |_| \_\___/|_.__/ \___/ \__| |_| \_\___/ \___|_|\_\ | |
# | |
# 1. http://en.wikipedia.org/wiki/Special:Random | |
# The first article title on the page is the name of your band. | |
# | |
# 2. www.quotationspage.com/random.php3 | |
# The last four words of the very last quote is the title of your album. | |
# | |
# 3. www.flickr.com/explore/interesting/7days/ | |
# The third picture, no matter what it is, will be your album cover. | |
%w[rubygems nokogiri open-uri rmagick].each{|gem| require gem} | |
# Wikipedia wants a nice a user agent, otherwise it'll 403 | |
user_agent = "Ruby/#{RUBY_VERSION}" | |
band_name_url = 'http://en.wikipedia.org/wiki/Special:Random' | |
band_name_page = Nokogiri::HTML(open(band_name_url, 'User-Agent' => user_agent)) | |
album_title_url = 'http://www.quotationspage.com/random.php3' | |
album_title_page = Nokogiri::HTML(open(album_title_url, 'User-Agent' => user_agent)) | |
album_cover_url = 'http://www.flickr.com/explore/interesting/7days/' | |
album_cover_page = Nokogiri::HTML(open(album_cover_url, 'User-Agent' => user_agent)) | |
# The first article title on the page is the name of your band. | |
band_name = band_name_page.css('#firstHeading').first.content | |
# The last four words of the very last quote is the title of your album. | |
album_title = album_title_page.css('dt.quote').last.content.split(' ')[-4..-1].join(' ') | |
# The third picture, no matter what it is, will be your album cover. | |
thumbnail_url = album_cover_page.css('.Photo img')[2]['src'] | |
# hack flickr url to get a larger image | |
# eg. | |
# http://farm5.static.flickr.com/4092/5011367066_c1d297f51f_m.jpg | |
# http://farm5.static.flickr.com/4092/5011367066_c1d297f51f_z.jpg | |
album_cover_url = thumbnail_url.sub('m.jpg', 'z.jpg') | |
# stash the flickr pic in a file | |
album_cover = open('cover.jpg', 'wb') | |
album_cover.write(open(album_cover_url).read) | |
album_cover.close | |
album_cover = Magick::ImageList.new('cover.jpg') | |
album_cover.resize_to_fill!(400, 400) | |
# write stuff on the album cover. | |
# I don't know why the pen is named gc | |
gc = Magick::Draw.new | |
gc.annotate(album_cover, 0,0,0,-40, band_name) { | |
self.fill = 'white' | |
self.stroke = 'black' | |
self.pointsize = 24 | |
self.font_weight = Magick::BoldWeight | |
self.gravity = Magick::CenterGravity | |
} | |
gc.annotate(album_cover, 0,0,0,10, album_title) { | |
self.fill = 'white' | |
self.stroke = 'gray50' | |
self.pointsize = 14 | |
self.font_weight = Magick::BoldWeight | |
self.gravity = Magick::CenterGravity | |
} | |
# display the cover on the screen (will use X on your mac or linus) | |
#album_cover.display | |
# write the cover to disk | |
album_cover.write('cover.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment