Chaton > Archives > 2009/05/15<< 2009/05/14 | Back to the chat room | 2009/05/16 >> |
えーっとですね、APIでアクセスしたい人は、まず $room-url/apilogin というcgiにPOSTアクセスをかけてください。 ここなら$room-urlは http://practical-scheme.net/chaton/gauche ですね。 POSTで渡すパラメータはひとつ。'who' という名前で、そちらのクライアントプログラムの名前とかバージョンとか簡単に。 このcgiは返事にS式のalistを返します。 ((post-uri . <post-uri>) (comet-uri . <comet-uri>) (cid . <cid>) (pos . <pos>)) 順不同。後で項目が追加されるかも。 post-uriはpostするためのcgiのurl。comet-uriはlong pollするためのurl cidはこのあとcomet-uriにGETするときに渡す値。 posは現在のログの最新位置で、現在の最新位置以降を取りたい場合はこの値をpの値として使ってください。最初のcometアクセスでアクティブログを全部取りたい場合はこの数値を無視してp=0を渡してください。#
(use gauche.parameter)
(use rfc.http)
(use rfc.uri)
(use srfi-27)
(use text.tree)
(use util.list)
(define-class <chaton-config> ()
((client :init-keyword :client
:accessor client-of :init-value #f)
(url :init-keyword :url
:accessor url-of :init-value #f)
(login :init-keyword :login
:accessor login-of :init-value #f)))
(define *chaton-config*
(make-parameter
(make <chaton-config> :client "TestChatonRader"
:url "http://practical-scheme.net/chaton/chaton"
:login "http://practical-scheme.net/chaton/chaton/apilogin")))
(define (make-mime alist)
(let1 boundary (format "boundary-~a"
(number->string (* (random-integer (expt 2 64))
(sys-time) (sys-getpid))
36))
(values (tree->string
`(,(map (lambda (k&v)
`("\r\n--",boundary"\r\n"
"Content-disposition: form-data; name=\"",(car k&v)"\"\r\n\r\n"
,(x->string (cdr k&v))))
alist)
"\r\n--",boundary"--\r\n"))
boundary)))
(define (POST room-url uri params)
(receive (host path) (host&path uri)
(receive (body boundary) (make-mime params)
(receive (status hdrs body)
(http-post host path body
:mime-version "1.0"
:content-type #`"multipart/form-data; boundary=,boundary")
(unless (equal? status "200")
(cerrf room-url "POST to ~a failed with ~a" uri status))
(safe-parse room-url body)))))
(define (host&path uri)
(receive (scheme specific) (uri-scheme&specific uri)
(receive (host path q f) (uri-decompose-hierarchical specific)
(values host path))))
(define (safe-parse room-url text)
(guard (e [(<read-error> e)
(cerrf room-url "invalid reply from server: ~s" reply)])
;;(print text)
(read-from-string text)))
(define (cerrf room-url fmt . args)
(apply errorf <chaton-error> :r#(use gauche.parameter)
(use rfc.http)
(use rfc.uri)
(use srfi-27)
(use text.tree)
(use util.list)
(define-class <chaton-config> ()
((client :init-keyword :client
:accessor client-of :init-value #f)
(url :init-keyword :url
:accessor url-of :init-value #f)
(login :init-keyword :login
:accessor login-of :init-value #f)))
(define *chaton-config*
(make-parameter
(make <chaton-config> :client "TestChatonRader"
:url "http://practical-scheme.net/chaton/chaton"
:login "http://practical-scheme.net/chaton/chaton/apilogin")))
(define (make-mime alist)
(let1 boundary (format "boundary-~a"
(number->string (* (random-integer (expt 2 64))
(sys-time) (sys-getpid))
36))
(values (tree->string
`(,(map (lambda (k&v)
`("\r\n--",boundary"\r\n"
"Content-disposition: form-data; name=\"",(car k&v)"\"\r\n\r\n"
,(x->string (cdr k&v))))
alist)
"\r\n--",boundary"--\r\n"))
boundary)))
(define (POST room-url uri params)
(receive (host path) (host&path uri)
(receive (body boundary) (make-mime params)
(receive (status hdrs body)
(http-post host path body
:mime-version "1.0"
:content-type #`"multipart/form-data; boundary=,boundary")
(unless (equal? status "200")
(cerrf room-url "POST to ~a failed with ~a" uri status))
(safe-parse room-url body)))))
(define (host&path uri)
(receive (scheme specific) (uri-scheme&specific uri)
(receive (host path q f) (uri-decompose-hierarchical specific)
(values host path))))
(define (safe-parse room-url text)
(guard (e [(<read-error> e)
(cerrf room-url "invalid reply from server: ~s" reply)])
;;(print text)
(read-from-string text)))
(define (cerrf room-url fmt . args)
(apply errorf <chaton-error> :r#(define-condition-type <chaton-error> <error>
(room-url #f))
(define (fetch comet-uri pos cid)
(receive (host path) (host&path comet-uri)
(receive (code head body)
(http-get host #`",|path|?t=,(sys-time)&p=,|pos|&c=,|cid|")
body)))
(define (main args)
(and-let* ((sx (POST (url-of (*chaton-config*))
(login-of (*chaton-config*))
(list (cons "who" (client-of (*chaton-config*))))))
(post-uri (assoc-ref sx 'post-uri))
(comet-uri (assoc-ref sx 'comet-uri))
(cid (assoc-ref sx 'cid))
(pos (assoc-ref sx 'pos)))
(fetch comet-uri pos cid)))#$ gosh chaton-client.scm
*** SYSTEM-ERROR: read failed on #<iport (socket input #<socket (connect "66.33.214.68:9993")>) 0x77cab0>: Connection reset by peer
Stack Trace:
_______________________________________
0 reader
1 (rfc822-header->list remote)
At line 278 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
2 (receive-header in)
At line 224 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
3 (with-error-handler (lambda (e) (let ((e e)) (%guard-rec e e (else ...
[unknown location]
4 (request-response request conn host request-uri request-body opts)
At line 179 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
5 (http-get host (string-append "" (x->string path) "?t=" (x->string ...
At line 67 of "./chaton-client.scm"#(define (fetch comet-uri pos cid)
(receive (host path) (host&path comet-uri)
(receive (code head body)
(http-get #?=host #?=#`",|path|?s=1&t=,(sys-time)&p=,|pos|&c=,|cid|")
body)))
(define (main args)
(and-let* ((sx (POST (url-of (*chaton-config*))
(login-of (*chaton-config*))
(list (cons "who" (client-of (*chaton-config*))))))
(post-uri (assoc-ref sx 'post-uri))
(comet-uri (assoc-ref sx 'comet-uri))
(cid (assoc-ref sx 'cid))
(pos (assoc-ref sx 'pos)))
(print (fetch comet-uri pos cid))))#$ gosh chaton-client.scm
#?=host
#?- "practical-scheme.net:9993"
#?=(string-append "" (x->string path) "?s=1&t=" (x->string (sys- ...
#?- "/?s=1&t=1242385891&p=16840&c=155141783"
*** SYSTEM-ERROR: read failed on #<iport (socket input #<socket (connect "66.33.214.68:9993")>) 0x78f000>: Connection reset by peer
Stack Trace:
_______________________________________
0 reader
1 (rfc822-header->list remote)
At line 278 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
2 (receive-header in)
At line 224 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
3 (with-error-handler (lambda (e) (let ((e e)) (%guard-rec e e (else ...
[unknown location]
4 (request-response request conn host request-uri request-body opts)
At line 179 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
5 (http-get (debug-print host) (debug-print (string-append "" (x->st ...
At line 67 of "./chaton-client.scm"
6 (fetch comet-uri pos cid)
At line 78 of "./chaton-client.scm"$ gosh chaton-client.scm *** CHATON-ERROR: POST to http://practical-scheme.net/chaton/chaton/apilogin failed with 500 Stack Trace: _______________________________________ 0 (cerrf room-url "POST to ~a failed with ~a" uri status) At line 44 of "./chaton-client.scm" 1 (POST (url-of (*chaton-config*)) (login-of (*chaton-config*)) (lis ... At line 71 of "./chaton-client.scm"
gosh chaton-client.scm
#?=host
#?- "practical-scheme.net:9993"
#?=(string-append "" (x->string path) "?t=" (x->string (sys-time ...
#?- "/?t=1242386504&p=20092&c=208201887"
*** SYSTEM-ERROR: read failed on #<iport (socket input #<socket (connect "66.33.214.68:9993")>) 0x78f000>: Connection reset by peer
Stack Trace:
_______________________________________
0 reader
1 (rfc822-header->list remote)
At line 278 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
2 (receive-header in)
At line 224 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
3 (with-error-handler (lambda (e) (let ((e e)) (%guard-rec e e (else ...
[unknown location]
4 (request-response request conn host request-uri request-body opts)
At line 179 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
5 (http-get (debug-print host) (debug-print (string-append "" (x->st ...
At line 67 of "./chaton-client.scm"
6 (fetch comet-uri pos cid)
At line 78 of "./chaton-client.scm"#$ gosh chaton-client.scm
#?=host
#?- "practical-scheme.net:9993"
#?=(string-append "" (x->string path) "?t=" (x->string (sys-time ...
#?- "/?t=1242386805&p=21792&c=98681268"
*** SYSTEM-ERROR: read failed on #<iport (socket input #<socket (connect "66.33.214.68:9993")>) 0x78f000>: Connection reset by peer
Stack Trace:
_______________________________________
0 reader
1 (rfc822-header->list remote)
At line 278 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
2 (receive-header in)
At line 224 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
3 (with-error-handler (lambda (e) (let ((e e)) (%guard-rec e e (else ...
[unknown location]
4 (request-response request conn host request-uri request-body opts)
At line 179 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
5 (http-get (debug-print host) (debug-print (string-append "" (x->st ...
At line 67 of "./chaton-client.scm"
6 (fetch comet-uri pos cid)
At line 78 of "./chaton-client.scm"#$ gosh chaton-client.scm
#?=host
#?- "practical-scheme.net:9993"
#?=(string-append "" (x->string path) "?t=" (x->string (sys-time ...
#?- "/?t=1242387083&p=23656&c=232710385"
*** SYSTEM-ERROR: read failed on #<iport (socket input #<socket (connect "66.33.214.68:9993")>) 0x10625a10>: Software caused connection abort
Stack Trace:
_______________________________________
0 reader
1 (rfc822-header->list remote)
At line 278 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
2 (receive-header in)
At line 224 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
3 (with-error-handler (lambda (e) (let ((e e)) (%guard-rec e e (else ...
[unknown location]
4 (request-response request conn host request-uri request-body opts)
At line 179 of "/usr/local/share/gauche/0.8.14/lib/rfc/http.scm"
5 (http-get (debug-print host) (debug-print (string-append "" (x->st ...
At line 67 of "./chaton-client.scm"
6 (fetch comet-uri pos cid)
At line 78 of "./chaton-client.scm"#404 Not Found The given session key or entry name is wrong, or the session is expired.#
$ gosh chaton-client.scm
#?=host
#?- "practical-scheme.net:9993"
#?=(string-append "" (x->string path) "?t=" (x->string (sys-time ...
#?- "/?t=1242388566&p=26975&c=49315108"
{"pos":27035,"nc":6,"ver":"kjod0a","cid":49315108,"text":"<div class=\"entry-header\"><span class=\"timestamp\">2009/05/15 04:56:27 PDT</span\n><span class=\"chatter\">えんどう</span\n></div\n><a class=\"permalink-anchor\" id=\"anchor-entry-4a0d586b-3a228\" href=\"http://practical-scheme.net/chaton/chaton/a/2009/05/15#entry-4a0d586b-3a228\" name=\"http://practical-scheme.net/chaton/chaton/a/2009/05/15#entry-4a0d586b-3a228\" target=\"_parent\">#</a\n><div class=\"entry-single\" id=\"entry-4a0d586b-3a228\"><span>test</span\n></div\n>"}#$ gosh chaton-client.scm
#?=host
#?- "practical-scheme.net:9993"
#?=(string-append "" (x->string path) "?s=1&t=" (x->string (sys- ...
#?- "/?s=1&t=1242388652&p=28212&c=186360003"
((pos . 28269) (nc . 7) (ver . "kjod0a") (cid . 186360003) (text ("えんどう" (1242388657 531412) "a")))#(define (fetch comet-uri pos cid)
(receive (host path) (host&path comet-uri)
(receive (code head body)
(http-get #?=host #?=#`",|path|?s=1&t=,(sys-time)&p=,|pos|&c=,|cid|")
(let* ((sx (call-with-input-string body (cut read <>)))
(pos2 (assoc-ref sx 'pos))
(cid2 (assoc-ref sx 'cid))
(text (assoc-ref sx 'text)))
(print text)
(fetch comet-uri pos2 cid2)))))#(use gauche.parameter)
(use gauche.process)
(use rfc.http)
(use rfc.uri)
(use srfi-27)
(use text.tree)
(use util.list)
(use util.match)
(define-class <chaton-config> ()
((client :init-keyword :client
:accessor client-of :init-value #f)
(url :init-keyword :url
:accessor url-of :init-value #f)
(login :init-keyword :login
:accessor login-of :init-value #f)
(npath :init-keyword :npath
:accessor npath-of :init-value #f)
(ipath :init-keyword :ipath
:accessor ipath-of :init-value #f)))
(define *chaton-config*
(make-parameter
(make <chaton-config> :client "TestChatonRader"
:url "http://practical-scheme.net/chaton/chaton"
:login "http://practical-scheme.net/chaton/chaton/apilogin"
:npath "/usr/local/bin/growlnotify"
:ipath "/Users/yasuyuki/archives/chaton-room-gauche.gif")))
(define (make-mime alist)
(let1 boundary (format "boundary-~a"
(number->string (* (random-integer (expt 2 64))
(sys-time) (sys-getpid))
36))
(values (tree->string
`(,(map (lambda (k&v)
`("\r\n--",boundary"\r\n"
"Content-disposition: form-data; name=\"",(car k&v)"\"\r\n\r\n"
,(x->string (cdr k&v))))
alist)
"\r\n--",boundary"--\r\n"))
boundary)))
(define (POST room-url uri params)
(receive (host path) (host&path uri)
(receive (body boundary) (make-mime params)
(receive (status hdrs body)
(http-post host path body
:mime-version "1.0"
:content-type #`"multipart/form-data; boundary=,boundary")
(unless (equal? status "200")
(cerrf room-url "POST to ~a failed with ~a" uri status))
(safe-parse room-url body)))))
(define (host&path uri)
(receive (scheme specific) (uri-scheme&specific uri)
(receive (host path q f) (uri-decompose-hierarchical specific#(define (safe-parse room-url text)
(guard (e [(<read-error> e)
(cerrf room-url "invalid reply from server: ~s" reply)])
;;(print text)
(read-from-string text)))
(define (cerrf room-url fmt . args)
(apply errorf <chaton-error> :room-url room-url fmt args))
(define-condition-type <chaton-error> <error>
(room-url #f))
(define (send-notify config name body)
(let ((client (client-of config))
(npath (npath-of config))
(ipath (ipath-of config)))
(process-output->string
(list npath "-i" ipath "-t" name "-m" body))))
(define (notify name body)
(print "name=" name ", body=" body))
(define (fetch comet-uri pos cid)
(receive (host path) (host&path comet-uri)
(receive (code head body)
(http-get host #`",|path|?s=1&t=,(sys-time)&p=,|pos|&c=,|cid|")
(let* ((sx (call-with-input-string body (cut read <>)))
(pos2 (assoc-ref sx 'pos))
(cid2 (assoc-ref sx 'cid))
(text (assoc-ref sx 'text)))
(match text [((name (sec mil) body)) (send-notify (*chaton-config*) name body)])
(fetch comet-uri pos2 cid2)))))
(define (main args)
(and-let* ((sx (POST (url-of (*chaton-config*))
(login-of (*chaton-config*))
(list (cons "who" (client-of (*chaton-config*))))))
(post-uri (assoc-ref sx 'post-uri))
(comet-uri (assoc-ref sx 'comet-uri))
(cid (assoc-ref sx 'cid))
(pos (assoc-ref sx 'pos)))
(print (fetch comet-uri pos cid))))#$ gosh chaton-client.scm
*** ERROR: : no matching clause for ()
Stack Trace:
_______________________________________
0 (match:error text)
[unknown location]
1 (fetch comet-uri pos cid)
At line 101 of "./chaton-client.scm"#$ gosh chaton-rader.scm
*** ERROR: : no matching clause for ()
Stack Trace:
_______________________________________
0 (match:error text)
[unknown location]
1 (fetch comet-uri pos cid)
At line 98 of "./chaton-rader.scm"#$ /usr/local/bin/growlnotify -a "test" -t "test" -m "mes"
2009-05-16 08:23:55.913 growlnotify[3856:10b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
2009-05-16 08:23:55.917 growlnotify[3856:10b] Stack: (
2499616939,
2449456699,
2499616395,
2499616458,
2520508681,
2520507896,
12843
)
Trace/BPT trap#<< 2009/05/14 | Back to the chat room | 2009/05/16 >> |