This repository has been archived by the owner on Jul 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.rb
286 lines (228 loc) · 7.72 KB
/
app.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
require 'sinatra'
require 'sinatra/respond_with'
require 'better_errors' if settings.development?
require 'net/http'
require 'mongo'
require 'multi_json'
require 'omniauth'
require 'omniauth-openid'
require 'omniauth-steam'
require 'steam-condenser/community'
require 'retriable'
env_variables_file = File.join(settings.root, 'env_variables.rb')
load(env_variables_file) if File.exists? env_variables_file
include Mongo
configure :development do
use BetterErrors::Middleware
end
set :db, MongoClient.from_uri(ENV['DATABASE_URL']).db(ENV['DATABASE_NAME'])
WebApi.api_key = ENV['STEAM_API_KEY']
enable :sessions
set :session_secret, ENV['SESSION_KEY']
use OmniAuth::Builder do
provider :steam, ENV['STEAM_API_KEY']
end
helpers do
# @param [String] type Type of the request ('gid' or 'groups')
# @param [String, Fixnum] groupid ID of the group
# @param [Array] fields Which fields to return
# @param [Fixnum] fetch_pages How many pages to fetch. Default -1 (all pages)
# @return [String] JSON with the group info and members or error.
def get_steam_group(type, groupid, fields = [:gid, :name, :avatar, :memberCount, :members], fetch_pages = -1)
current_page = 1
total_pages = 1
all_members = []
output = {}
groupid = groupid.gsub('[', '%5B').gsub(']', '%5D')
begin
url = "http://steamcommunity.com/#{type}/#{groupid}/memberslistxml/?xml=1&p=#{current_page}"
response = Net::HTTP.get_response(URI(url))
response = MultiXml.parse(response.body).to_hash
if current_page == 1
if fetch_pages == -1
total_pages = response['memberList']['totalPages'].to_i
else
total_pages = fetch_pages
end
total_pages = 9 if total_pages >= 10 # check 10k members, not more - it would be too slow
output[:gid] = response['memberList']['groupID64'] if fields.include? :gid
output[:name] = response['memberList']['groupDetails']['groupName'] if fields.include? :name
output[:avatar] = response['memberList']['groupDetails']['avatarIcon'][67..-5] if fields.include? :avatar
output[:memberCount] = response['memberList']['memberCount'].to_i if fields.include? :memberCount
end
all_members.push response['memberList']['members']['steamID64'] if fields.include? :members
current_page += 1
end while current_page <= total_pages
if fields.include? :members
all_members.flatten!
output[:membersWithEntries] = settings.db.collection('users').find({ :_id => { '$in' => all_members } }, { fields: [:_id] }).to_a.map { |user| user['_id'] }
end
output # call MultiJson.dump on the returned hash
end
end
%w(get post).each do |method|
send(method, '/auth/:provider/callback') do
begin
session[:user] = env['omniauth.auth'][:extra][:raw_info]
redirect "/#!/profiles/#{session[:user].steamid}"
rescue StandardError
redirect '/'
end
end
end
get '/auth/failure' do
redirect '/'
end
get '/' do
if params[:_escaped_fragment_]
# AJAX crawling - see documentation: https://developers.google.com/webmasters/ajax-crawling/docs/specification
redirect "/#!/#{params[:_escaped_fragment_]}"
else
if session[:user]
@current_user = session[:user]
end
erb :layout
end
end
get '/signout' do
session[:user] = nil
redirect '/'
end
# API stuff
get '/api/profiles', provides: :json do
begin
steamids = params[:steamids].split(',')
steamids.uniq!
if steamids.count > 300 # Steam friends limit is 300 (250+50 for connecting FB), don't bother with more requests
raise
end
if steamids.all? { |i| i.to_i.to_s == i }
users = []
# Steam has a limit of 100 IDs per request
steamids.each_slice(100) do |ids|
retriable tries: 3, interval: 2 do
api_users = WebApi.get(:json, 'ISteamUser', 'GetPlayerSummaries', '0002', steamids: ids.join(','))
users << MultiJson.load(api_users)['response']['players'].map do |user|
{
_id: user['steamid'],
username: user['personaname'],
avatar: user['avatar'].split('.')[0..-2].join('.').split('/')[-2..-1].join('/'),
public: (user['communityvisibilitystate'] == 3 ? true : false)
}
end
end
end
users.flatten!
status 404 if users == []
users.to_json
else
raise # all Steam IDs must be numbers
end
rescue => e
raise e if settings.development?
status 404
MultiJson.dump({ error: true })
end
end
get '/api/id/:customurl', provides: :json do
begin
{ steamid: SteamId.resolve_vanity_url(params[:customurl]).to_s }.to_json
rescue => e
raise e if settings.development?
status 404
MultiJson.dump({ error: true })
end
end
get '/api/friends/:steamid', provides: :json do
begin
friends = WebApi.get(:json, 'ISteamUser', 'GetFriendList', '0001', steamid: params[:steamid])
friends = MultiJson.load(friends)['friendslist']['friends'].map { |friend| friend['steamid'] }
friends.to_json
rescue => e
raise e if settings.development?
status 404
{ error: true }.to_json
end
end
get '/api/usergroups/:steamid', provides: :json do
begin
groups = WebApi.get(:json, 'ISteamUser', 'GetUserGroupList', '0001', steamid: params[:steamid])
groups = MultiJson.load(groups)['response']['groups'].map { |gid| gid['gid'] }
groups_result = {
groupCount: groups.count,
groups: []
}
# return empty groups if user is a member of more than 1000 groups - hi, http://steamcommunity.com/id/hyins
if groups.count <= 1000
threads = []
groups.each_slice(5) do |groups_slice|
threads << Thread.new(groups_slice) do |slice|
slice.map do |groupid|
# groupid is in a weird format, it needs to be [g:0:groupid] to fetch it (thanks iveinsomnia)
gid = "[g:0:#{groupid}]"
begin
group = get_steam_group('gid', gid, [:gid, :name, :avatar, :memberCount], 1)
groups_result[:groups].push group
rescue => e
puts "Exception while loading group ID #{gid}" if settings.development?
# silently fail if it's a private group or if Steam API is derping
next
end
end
end
end
threads.each { |t| t.join }
end
groups_result.to_json
rescue => e
raise e if settings.development?
status 404
MultiJson.dump({ error: true })
end
end
get %r{/api/(?<type>(gid|groups))/(?<groupid>.*)}, provides: :json do |type, groupid|
begin
MultiJson.dump get_steam_group(type, groupid)
rescue => e
raise e if settings.development?
status 404
MultiJson.dump({ error: true })
end
end
get '/api/latestcommits', provides: :json do
begin
http = Net::HTTP.new('api.github.com', 443)
http.use_ssl = true
response = http.get("/repos/TomasDuda/super-hexagon-stats/commits?per_page=5&client_id=#{ENV['GITHUB_OAUTH_CLIENT_ID']}&client_secret=#{ENV['GITHUB_OAUTH_CLIENT_SECRET']}")
response.body
rescue => e
raise e if settings.development?
status 404
MultiJson.dump({ error: true })
end
end
# database wrapper
get %r{/api/db/(?<collection>(leaderboard|users|settings))$}, provides: :json do |collection|
find_options = {}
if params[:s]
find_options[:sort] = MultiJson.load params[:s]
end
if params[:l]
find_options[:limit] = params[:l].to_i
end
if params[:sk]
find_options[:skip] = params[:sk].to_i
end
if params[:f]
find_options[:fields] = MultiJson.load params[:f]
end
unless params[:q]
params[:q] = '{}'
end
query = settings.db.collection(collection).find(MultiJson.load(params[:q]), find_options)
if params[:c] then
query.count(true).to_json
else
query.to_a.to_json
end
end