This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsss.lisp
260 lines (221 loc) · 9.14 KB
/
rsss.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
;; This program is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the
;; Free Software Foundation, either version 3 of the License, or (at your
;; option) any later version.
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE.
;; See the GNU General Public License for more details.
;; -----------------
(defpackage :rsss
(:use :cl)
(:export
:parse
:entries :name :uri :date :desc :author :text :media))
(in-package :rsss)
;; —————————————————————————————————————
;; CLASSES
;; —————————————————————————————————————
(defclass feed ()
((uri :initarg :uri :accessor uri :initform nil)
(name :initarg :name :accessor name :initform nil)
(date :initarg :date :accessor date :initform nil)
(desc :initarg :desc :accessor desc :initform nil)
(entries :initarg :entries :accessor entries :initform nil)))
(defclass entry ()
((uri :initarg :uri :accessor uri :initform nil)
(name :initarg :name :accessor name :initform nil)
(date :initarg :date :accessor date :initform nil)
(author :initarg :author :accessor author :initform nil)
(desc :initarg :desc :accessor desc :initform nil)
(media :initarg :media :accessor media :initform nil)
(text :initarg :text :accessor text :initform nil)))
;; —————————————————————————————————————
;; MISC
;; —————————————————————————————————————
(defmacro append-or-replace (list item)
"If a list is empty (nil), then replace it with a new list containing item.
Otherwise, append item to the pre-existing list.
Side-effectively, with nconc et. al."
`(if (nilp ,list)
(setf ,list (list ,item))
(nconc ,list (list ,item))))
(defmacro mapnil (function list)
"Map over a list with a function, but remove all NILs from the result list."
`(remove nil (mapcar ,function ,list)))
(defmacro mapfirst (function list)
"Map over a list with a function, and return the first non-NIL result."
`(car (mapnil ,function ,list)))
;; VARYING LIST → LIST
(defun str-assoc (item list)
"Run #'assoc, but with #'string-equal as the test function."
(assoc item list :test #'string-equal))
;; VARYING → BOOLEAN
(defun nilp (item)
"Return whether or note an item is eq to NIL."
(eq nil item))
;; LIST → VARYING
(defun ie-car (item)
"Try car'ing something… but don't sweat it if, y'know, it fucks."
(ignore-errors (car item)))
;; —————————————————————————————————————
;; PARSING
;; —————————————————————————————————————
;; STRING → RSSS:FEED
(defun parse (xml)
"Parse a given XML string (atom/rss[12]) into a FEED object."
(let* ((node (xmls:parse xml))
(type (feed-type node)))
(cond ((or (eq type :rss2) (eq type :rss1))
(parse-rss node))
((eq type :atom)
(parse-atom node)))))
;; -----------------
(defmacro common-let (node extra-let form &optional extra-form)
"A let-statement used by basically every parsing-function/macro."
`(let ((name (xmls:node-name ,node))
(chchild (xmls:node-children ,node))
(attrs (xmls:node-attrs ,node))
,@extra-let)
,form))
;; —————————————————
;; ATOM PARSING
;; —————————————————
(defmacro parse-atom-children (rsss parent-node child-node
&optional (cond-1 '(T nil))
(cond-2 '(T nil)))
"Code common to parsing both overarching Atom XML and individual entries."
`(mapcar
(lambda (,child-node)
(common-let ,child-node nil
(cond ((string-equal "link" name)
(setf (uri ,rsss) (cadr (str-assoc "href" attrs))))
((string-equal "title" name)
(setf (name ,rsss) (car chchild)))
((string-equal "updated" name)
(setf (date ,rsss) (car chchild)))
((string-equal "summary" name)
(setf (desc ,rsss) (car chchild)))
,cond-1 ,cond-2)))
;; nil))
(xmls:node-children ,parent-node)))
;; -----------------
;; XMLS:NODE → RSSS:FEED
(defun parse-atom (atom-node)
"Parse Atom XMLS node into an rsss FEED object."
(let ((feed (make-instance 'feed)))
(parse-atom-children
feed atom-node atom-child
((string-equal "entry" name)
(append-or-replace (entries feed) (parse-atom-entry atom-child))))
feed))
;; XMLS:NODE → RSSS:ENTRY
(defun parse-atom-entry (entry-node)
"Parse an Atom <entry>'s XMLS:NODE into an RSSS:ENTRY object."
(let ((entry (make-instance 'entry)))
(parse-atom-children
entry entry-node entry-child
((string-equal "content" name)
(setf (text entry) (car chchild)))
((string-equal "author" name)
(setf (author entry)
(parse-atom-author-name entry-child))))
entry))
;; -----------------
;; XMLS:NODE → STRING
(defun parse-atom-author-name (author-node)
"Return the proper name of an author, given an Atom <author> node."
(common-let author-node nil
(if (stringp chchild) chchild
(mapfirst
(lambda (chchchild)
(if (string-equal "name" (xmls:node-name chchchild))
(car (xmls:node-children chchchild))))
chchild))))
;; —————————————————
;; RSS1/RSS2 PARSING
;; —————————————————
(defmacro parse-rss-children (rsss parent-node child-node
&optional (cond-1 '(T nil))
(cond-2 '(T nil))
(cond-3 '(T nil))
(cond-4 '(T nil)))
"Some code common to parsing the children of rss nodes."
`(mapcar
(lambda (,child-node)
(common-let ,child-node nil
(cond ((string-equal "title" name)
(setf (name ,rsss) (ie-car chchild)))
((string-equal "pubDate" name)
(setf (date ,rsss) (ie-car chchild)))
((string-equal "date" name)
(setf (date ,rsss) (ie-car chchild)))
((string-equal "link" name)
(setf (uri ,rsss) (ie-car chchild)))
,cond-1 ,cond-2 ,cond-3 ,cond-4)))
(xmls:node-children ,parent-node)))
;; -----------------
;; XMLS:NODE → RSSS:FEED
(defun parse-rss (rss-node)
"Parse an RSS XMLS node into an rsss:FEED object."
(let ((feed (make-instance 'feed)))
(mapcar
(lambda (rss-child)
(let ((name (xmls:node-name rss-child)))
(cond ((string-equal "channel" name)
(parse-rss-channel feed rss-child))
((string-equal "item" name)
(append-or-replace
(entries feed) (parse-rss-item rss-child))))))
(xmls:node-children rss-node))
feed))
;; RSSS:FEED XMLS:NODE → NIL
(defun parse-rss-channel (feed channel-node)
"Parse a channel node of an RSS feed; modifies the FEED object."
(parse-rss-children
feed channel-node channel-child
((string-equal "description" name)
(setf (desc feed) (ie-car chchild)))
((string-equal "item" name)
(append-or-replace (entries feed) (parse-rss-item channel-child))))
feed)
;; XMLS:NODE → RSSS:ENTRY
(defun parse-rss-item (entry-node)
"Parse an item (XMLS:NODE) of an RSS feed."
(let ((entry (make-instance 'entry)))
(parse-rss-children
entry entry-node entry-child
((or (string-equal "content" name) (string-equal "encoded" name))
(setf (text entry) (ie-car chchild)))
;; about the following: people use <description> tags for both summaries
;; and for actual post-bodies. (wtf :/)
;; so, if the text is longer than 250 characters, it's *probably* not
;; a summary, but an actual post. then again, not all posts are *that*
;; long…
;; this is a hack that won't always be helpful or effective, it's the
;; best trade-off I could think of. sorry ♥
((string-equal "description" name)
(if (and (< 250 (length (ie-car chchild))) (not (text entry)))
(setf (text entry) (ie-car chchild))
(setf (desc entry) (ie-car chchild))))
((string-equal "enclosure" name)
(setf (media entry) (cadr (str-assoc "url" attrs))))
((or (string-equal "author" name) (string-equal "creator" name))
(setf (author entry) (ie-car chchild))))
entry))
;; —————————————————————————————————————
;; PRE-PARSING
;; —————————————————————————————————————
;; STRING → SYMBOL
(defun feed-type (node)
"Return the type of the feed-- :rss2 for RSS 2.0, :rss1 for RSS 1.0/other,
and :atom for (obviously!) Atom."
(let ((name (xmls:node-name node))
(attrs (xmls:node-attrs node)))
(cond ((and (string-equal "rss" name)
(equal "2.0" (cadr (assoc "version" attrs :test #'equal))))
:rss2)
((or (string-equal "rss" name) (string-equal "rdf" name))
:rss1)
((string-equal "feed" name)
:atom))))