-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomImages.rb
More file actions
66 lines (53 loc) · 1.33 KB
/
RandomImages.rb
File metadata and controls
66 lines (53 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'open-uri'
require 'uri'
require 'RMagick'
module Flickr
InterestingImages = []
def interesting_image
if InterestingImages.empty?
4.times do |i|
begin
html = open('http://www.flickr.com/explore/interesting/7days/'){|fd| fd.read}
uris = URI.extract(html, 'http')
uris.select do |uri|
next unless uri =~ /\.jpg$/
InterestingImages.push(uri.gsub(%r/_m/, ''))
end
break
rescue
raise if i==3
sleep(rand)
end
end
end
InterestingImages.shift
end
def interesting_images
[interesting_image, *InterestingImages]
ensure
InterestingImages.clear
end
extend(Flickr)
end
# Counter for image file names
counter = 1
# Pulls 9 random images from Flickr
Flickr.interesting_images.each do |url|
# Remove .jpg from string
image = url.gsub!(".jpg", "_b.jpg")
# Create file name to save image out to
jpgName = counter.to_s() + ".jpg"
# Download and save the image
writeOut = open(jpgName, "wb")
writeOut.write(open(image).read)
writeOut.close
# Convert the JPG to PNG
thumb = Magick::Image.read(jpgName).first
thumb.format = "PNG"
pngName = counter.to_s() + ".png"
thumb.write(pngName)
# Remove the JPG
FileUtils.rm(jpgName)
# Increment the counter
counter+=1
end