Scripts/define.lisp
2022-03-30 23:20:58 +02:00

21 lines
1005 B
Common Lisp

(asdf:load-system :drakma)
(asdf:load-system :yason)
(defun wiktionary-query (search-string)
(let ((query-string (format nil "https://en.wikipedia.org/w/rest.php/v1/search/page?q=~A&limit=1" search-string)))
(let ((http-result (handler-case (drakma:http-request query-string)
(usocket:ns-host-not-found-error (c)
(princ "Host not found.")
nil))))
(let ((json (yason:parse (coerce (map 'list #'code-char http-result) 'string))))
(let ((results (gethash "pages" json)))
(if (null results)
(princ "No results found.")
(let ((page-query-string (format nil "https://en.wikipedia.org/api/rest_v1/page/summary/~A" (gethash "title" (car results)))))
(let ((page-result (handler-case (drakma:http-request page-query-string)
(usocket:ns-host-not-found-error (c)
(princ "Host not found when retreiving data.")
nil))))
(let ((json (yason:parse (map 'string #'code-char page-result))))
(princ (gethash "extract" json)))))))))))