Skip to content

Instantly share code, notes, and snippets.

@ayato-p
Created September 3, 2013 13:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayato-p/6423583 to your computer and use it in GitHub Desktop.
Save ayato-p/6423583 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/gosh
(use rfc.http)
(use rfc.uri)
(use rfc.json)
(use net.twitter)
;; twitter
(define *cred* (make <twitter-cred>
:consumer-key "XXXX"
:consumer-secret "XXXX"
:access-token "XXXX"
:access-token-secret "XXXX"))
;;http://ja.wikipedia.org/w/api.php?action=query&list=random&format=xml
(define (wikipedia-random)
(let1 jsonp
(receive (status head body)
(http-get "jp.wikipedia.org"
(string-append "/w/api.php"
"?action=query"
"&list=random"
"&rnnamespace=0"
"&format=json"))
(parse-json-string
(string-scan body "</html>" 'after)))
(let1 param (ref (cdadar jsonp) 0)
(let ((id (assoc-ref param "id"))
(title (assoc-ref param "title")))
(values id title)))))
(define (get-wikipedia-url id)
(let1 jsonp (receive (status head body)
(http-get "jp.wikipedia.org"
(string-append "/w/api.php"
"?action=query"
"&pageids=" (number->string id)
"&prop=info"
"&inprop=url"
"&format=json"))
(parse-json-string
(string-scan body "</html>" 'after)))
(assoc-ref (car (cdadar jsonp)) "fullurl")))
(define (main args)
(receive (id title)
(wikipedia-random)
(twitter-update *cred*
(string-join `(,title ,(get-wikipedia-url id))
"\n"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment