-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs.rb
70 lines (61 loc) · 1.76 KB
/
rs.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
62
63
64
65
66
67
68
69
70
require "json"
require "net/http"
require "openssl"
require "securerandom"
def user2lid(username)
uri = URI("https://letterboxd.com/#{username}/")
response = nil
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
response = http.head(uri)
end
lid = response['x-letterboxd-identifier']
end
def letterboxd(endpoint, user_params = "")
apikey = ENV.fetch("KEY")
nonce = SecureRandom.uuid
timestamp = Time.now.to_i
base_url = "https://api.letterboxd.com/api/v0"
params = "?apikey=#{apikey}&nonce=#{nonce}×tamp=#{timestamp}#{user_params}"
url = "#{base_url}#{endpoint}#{params}"
http_method = "GET"
body = ""
digest = OpenSSL::Digest::SHA256.new
key = ENV.fetch("SECRET")
data = "#{http_method}\u0000#{url}\u0000#{body}"
signature = OpenSSL::HMAC.hexdigest(digest, key, data)
uri = URI("#{url}&signature=#{signature}")
response = nil
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
response = http.get(uri)
end
JSON.parse(response.body)
rescue JSON::ParserError
p response.body if response
raise
end
def watchlist(username)
lid = user2lid(username)
names = []
cursor = nil
print "#{username}: "
begin
print "."
endpoint = "/member/#{lid}/watchlist"
params = "&perPage=100"
params += "&cursor=#{cursor}" if cursor
json = letterboxd(endpoint, params)
cursor = json["next"]
names += json["items"].map { |item| item["name"] }
end while cursor
puts
names
end
abort "No users given" if ARGV.empty?
users = {}
ARGV.each { |arg| users.store(arg, []) }
users.keys.each do |username|
users[username] += watchlist(username)
end
common_movies = users.values.inject(:&)
require "pp"
pp common_movies