(module isbndb (api-key database-url isbn->alist) (import chicken scheme) (use srfi-1 sxml-transforms sxpath sxpath-lolevel intarweb http-client ssax uri-common) (define api-key (make-parameter "")) (define database-url (make-parameter "http://isbndb.com")) (define (make-query isbn) (when (equal? "" (api-key)) (error "Please provide your own api-key with the api-key parameter")) (update-uri (uri-reference (database-url)) path: '("api" "books.xml") query: `((index1 . "isbn") (value1 . ,isbn) (access_key . ,(api-key))))) (define (search isbn) (let-values (((content uri response) (with-input-from-request (make-query isbn) #f (lambda () (ssax:xml->sxml (current-input-port) '()))))) (close-connection! uri) content)) (define isbndb->scheme-rules `((Title . ,(lambda (tag title) `(title . ,title))) (AuthorsText . ,(lambda (tag . authors) `(authors ,authors))) (PublisherText . ,(lambda (tag . pubtext) `(publisher . ,(cdr pubtext)))) (TitleLong . ,(lambda (tag . longtitle) `(subtitle . ,longtitle))) (*text* . ,(lambda (tag t) t)) (*default* . ,(lambda x x)))) (define (isbn->alist isbn) (cons `(isbn . ,isbn) (filter (lambda (x) (not (null? (cdr x)))) ; ignore empty tags (pre-post-order (sxml:child-elements ((sxpath '(// BookList BookData)) (search isbn))) isbndb->scheme-rules)))))