class Bingwallpaper::Image
Provides a class that queries and fetches the Bing “image of the day”.
Constants
- DEFAULT_INDEX
Default index to fetch (today)
- DEFAULT_MARKET
Default market to fetch
- DEFAULT_STORAGE_PATH
Default storage path
- DOMAIN
Current Bing domain
- FORMAT
Format for retrieving data
- PATH
Path for the image archive
- PROTO
Protocol for accessing Bing
Attributes
index to fetch
Market to fetch
path for storing downloaded images
Public Class Methods
Creates a new instance.
- market
-
The market to use when fetching
- index
-
Index of the image to fetch (0 = today)
storage_path
-
Path to directory for storing images
# File lib/bingwallpaper/image.rb, line 48 def initialize(market = DEFAULT_MARKET, index = DEFAULT_INDEX, storage_path = DEFAULT_STORAGE_PATH) @market = market @index = index @storage_path = storage_path # Ensure the storage path exists FileUtils.mkpath storage_path end
Public Instance Methods
Constructs a full path for the provided partial URL.
- partial
-
the end of the target URL
# File lib/bingwallpaper/image.rb, line 62 def build_url(partial) return PROTO + DOMAIN + partial end
Parses out a nice filename from the icky URL.
- filename
-
Filename from the URL
# File lib/bingwallpaper/image.rb, line 103 def cleanup_filename(filename) matches = /[A-Za-z0-9_-]+\.jpg/.match(filename) if matches.length > 0 return matches[0] end return filename end
Downloads the image at the supplied URL and saves it to the specified file.
- file_path
-
Path for the storing image
- url
-
URL to the image to download
# File lib/bingwallpaper/image.rb, line 126 def download_image(file_path, url) begin # download the hi-res image open(file_path, 'wb') do |file| file << URI.open(url).read end rescue Exception => exception file_path.delete raise exception end end
Returns the data URL for the image of the day.
# File lib/bingwallpaper/image.rb, line 68 def get_data_url return build_url(PATH + '?format=' + FORMAT + '&idx=' + @index.to_s + '&n=1' + '&mkt=' + @market) end
Returns a Pathname with location where the image from the provided Bing image hash will be stored.
- file_name
-
Path to the file storage location'
# File lib/bingwallpaper/image.rb, line 117 def image_storage_path(file_name) Pathname.new @storage_path + "/" + file_name end
Parses the XML data and returns a hash of image information.
- url
-
complete path to the Bing image of the day
# File lib/bingwallpaper/image.rb, line 77 def parse_xml(url) doc = Nokogiri::HTML(URI.open(url)) doc.xpath('//images/image').map do |image| # figure out the hi-res image path image_path = image.xpath('url').text.sub( image.xpath('url').text.rpartition("_").last, '1920x1200.jpg') # store the other path as fallback image_fallback_path = image.xpath('url').text.to_s # build our hash of image data {:links => {:url => build_url(image_path), :fallback_url => build_url(image_fallback_path), :copyright_url => image.xpath('copyrightlink').text}, :file_name => cleanup_filename(Pathname.new(image_path).basename.to_s), :fallback_file_name => Pathname.new(image_fallback_path).basename.to_s, :copyright => image.xpath('copyright').text} end end