Skip to content

Instantly share code, notes, and snippets.

@Johniel
Created April 7, 2012 09:14
Show Gist options
  • Save Johniel/2326738 to your computer and use it in GitHub Desktop.
Save Johniel/2326738 to your computer and use it in GitHub Desktop.
(require 'cl)
(require 'deferred)
(require 'http-get)
(require 'xml)
(require 'url)
(setq aoj-url "http://judge.u-aizu.ac.jp/onlinejudge")
(setq aoj-passwd "pass")
(setq aoj-id "id")
(defun aoj-user-info (id pass)
(setq aoj-passwd pass)
(setq aoj-id id))
(defun parse-probNO ()
(car (split-string (buffer-name (current-buffer)) "[.]")))
(defun parse-language ()
(assoc-default
(car (cdr (split-string (buffer-name (current-buffer)) "[.]")))
'(("c" . "C") ("cpp" . "C++") ("cc" . "C++") ("java" . "JAVA") (nil . "C"))))
(defun aoj-sumbit ()
(interactive)
(let ((post-data (concat "userID=" (http-url-encode aoj-id 'utf-8) "&"
"password=" (http-url-encode aoj-passwd 'utf-8) "&"
"problemNO=" (http-url-encode (parse-probNO) 'utf-8) "&"
"language=" (http-url-encode (parse-language) 'utf-8) "&"
"sourceCode=" (http-url-encode (buffer-string) 'utf-8))))
(deferred:$
(deferred:process-buffer
"wget" "-q" "-O-"
(concat "--post-data=" post-data)
(concat aoj-url "/servlet/Submit"))
(deferred:error it
(lambda (err)
(insert "// ERROR: " err))))))
(defun remove-char (s c)
(mapconcat 'identity (split-string s c) ""))
(defun print-format (result)
(if (listp result)
(remove-char
(let ((run-id (nth 2 (nth 3 result)))
(user-id (nth 2 (nth 5 result)))
(prob-id (nth 2 (nth 7 result)))
(sub-date (nth 2 (nth 9 result)))
(stats (nth 2 (nth 11 result)))
(lang (nth 2 (nth 13 result)))
(cpu-time (nth 2 (nth 15 result)))
(memory (nth 2 (nth 17 result)))
(code-size (nth 2 (nth 19 result))))
(format "%14s | %04s | %24s | %8s | %8s | %8s | %10s"
(substring user-id 0 -1)
(substring prob-id 0 -1)
(substring stats 0 -1)
(substring lang 0 -1)
(substring cpu-time 0 -1)
(substring memory 0 -1)
(substring code-size 0 -1)))
"\n")
""))
(defun get-last20sub-buffer (buff-name)
(let ((buff (get-buffer buff-name)))
(if (and (bufferp buff) (buffer-live-p buff))
buff
(generate-new-buffer buff-name))))
(defun aoj-last20sub ()
(interactive)
(deferred:$
(deferred:url-get (concat aoj-url "/webservice/status_log"))
(deferred:nextc it
(lambda (buff)
(switch-to-buffer (get-last20sub-buffer "last20sub"))
(erase-buffer)
(insert (with-current-buffer buff
(mapconcat
'print-format
(cdddr (car (xml-parse-region (point-min) (point-max))))
"\n")))
(goto-char (point-min))
(kill-buffer buff)))
(deferred:error it
(lambda (err)
(insert "ERROR: " err)))))
(provide 'aoj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment