-
Notifications
You must be signed in to change notification settings - Fork 1
/
reg0.rb
76 lines (69 loc) · 2.38 KB
/
reg0.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 'net/http'
require 'uri'
require 'json'
require 'sqlite3'
require_relative './config.rb'
enable :sessions
set :port => 28538, :bind => '127.0.0.1'
get '/r2020' do
erb :r2020
end
get '/reg1' do
if request['code'].nil?
puts "No code"
erb :reg1fail
else
begin
retries ||= 0
puts "code is #{request['code']}"
uri = URI.parse("https://www.strava.com")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
rq = Net::HTTP::Post.new("/oauth/token?client_id=#{CLIENT_ID}&client_secret=#{CLIENT_SECRET}&code=#{request['code']}&grant_type=authorization_code")
response = http.request(rq)
rescue => e
puts 'Strava error, retry:', $!, $@
sleep 1
retry if (retries += 1) < 3
end
j = JSON.parse(response.body)
puts j
session[:sid]=j['athlete']['id']
session[:uname]=j['athlete']['username']
session[:fname]=j['athlete']['firstname']
session[:lname]=j['athlete']['lastname']
session[:sex]=j['athlete']['sex']=='F' ? 0 : 1
session[:acctoken]=j['access_token']
session[:reftoken]=j['refresh_token']
erb :reg01
end
end
get '/reg02' do
begin
p :locals
retries ||= 0
db = SQLite3::Database.new("2020.db")
fullname="#{params[:fname]} #{params[:lname]}"
puts "INSERT OR REPLACE INTO runners VALUES (#{session[:sid]},'#{fullname}', '#{session[:uname]}', '#{params[:email]}', 0, #{params[:volume]}, #{session[:sex]}, '#{session[:acctoken]}', '#{session[:reftoken]}')"
db.execute("INSERT OR REPLACE INTO runners VALUES (#{session[:sid]},'#{fullname}', '#{session[:uname]}', '#{params[:email]}', 0, #{params[:volume]}, #{session[:sex]}, '#{session[:acctoken]}', '#{session[:reftoken]}')")
d = db.execute("SELECT * FROM runners WHERE runnerid=#{session[:sid]}")[0]
session[:fullname]=d[1]
session[:email]=d[3]
session[:sid]=d[0]
session[:uname]=d[2]
session[:volume]=d[5]
db.close
erb :reg02
rescue => e
puts 'db error, retry:', $!, $@
sleep 1
retry if (retries += 1) < 3
end
end
get '/gudsqap' do
"Hi, #{session['name']}, got gudsqap: #{request['code']}"
end
get '/aero' do
redirect 'http://aerobia.ru'
end