Skip to content

Instantly share code, notes, and snippets.

@FiXato
Created December 2, 2009 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FiXato/246833 to your computer and use it in GitHub Desktop.
Save FiXato/246833 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Retrieves all National Geographic Photo-contest wallpapers from 2008 and 2009
# Original scripts from: http://www.webupd8.org/2009/11/automatically-download-all-wallpapers.html
require 'net/http'
class WallpaperRetriever
attr_reader :destination_folder,:urls
def initialize(destination="~/Pictures/Wallpapers/NationalGeographic/")
@destination_folder = File.expand_path(destination)
@url_mappings = {}
create_destination_folder
end
def create_destination_folder
`mkdir -p #{destination_folder}` unless File.exists?(destination_folder)
end
def download_all
get_wallpaper_urls
@url_mappings.each do |url,v|
dateyear = "%s%s" % [v[:year],v[:date]]
entry = v[:entry]
destination = File.join(destination_folder,"#{dateyear}-#{entry}.jpg")
puts "Testing #{url}"
unless File.exists?(destination)
if Net::HTTP.start("ngm.nationalgeographic.com",80){|http| http.head(url.gsub("http://ngm.nationalgeographic.com",''))}.code.to_i == 200
puts "Retrieving entry #{entry} from #{dateyear} (#{url}) to #{destination}"
puts `wget -U -q "Mozilla" -O "#{destination}" --referer="#{url}" "#{url}"`
end
end
end
end
private
# Specifying a resolution only affects 2008, since 2009 only has 1600px width wallpapers
# only valid resolutions for 2008 are 1280 (default), 1024 and 800.
def get_wallpaper_urls(resolution=1280)
dates_2008 = %w[1107 1103 1027 1020 1014 1006 0929 0922 0915 0908 0901 0825 0818 0811]
dates_2009 = %w[1109 1102 1026 1019 1013 1005 0928 0921 0914 0907 0831 0824 0817]
dates_2008.map do |date|
(1..26).each do |entry|
url = "http://ngm.nationalgeographic.com/photo-contest/img/wallpaper/#{date}wallpaper-#{entry}_#{resolution}.jpg"
destination = File.join(destination_folder,"2008#{date}-#{entry}.jpg")
@url_mappings[url] = {:year => 2008, :date => date, :entry => entry}
end
end
dates_2009.map do |date|
(1..26).each do |entry|
url = "http://ngm.nationalgeographic.com/photo-contest/2009/img/wallpaper/#{date}wallpaper-#{entry}_1600.jpg"
@url_mappings[url] = {:year => 2009, :date => date, :entry => entry}
end
end
end
end
WallpaperRetriever.new.download_all
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment