-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathia-api.lisp
95 lines (80 loc) · 2.92 KB
/
ia-api.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(in-package :ia-browser)
;;;
;;; Functions to search the Internet Archive
;;;
(defun do-texts-search (query rows page)
"Run a search on texts only"
(do-search query rows page '("texts")))
(defun do-search (query rows page mediatypes)
"Run a search and return the results. Mediatypes is an array."
(let* ((params (list
; (cons "q" query)
(cons "output" "json")
(cons "rows" rows)
(cons "page" page)))
(q query))
(loop
:for type :in mediatypes
:do (setf q (format nil "~a mediatype:~a" q type)))
(push (cons "q" q) params)
(let ((uri (quri:render-uri (quri:make-uri
:scheme "https"
:host "archive.org"
:path "/advancedsearch.php"
:query params))))
(decode-json-response (drakma:http-request uri :preserve-uri t)))))
;;;
;;; Functions to fetch information about individual items on the Internet Archive
;;;
(defun fetch-thumbnail (identifier)
"Fetch an appropriate thumbnail for the given identifier from archive.org"
(let ((uri (quri:render-uri
(quri:make-uri :scheme "https"
:host "archive.org"
:path (format nil "/services/img/~a" identifier)))))
(drakma:http-request uri :preserve-uri t)))
(defun fetch-metadata (identifier)
"Fetch metadata about a particular identifier from archive.org"
(let ((uri (quri:render-uri
(quri:make-uri :scheme "https"
:host "archive.org"
:path (format nil "/metadata/~a" identifier)))))
(decode-json-response (drakma:http-request uri :preserve-uri t))))
;;;
;;; Functions for dealing with the results of searches
;;;
(defun get-num-found-from-results (results)
"Get the number of results found from do-search"
(cdr (assoc :num-found (cdr (assoc :response results)))))
(defun get-docs-from-results (results)
"Given results (as returned by do-search), hand back the :DOCS section"
(cdr (assoc :docs (cdr (assoc :response results)))))
(defun get-title (doc)
"Given a single document (one element of get-docs-from-results), return the title"
(cdr (assoc :title doc)))
(defun get-year (doc)
"Given a single document, return the year"
(cdr (assoc :year doc)))
(defun get-description (doc)
"Given a single document, return the description"
(cdr (assoc :description doc)))
(defun get-ia-identifier (doc)
"Given a single document, return the identifier"
(cdr (assoc :identifier doc)))
;;;
;;; Functions for dealing with metadata
;;;
(defun get-metadata-field (field obj)
"Fetch the named field from the metadata object"
(let ((meta (cdr (assoc :metadata obj))))
(cdr (assoc field meta))))
(defun get-file-info (format metadata)
"Get info block for a file of the given format in the metadata object"
(let ((files (cdr (assoc :files metadata))))
(find-format-in-file-list format files)))
(defun find-format-in-file-list (format list)
(if list
(if (equal format (cdr (assoc :format (car list))))
(car list)
(find-format-in-file-list format (cdr list)))
nil))