Skip to content

Commit d6a651c

Browse files
committed
fix: submission detail API modification
see: #43
1 parent 7d43d95 commit d6a651c

File tree

2 files changed

+57
-50
lines changed

2 files changed

+57
-50
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2019-10-10 Wang Kai <[email protected]>
2+
3+
* leetcode.el: submissions detail API modification
4+
15
2019-10-09 Wang Kai <[email protected]>
26

37
* leetcode.el (leetcode--fetch-problem): filter problems by regex or tag

leetcode.el

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
;; Keywords: extensions, tools
77
;; URL: https://github.com/kaiwk/leetcode.el
88
;; Package-Requires: ((emacs "26") (dash "2.16.0") (graphql "0.1.1") (spinner "1.7.3") (aio "1.0"))
9-
;; Version: 0.1.7
9+
;; Version: 0.1.8
1010

1111
;; This program is free software; you can redistribute it and/or modify
1212
;; it under the terms of the GNU General Public License as published by
@@ -79,11 +79,12 @@ The elements of :problems has attributes:
7979
:tags List")
8080

8181
(defvar leetcode--all-tags nil
82-
"Problems tags")
82+
"All problems tags.")
8383

8484
(defvar leetcode--problem-titles nil
8585
"Problem titles that have been open in solving layout.")
8686

87+
(defvar leetcode-retry-threshold 20 "`leetcode-try' or `leetcode-submit' retry times.")
8788
(defvar leetcode--filter-regex nil "Filter rows by regex.")
8889
(defvar leetcode--filter-tag nil "Filter rows by leetcode tag.")
8990

@@ -138,6 +139,7 @@ The elements of :problems has attributes:
138139
(defconst leetcode--api-try (concat leetcode--base-url "/problems/%s/interpret_solution/"))
139140

140141
(defun to-list (vec)
142+
"Convert VEC to list."
141143
(append vec '()))
142144

143145
(defun leetcode--referer (value)
@@ -270,8 +272,7 @@ USER-AND-PROBLEMS is an alist comes from
270272
problems)))))
271273

