// description of your code here
1
2
3
4
5
6
7
8 groups = ['trees']
9 flick_api_key = 'cc0880a9d309466758fecb6557d7040b'
10
11
12
13
14
15
16 p1 = 'c:/tmp/img.jpg'
17 p2 = 'c:\\tmp\\img.bmp'
18
19
20
21
22
23 imagick_path = 'convert.exe'
24
25
26 skip_small = 1000
27 skip_big = 2100
28
29
30
31
32
33 require 'Win32API'
34 require 'net/http'
35 require 'win32/sspi'
36 require 'json'
37 require 'yaml'
38 require "win32/open3"
39 require "win32/process"
40
41 def parse_url(url)
42 m = /(?:.*?\/\/)(.*?)(\/.*)/.match(url)
43 [ m[1] , m[2] ]
44 end
45
46 class NetWrap
47 def initialize(p_type = nil, p_host = nil, p_port = nil)
48 if !p_type
49 @conn = Net::HTTP
50 else
51 @conn = Net::HTTP.Proxy(p_host, p_port)
52 end
53 @p_type = p_type
54 end
55
56 def download(url)
57 site, path = parse_url(url)
58 data = ''
59 @conn.start(site) do |http|
60 if @p_type && @p_type == :sspi
61 resp, data = Win32::SSPI::NegotiateAuth.proxy_auth_get(http, path)
62 else
63 resp, data = http.get(path)
64 end
65 end
66 return data
67 end
68 end
69
70 class FlickR
71 def initialize(apikey, netw)
72 @apikey = apikey
73 @netw = netw
74 end
75
76 def request(api_meth,params = {})
77 url = "http://api.flickr.com/services/rest/?method=#{api_meth}&api_key=#{@apikey}&format=json"
78 params.each {|key,val| url << "&#{key}=#{val}"}
79 res = @netw.download(url)
80 JSON.parse(res['jsonFlickrApi('.size..-(');'.size)])
81 end
82
83 def group_photos(id)
84 request('flickr.groups.pools.getPhotos', :group_id => id)['photos']['photo']
85 end
86
87 def group_lookup(name)
88 request('flickr.urls.lookupGroup', :url => "http://flickr.com/groups/#{name}/pool/")['group']['id']
89 end
90
91 def photo_urls(id)
92 request('flickr.photos.getSizes', :photo_id => id)['sizes']['size']
93 end
94
95 def photo_best(id, width)
96 sizes = photo_urls(id).sort {|a,b| a['width'].to_i <=> b['width'].to_i}
97 return sizes.find {|x| x['width'].to_i > width} || sizes.last()
98 end
99 end
100
101 class ImgList
102 def initialize(cfgname)
103 @cfg = cfgname
104 @saw = {}
105 begin
106 if f = open(@cfg,'r')
107 @saw = YAML.load(f)
108 f.close()
109 cnt = 0
110 @saw.each {|key,val| cnt += 1 if val[1]}
111 end
112 rescue
113 end
114 end
115
116 def save()
117 if f = open(@cfg,'w+')
118 f.write(@saw.to_yaml)
119 f.close()
120 end
121 end
122
123 def next()
124 img = @saw.detect {|x| !x[1]}
125 return img[0] if img
126 img
127 end
128
129 def add(url)
130 if !@saw.has_key?(url)
131 @saw[url] = false
132 end
133 end
134
135 def mark(url)
136 @saw[url] = true
137 end
138
139 def reset()
140 @saw.each {|key,val| @saw[key] = false}
141 end
142 end
143
144 if defined?(http_proxy)
145 netw = NetWrap.new(http_proxy[:type], http_proxy[:host], http_proxy[:port])
146 else
147 netw = NetWrap.new()
148 end
149
150 scr_w = Win32API.new("user32", "GetSystemMetrics", ['I'] , 'I').call(0)
151
152 il = ImgList.new('wpcache.yaml')
153
154 photo_url = il.next()
155
156 if !photo_url
157 fr = FlickR.new(flick_api_key, netw)
158 groups.each do |group|
159 fr.group_photos(fr.group_lookup(group)).each do |x|
160 next if !x['ispublic']
161 photo = fr.photo_best(x['id'],scr_w)
162 next if skip_small && photo['width'].to_i < skip_small
163 next if skip_big && photo['width'].to_i > skip_big
164 url = photo['source']
165 il.add(url)
166 end
167 end
168 photo_url = il.next()
169 end
170
171 if !photo_url
172 il.reset()
173 photo_url = il.next()
174 end
175
176 il.mark(photo_url)
177 il.save()
178
179 open(p1,'wb+') { |f| f.write(netw.download(photo_url)) }
180
181 cmdline = "#{imagick_path} -resize #{scr_w} #{p1} bmp3:#{p2}"
182 Process::waitpid2(Open4::popen4(cmdline)[3])
183
184 Win32API.new("user32", "SystemParametersInfo", ['L','L','P','L'] , 'L').Call(20,0,p2,3)