This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcf-main.el
102 lines (89 loc) · 3.49 KB
/
cf-main.el
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
(load "cf-languages.el")
(require 'json)
(setq cf-default-language cf-pl-g++)
(setq cf-host "codeforces.com")
(setq cf-proto "http")
(setq cf-cookies-file "~/.cf-cookies")
(defun cf-get-csrf-token(page)
(string-match "name='csrf_token' +value='\\([^\']+\\)'" page)
(match-string 1 page))
(defun cf-logged-in-as ()
(setq cf-response
(shell-command-to-string
(format "curl --silent --cookie-jar %s --cookie %s '%s://%s/' "
cf-cookies-file cf-cookies-file
cf-proto cf-host)))
(when (string-match "<a href=\"/[a-z0-9]*/logout\">" cf-response)
(string-match "<a href=\"/profile/\\([^\"]*\\)\">" cf-response)
(match-string 1 cf-response)))
(defun cf-login (uname psswd remember)
(setq cf-response
(shell-command-to-string
(format "curl --silent --cookie-jar %s '%s://%s/enter'"
cf-cookies-file
cf-proto cf-host)))
(setq cf-csrf-token (cf-get-csrf-token cf-response))
(setq cf-response
(shell-command-to-string
(format "curl --location --silent --cookie-jar %s --cookie %s --data 'action=enter&handle=%s&password=%s&remember=%s&csrf_token=%s' '%s://%s/enter'"
cf-cookies-file cf-cookies-file
uname psswd remember cf-csrf-token
cf-proto cf-host))
)
(if (string-match "\"error for__password\"" cf-response)
'nil
't))
(defun cf-submit(contest problem solution language)
(setq cf-csrf-token
(cf-get-csrf-token
(shell-command-to-string
(format "curl --silent --cookie-jar %s --cookie %s '%s://%s/contest/%s/submit'"
cf-cookies-file cf-cookies-file
cf-proto cf-host contest))))
(setq temp-file (make-temp-file "cfparser"))
(with-temp-file temp-file (insert solution))
(setq cf-response
(shell-command-to-string
(format
"curl --location --silent --cookie-jar %s --cookie %s -F 'csrf_token=%s' -F 'action=submitSolutionFormSubmitted' -F 'submittedProblemIndex=%s' -F 'programTypeId=%s' -F \"source=@%s\" '%s://%s/contest/%s/submit?csrf_token=%s'"
cf-cookies-file cf-cookies-file
cf-csrf-token
problem
language temp-file
cf-proto cf-host contest cf-csrf-token
))))
(defun cf-parse-tests(page)
(let ((input_regex "<div class=\"input\">.*?<pre>\\(.*?\\)</pre></div>")
(output_regex "<div class=\"output\">.*?<pre>\\(.*?\\)</pre></div>")
(from 0)
(input "")
(output "")
(result '()))
(while (setq from (string-match input_regex page from))
(setq input (match-string 1 page))
(setq from (string-match output_regex page from))
(setq output (match-string 1 page))
(setq input (replace-regexp-in-string "<br[^>]*?>" "\n" input))
(setq output (replace-regexp-in-string "<br[^>]*?>" "\n" output))
(push (list input output) result))
result))
(defun cf-get-tests(contest problem)
(setq cf-response
(shell-command-to-string
(format "curl --silent --cookie-jar %s --cookie %s '%s://%s/contest/%s/problem/%s'"
cf-cookies-file cf-cookies-file
cf-proto cf-host contest problem)))
(cf-parse-tests cf-response))
(defun cf-logout ()
(when (file-exists-p cf-cookies-file)
(delete-file cf-cookies-file)))
(defun cf-submission-vector (handle)
(setq cf-response
(shell-command-to-string
(format
"curl --location --silent '%s://%s/api/user.status?handle=%s&from=1&count=5'"
cf-proto cf-host handle)))
(setq json-response
(let ((json-key-type 'string))
(json-read-from-string cf-response)))
(cdr (assoc '"result" json-response)))