Class: Lipaste::Lichess

Inherits:
Object
  • Object
show all
Defined in:
lib/lipaste/lichess.rb

Constant Summary collapse

HOST =
"http://en.lichess.org"
"lila2"

Class Method Summary collapse

Class Method Details

.get_uri(route) ⇒ Object



41
42
43
# File 'lib/lipaste/lichess.rb', line 41

def self.get_uri(route)
  URI.parse "#{HOST}/#{route.sub(/^\//, "")}"
end

.login(username, password) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/lipaste/lichess.rb', line 9

def self.(username, password)
  uri = get_uri "login"
  res = Net::HTTP.post_form uri, username: username, password: password
  cookie_headers = res.get_fields "set-cookie"
  matches = cookie_headers.map {|c| c.match /^([^=]+)=([^;]+)/}
  cookies = matches.map {|match| {name: match[1], value: match[2]}}
   = cookies.select {|cookie| cookie[:name] == LOGIN_COOKIE_NAME}
  .pop[:value]
end

.upload(login_cookie, pgn) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lipaste/lichess.rb', line 19

def self.upload(, pgn)
  uri = get_uri "import"
  http = Net::HTTP.new uri.host
  request = Net::HTTP::Post.new uri.request_uri
  request.set_form_data pgn: pgn
  request["Cookie"] = "#{LOGIN_COOKIE_NAME}=#{}" if 
  response = http.request request

  if response.code == "429"
    STDERR.puts "Your account is currently rated limited by Lichess, try waiting a couple minutes"
    exit 1
  elsif response.code != "303"
    STDERR.puts "Error uploading to Lichess"
    STDERR.puts response.code
    STDERR.puts response.body
    exit 1
  end

  paste_route = response.to_hash["location"][0]
  return get_uri(paste_route).to_s
end