This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfleet.rb
75 lines (61 loc) · 2.15 KB
/
fleet.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
71
72
73
74
75
require 'sinatra'
require 'oj'
require 'httparty'
class HTTP
include HTTParty
base_uri 'https://m.np.playstation.net/api'
headers 'Accept-Language' => 'en-US'
headers 'User-Agent' => 'Mozilla/5.0 (PlayStation 5 3.03/SmartTV) AppleWebKit/605.1.15 (KHTML, like Gecko)'
end
class PlayStation < Sinatra::Base
set :server, 'thin'
set :logging, true
before do
content_type :json
@@requests ||= 0
@@resets_at ||= Time.now + 3600
# I don't think this is the best approach. This data resets, regardless of the 3rd party API status, whenever
# the script is run,
# If we don't add some sort of in-memory cache, like Redis, we will have to use games.directory instead, send
# a POST each time and store the values in there.
#
if Time.now > @@resets_at
@@requests = 0
@@resets_at = Time.now + 3600
end
@@current_request_path = request.env['REQUEST_URI']
if @@requests >= 150
halt 429, {
error: 'Too many requests',
message: "You have made #{ @@requests } so far. Please try again later.",
resets_at: @@resets_at
}.to_json
end
HTTP.headers('Authorization' => request.env['HTTP_AUTHORIZATION']) rescue nil
end
get '/heartbeat' do
HTTParty.get('https://games.directory/heartbeat',
headers: {
'User-Agent' => 'Fleet/1.0 (compatible; FleetBot/1.0; https://github.com/games-directory/fleet; [email protected]'
}
)
end
get '/stats' do
{
requests: @@requests,
resets_at: @@resets_at
}.to_json
end
# Proof of concept. Might be a bulletproof solution without having to replicate every single endpoint
# point we want to access. Every gem we currently use in games.directory defines its own routes, params
# and the authentication credentials and sends them in the request.
#
# With this in place, Sinatra will catch anything we throw at him and send the request out. The only thing
# we need to do is ensure we pass the authentication, coming from the gem, along with the request.
#
get '/*' do
@@requests += 1
Oj.dump(HTTP.get(@@current_request_path).parsed_response)
end
end
PlayStation.run!