272274
(defun leetcode--set-tags (all-tags)
273-
"Set `leetcode--all-tags' and `leetcode--all-problems' with
274-
ALL-TAGS."
275+
"Set `leetcode--all-tags' and `leetcode--all-problems' with ALL-TAGS."
275276
(let-alist all-tags
276277
;; set problems tags
277278
(dolist (problem (plist-get leetcode--all-problems :problems))
@@ -417,7 +418,7 @@ Return a list of rows, each row is a vector:
417418
rows))
418419

419420
(defun leetcode--filter (rows)
420-
"Filter ROWS by `leetcode--filter-regex' and `leetcode--filter-tag'"
421+
"Filter ROWS by `leetcode--filter-regex' and `leetcode--filter-tag'."
421422
(seq-filter
422423
(lambda (row)
423424
(and
@@ -445,7 +446,7 @@ Return a list of rows, each row is a vector:
445446
(leetcode-refresh))
446447

447448
(defun leetcode-set-filter-tag ()
448-
"Set `leetcode--filter-tag'"
449+
"Set `leetcode--filter-tag'."
449450
(interactive)
450451
(setq leetcode--filter-tag
451452
(completing-read "Tags: " leetcode--all-tags))
@@ -540,12 +541,12 @@ see: https://github.com/skeeto/emacs-aio/issues/3."
540541
(testcase-buf (get-buffer leetcode--testcase-buffer-name))
541542
(slug-title (with-current-buffer code-buf
542543
(file-name-base (buffer-name))))
543-
(id (plist-get (seq-find (lambda (p)
544-
(equal slug-title
545-
(leetcode--slugify-title
546-
(plist-get p :title))))
547-
(plist-get leetcode--all-problems :problems))
548-
:id)))
544+
(cur-problem (seq-find (lambda (p)
545+
(equal slug-title
546+
(leetcode--slugify-title
547+
(plist-get p :title))))
548+
(plist-get leetcode--all-problems :problems)))
549+
(id (plist-get cur-problem :id)))
549550
(let* ((url-request-method "POST")
550551
(url-request-extra-headers
551552
`(,leetcode--User-Agent
@@ -572,42 +573,40 @@ see: https://github.com/skeeto/emacs-aio/issues/3."
572573
(with-current-buffer res-buf
573574
(erase-buffer)
574575
(insert (concat "Your input:\n" .test_case "\n\n")))
575-
;; poll expected
576-
(let ((expected_res (aio-await (leetcode--check-submission .interpret_expected_id slug-title))))
577-
(while (not expected_res)
578-
(aio-await (aio-sleep 0.5))
579-
(setq expected_res (aio-await (leetcode--check-submission .interpret_expected_id slug-title))))
580-
(let ((answer (aref (alist-get 'code_answer expected_res) 0)))
581-
(with-current-buffer res-buf
582-
(insert (concat "Expected:\n" answer "\n\n")))))
583576
;; poll interpreted
584-
(let ((actual_res (aio-await (leetcode--check-submission .interpret_id slug-title))))
585-
(while (not actual_res)
577+
(let ((actual_res (aio-await (leetcode--check-submission .interpret_id slug-title)))
578+
(retry-times 0))
579+
(while (and (not actual_res) (< retry-times leetcode-retry-threshold))
586580
(aio-await (aio-sleep 0.5))
587-
(setq actual_res (aio-await (leetcode--check-submission .interpret_id slug-title))))
588-
(let-alist actual_res
589-
(with-current-buffer res-buf
590-
(insert "Output:\n")
591-
(cond
592-
((eq .status_code 10)
593-
(insert (aref .code_answer 0)))
594-
((eq .status_code 14)
595-
(insert .status_msg))
596-
((eq .status_code 15)
597-
(insert .status_msg)
598-
(insert "\n\n")
599-
(insert .full_runtime_error))
600-
((eq .status_code 20)
601-
(insert .status_msg)
602-
(insert "\n\n")
603-
(insert .full_compile_error)))
604-
(when (> (length .code_output) 0)
605-
(insert "\n\n")
606-
(insert "Code output:\n")
607-
(dolist (item (append .code_output nil))
608-
(insert (concat item "\n"))))
609-
(insert "\n\n")
610-
(leetcode--loading-mode -1))))))))))
581+
(setq actual_res (aio-await (leetcode--check-submission .interpret_id slug-title)))
582+
(setq retry-times (1+ retry-times)))
583+
(if (< retry-times leetcode-retry-threshold)
584+
(let-alist actual_res
585+
(with-current-buffer res-buf
586+
(goto-char (point-max))
587+
(insert (concat "Expected:\n" (aref .expected_code_answer 0) "\n\n"))
588+
(insert "Output:\n")
589+
(cond
590+
((eq .status_code 10)
591+
(insert (aref .code_answer 0)))
592+
((eq .status_code 14)
593+
(insert .status_msg))
594+
((eq .status_code 15)
595+
(insert .status_msg)
596+
(insert "\n\n")
597+
(insert .full_runtime_error))
598+
((eq .status_code 20)
599+
(insert .status_msg)
600+
(insert "\n\n")
601+
(insert .full_compile_error)))
602+
(when (> (length .code_output) 0)
603+
(insert "\n\n")
604+
(insert "Code output:\n")
605+
(dolist (item (append .code_output nil))
606+
(insert (concat item "\n"))))
607+
(insert "\n\n")))
608+
(message "LeetCode try timeout.")))
609+
(leetcode--loading-mode -1)))))))
611610

612611
(aio-defun leetcode--check-submission (submission-id slug-title)
613612
"Polling to check submission detail.
@@ -764,12 +763,16 @@ following possible value:
764763
(progn (goto-char url-http-end-of-headers)
765764
(json-read))))
766765
(submission-id (alist-get 'submission_id resp))
767-
(submission-res (aio-await (leetcode--check-submission submission-id slug-title))))
766+
(submission-res (aio-await (leetcode--check-submission submission-id slug-title)))
767+
(retry-times 0))
768768
;; poll submission result
769-
(while (not submission-res)
769+
(while (and (not submission-res) (< retry-times leetcode-retry-threshold))
770770
(aio-await (aio-sleep 0.5))
771-
(setq submission-res (aio-await (leetcode--check-submission submission-id slug-title))))
772-
(leetcode--show-submission-result submission-res)
771+
(setq submission-res (aio-await (leetcode--check-submission submission-id slug-title)))
772+
(setq retry-times (1+ retry-times)))
773+
(if (< retry-times leetcode-retry-threshold)
774+
(leetcode--show-submission-result submission-res)
775+
(message "LeetCode submit timeout."))
773776
(leetcode--loading-mode -1))))))
774777

775778
(defun leetcode--problem-link (title)

0 commit comments

Comments
 (0)