-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit.rb
executable file
·61 lines (48 loc) · 1.37 KB
/
reddit.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env ruby
require 'redd'
require 'aws-sdk-rekognition'
require 'open-uri'
require 'json'
session = Redd.it(
user_agent: 'Redd:FindMyAncestor:v1.0.0 (by /u/Mustermind)',
client_id: ENV['REDDIT_CLIENT_ID'],
secret: ENV['REDDIT_SECRET'],
username: ENV['REDDIT_LOGIN'],
password: ENV['REDDIT_PASSWORD'],
)
rekognition = Aws::Rekognition::Client.new(
region: 'eu-west-1',
profile: 'perso',
)
subreddit = ARGV[0] || 'TheWayWeWere'
imported = 0
last_known = "progress/reddit_#{subreddit}.last_known"
after = File.read(last_known).chomp || nil
loop do
puts "Getting listing after #{after}"
listing = session.subreddit(subreddit).listing(:new, after: after)
listing.each_with_index do |l, i|
puts l
url = l.url
uri = URI.parse(url)
id = l.id
img = uri.path.sub(%r{^/}, '')
ref = "reddit:#{subreddit}:#{id}:#{img}"
begin
img = URI.open(url)
puts "[#{imported+i+1}] Importing #{ref} (#{url}) into collection"
rekognition.index_faces({
collection_id: 'flickr',
image: { bytes: img.read },
external_image_id: ref,
})
after = l.name
File.open(last_known, 'w') { |f| f.puts after }
rescue => e
puts "W: failed to import #{ref} (#{url}): #{e}"
end
end
puts "done with listing: #{listing}"
imported += listing.to_a.length
end
puts "imported #{imported} photos"