Class: Bingwallpaper::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/bingwallpaper/image.rb

Overview

Provides a class that queries and fetches the Bing “image of the day”.

Constant Summary collapse

PROTO =

Protocol for accessing Bing

'https://'
DOMAIN =

Current Bing domain

'www.bing.com'
PATH =

Path for the image archive

'/HPImageArchive.aspx'
FORMAT =

Format for retrieving data

'xml'
DEFAULT_MARKET =

Default market to fetch

'en-US'
DEFAULT_INDEX =

Default index to fetch (today)

0
DEFAULT_STORAGE_PATH =

Default storage path

ENV['HOME'] + '/.config/bing_wallpaper'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(market = DEFAULT_MARKET, index = DEFAULT_INDEX, storage_path = DEFAULT_STORAGE_PATH) ⇒ Image

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



48
49
50
51
52
53
54
55
56
57
# 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

Instance Attribute Details

#indexObject

index to fetch



38
39
40
# File 'lib/bingwallpaper/image.rb', line 38

def index
  @index
end

#marketObject

Market to fetch



35
36
37
# File 'lib/bingwallpaper/image.rb', line 35

def market
  @market
end

#storage_pathObject

path for storing downloaded images



41
42
43
# File 'lib/bingwallpaper/image.rb', line 41

def storage_path
  @storage_path
end

Instance Method Details

#build_url(partial) ⇒ Object

Constructs a full path for the provided partial URL.

partial

the end of the target URL



62
63
64
65
# File 'lib/bingwallpaper/image.rb', line 62

def build_url(partial)

  return PROTO + DOMAIN + partial
end

#cleanup_filename(filename) ⇒ Object

Parses out a nice filename from the icky URL.

filename

Filename from the URL



103
104
105
106
107
108
109
110
111
# 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

#download_image(file_path, url) ⇒ Object

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



126
127
128
129
130
131
132
133
134
135
136
137
138
# 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

#get_data_urlObject

Returns the data URL for the image of the day.



68
69
70
71
72
# 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

#image_storage_path(file_name) ⇒ Object

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’



117
118
119
# File 'lib/bingwallpaper/image.rb', line 117

def image_storage_path(file_name)
  Pathname.new @storage_path + "/" + file_name
end

#parse_xml(url) ⇒ Object

Parses the XML data and returns a hash of image information.

url

complete path to the Bing image of the day



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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