Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#get_xkcd(number, caption = false, location = nil, name = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/xkcd_downloader.rb', line 9

def get_xkcd(number, caption = false, location = nil,name = nil)

	page = $agent.get("http://xkcd.com/#{number}/")
	comic = page.image_with(:src => /\/comics\//)
	last_dot = comic.src.rindex('.')
	extension = comic.src[last_dot..-1]
	if location.nil?
		location = $pwd
	end

	if name.nil? 
		name = "\##{number} - "+comic.alt
	end

	path_to_file = location+'/'+name
	comic.fetch.save path_to_file + extension
	if caption
		File.open(path_to_file + " - caption",'w') do |f|
			f.write comic.title
		end
	end
end