-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_json.rb
41 lines (36 loc) · 1.07 KB
/
extract_json.rb
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
require 'json'
require 'dotenv/load'
require 'http'
require 'pp'
json = JSON.parse(File.read('ffhq-dataset-v2.json'))
skip_to_idx = 0
written_idx = 0
json.each.with_index do |(key, image), idx|
next if idx < skip_to_idx
puts "idx: #{idx}, written_idx: #{written_idx}"
sleep 1
begin
flickr_url = image['metadata']['photo_url']
photo_id = flickr_url.split('/').last
dimensions = image['in_the_wild']['pixel_size']
response = HTTP.get('https://www.flickr.com/services/rest/',
params: {
method: 'flickr.photos.getSizes',
api_key: ENV['FLICKR_API_KEY'],
photo_id: photo_id,
format: 'json',
nojsoncallback: 1
})
response = JSON.parse(response.to_s)
size = response['sizes']['size'].find { |size| [size['width'], size['height']] == dimensions }
File.write("data/#{written_idx}.json", {
flickr_url: flickr_url,
photo_url: size['source'],
author: image['metadata']['author'],
face_quad: image['in_the_wild']['face_quad']
}.to_json)
written_idx += 1
rescue
# pass
end
end