git.fiddlerwoaroof.com
Browse code

Add custom errors and functio to filter feed items

fiddlerwoaroof authored on 28/03/2017 23:08:24
Showing 3 changed files
... ...
@@ -11,8 +11,14 @@
11 11
 (defgeneric belongs-to (feed-entity)
12 12
   (:documentation "Returns the person responsible for this feed item"))
13 13
 
14
+(define-condition feed-type-unsupported (error)
15
+  ((%type :initarg :type :reader feed-type)
16
+   (%feed-link :initarg :feed-link :reader feed-link)))
17
+
14 18
 (defgeneric -to-feed (doc type &key feed-link)
15
-  (:documentation "Given an xml-document, return a feed object"))
19
+  (:documentation "Given an xml-document, return a feed object")
20
+  (:method (doc type &key feed-link)
21
+    (error 'feed-type-unsupported :type type :feed-link feed-link)))
16 22
 
17 23
 (defgeneric render (object renderer)
18 24
   (:documentation "Given a lisp object representing a feed, return it rendered
... ...
@@ -211,6 +217,12 @@
211 217
     (push-item feed it)
212 218
     (values feed it)))
213 219
 
220
+(defun filter-feed (feed function &key key)
221
+  (setf (items feed)
222
+	(remove-if-not function (items feed)
223
+		       :key key))
224
+  feed)
225
+
214 226
 (defun shorten-link (link)
215 227
   (let ((link (cl-ppcre:regex-replace "^https?:" link "")))
216 228
     (subseq link 0 (min 30 (length link)))))
... ...
@@ -13,10 +13,11 @@
13 13
 
14 14
 (defpackage #:alimenta
15 15
   (:use #:cl #:should-test #:lquery #:plump #:alexandria #:anaphora #:collection-class)
16
-  (:export #:to-feed #:generate-xml #:feed #:title #:link #:items #:feed-link
16
+  (:export #:to-feed #:generate-xml #:feed #:title #:link #:items #:feed-link #:feed-type
17 17
            #:doc #:source-type #:id #:date #:content #:item #:description
18 18
            #:%generate-xml #:%to-feed #:get-items #:make-item #:complex-value
19
-           #:primary-value #:render #:author #:content-el))
19
+           #:primary-value #:render #:author #:content-el #:feed-type-unsupported
20
+	   #:pop-token #:filter-feed))
20 21
 
21 22
 (defpackage #:alimenta.html
22 23
   (:use #:cl #:should-test #:lquery #:alexandria #:anaphora #:alimenta #:data-class
... ...
@@ -41,7 +42,8 @@
41 42
 (defpackage #:alimenta.pull-feed 
42 43
   (:use #:cl #:alimenta #:alexandria #:anaphora #:lquery)
43 44
   (:export #:pull-feed #:fetch-doc-from-url #:fetch-feed-from-url
44
-           #:fetch-error #:feed-ambiguous #:no-feed)) 
45
+           #:fetch-error #:feed-ambiguous #:no-feed #:with-user-agent
46
+	   #:skip-feed)) 
45 47
 
46 48
 (defmethod asdf:perform ((o asdf:test-op) (s (eql (asdf:find-system :alimenta))))
47 49
   (asdf:load-system :alimenta)
... ...
@@ -144,15 +144,15 @@
144 144
   (prog1 partial
145 145
     (let ((item-root (make-element ($1 (inline partial) "channel") "item")))
146 146
       (flet ((make-element (tag) (make-element item-root tag)))
147
-        (with-slots (title id date link content) item
148
-          ($ (inline (make-element "title")) (text title)
149
-            (inline (make-element "link")) (text link)
150
-            (inline (make-element "pubDate")) (text date)
151
-            (inline (make-element "description")) (text content))    
152
-          (plump-dom:set-attribute
153
-            ($ (inline (make-element "guid")) (text id) (node))
154
-            "isPermaLink"
155
-            "false"))))))
147
+	(with-slots (title id date link content) item
148
+	  ($ (inline (make-element "title")) (text title)
149
+	     (inline (make-element "link")) (text link)
150
+	     (inline (make-element "pubDate")) (text date)
151
+	     (inline (make-element "description")) (text content))    
152
+	  (plump-dom:set-attribute
153
+	   ($ (inline (make-element "guid")) (text id) (node))
154
+	   "isPermaLink"
155
+	   "false"))))))
156 156
 
157 157
 (defmethod generate-xml ((feed feed) (feed-type (eql :rss)) &rest r)
158 158
   (declare (ignore r))