Browse code
Refactoring
fiddlerwoaroof authored on 17/02/2017 07:47:34
Showing 1 changed files
Showing 1 changed files
... | ... |
@@ -40,27 +40,26 @@ |
40 | 40 |
(ubiquitous:value :feeds) |
41 | 41 |
:test #'equalp)) |
42 | 42 |
|
43 |
-(defun safe-pull-feed (feed-url) |
|
43 |
+(defun safe-pull-feed (feed-url &aux (pop-times 0)) |
|
44 | 44 |
"Handles date parsing errors in the feed: chronicity won't parse |
45 | 45 |
certain date formats, this catches the error and modifies the |
46 | 46 |
format to something chronicity can handle." |
47 |
- (let ((pop-times 0)) |
|
48 |
- (flet ((pop-50-tokens (c) |
|
49 |
- (declare (ignore c)) |
|
50 |
- (when (find-restart 'alimenta.rss::pop-token) |
|
51 |
- (if (< pop-times 50) |
|
52 |
- (progn (incf pop-times) |
|
53 |
- (format t "~&Processing error, trying to pop a token (popped ~d times)~%" |
|
54 |
- pop-times) |
|
55 |
- (alimenta.rss::pop-token)) |
|
56 |
- (continue))))) |
|
57 |
- (handler-bind ((warning #'muffle-warning) |
|
58 |
- (error #'pop-50-tokens)) |
|
59 |
- (format t "~&Tring to pull: ~a... " feed-url) |
|
60 |
- (prog1 (alimenta.pull-feed:pull-feed feed-url) |
|
61 |
- ;; Why am I decf-ing here? |
|
62 |
- (format t "... Success~%" feed-url) |
|
63 |
- (decf pop-times)))))) |
|
47 |
+ (flet ((pop-50-tokens (c) |
|
48 |
+ (declare (ignore c)) |
|
49 |
+ (when (find-restart 'alimenta:pop-token) |
|
50 |
+ (if (< pop-times 50) |
|
51 |
+ (progn (incf pop-times) |
|
52 |
+ (format t "~&Processing error, trying to pop a token (popped ~d times)~%" |
|
53 |
+ pop-times) |
|
54 |
+ (alimenta:pop-token)) |
|
55 |
+ (continue))))) |
|
56 |
+ (handler-bind ((warning #'muffle-warning) |
|
57 |
+ (error #'pop-50-tokens)) |
|
58 |
+ (format t "~&Trying to pull: ~a... " feed-url) |
|
59 |
+ (prog1 (alimenta.pull-feed:pull-feed feed-url) |
|
60 |
+ ;; Why am I decf-ing here? |
|
61 |
+ (format t "... Success~%") |
|
62 |
+ (decf pop-times))))) |
|
64 | 63 |
|
65 | 64 |
(defun skip-feed () |
66 | 65 |
(when-let ((restart (find-restart 'skip-feed))) |
... | ... |
@@ -76,32 +75,32 @@ |
76 | 75 |
(store feed pull-directory))))) |
77 | 76 |
|
78 | 77 |
(defun archive-feeds () |
79 |
- (let ((pull-time (local-time:now))) |
|
80 |
- (let* ((pull-directory (get-store-directory-name pull-time)) |
|
81 |
- (paths (pull-and-store-feeds *feeds* pull-directory))) |
|
82 |
- (with-open-file (index (merge-pathnames "index.json" pull-directory) :direction :output) |
|
83 |
- (yason:with-output (index :indent t) |
|
84 |
- (yason:with-object () |
|
85 |
- (yason:encode-object-element "pull-time" (local-time:format-timestring nil pull-time)) |
|
86 |
- (yason:encode-object-element "feed-urls" *feeds*) |
|
87 |
- (yason:with-object-element ("feeds") |
|
88 |
- (yason:with-array () |
|
89 |
- (mapcar (lambda (url feed-data) |
|
90 |
- (yason:with-object () |
|
91 |
- (yason:encode-object-element "url" url) |
|
92 |
- (when feed-data |
|
93 |
- (destructuring-bind (title path) feed-data |
|
94 |
- (yason:encode-object-element "title" title) |
|
95 |
- (yason:encode-object-element "path" |
|
96 |
- (princ-to-string |
|
97 |
- (uiop:enough-pathname path *feed-base*))))))) |
|
98 |
- *feeds* |
|
99 |
- paths))))))))) |
|
78 |
+ (let* ((pull-time (local-time:now)) |
|
79 |
+ (pull-directory (get-store-directory-name pull-time)) |
|
80 |
+ (paths (pull-and-store-feeds *feeds* pull-directory))) |
|
81 |
+ (with-open-file (index (merge-pathnames "index.json" pull-directory) :direction :output) |
|
82 |
+ (feed-index index pull-time paths)))) |
|
83 |
+ |
|
84 |
+(defun feed-index (index pull-time paths) |
|
85 |
+ (flet ((encode-feed-reference (url feed-data) |
|
86 |
+ (yason:with-object () |
|
87 |
+ (yason:encode-object-element "url" url) |
|
88 |
+ (when feed-data |
|
89 |
+ (destructuring-bind (title path) feed-data |
|
90 |
+ (yason:encode-object-element "title" title) |
|
91 |
+ (yason:encode-object-element "path" (princ-to-string |
|
92 |
+ (uiop:enough-pathname path *feed-base*)))))))) |
|
93 |
+ (yason:with-output (index :indent t) |
|
94 |
+ (yason:with-object () |
|
95 |
+ (yason:encode-object-element "pull-time" (local-time:format-timestring nil pull-time)) |
|
96 |
+ (yason:encode-object-element "feed-urls" *feeds*) |
|
97 |
+ (yason:with-object-element ("feeds") |
|
98 |
+ (yason:with-array () |
|
99 |
+ (mapcar #'encode-feed-reference *feeds* paths))))))) |
|
100 | 100 |
|
101 | 101 |
|
102 | 102 |
;; This is an ungodly mess, we need to avoid funneling everything through fix-pathname-or-skip |
103 | 103 |
(defun command-line-main (&optional (feed-list-initializer #'init-feeds)) |
104 |
- (declare (optimize (debug 3))) |
|
105 | 104 |
(labels ((feed-type-unsupported (c &key (restart 'skip-feed)) |
106 | 105 |
(format t "~&Feed type unsupported: ~a for feed ~a~%" |
107 | 106 |
(alimenta:feed-type c) |
... | ... |
@@ -118,15 +117,15 @@ |
118 | 117 |
(progn (unless (eq restart 'continue) |
119 | 118 |
(format t "~&Skipping a feed... ~s~%" |
120 | 119 |
(if wc-p |
121 |
- (alimenta.feed-archive.encoders::the-feed c) |
|
120 |
+ (alimenta.feed-archive.encoders:the-feed c) |
|
122 | 121 |
"Unknown"))) |
123 | 122 |
(funcall restart)))))))) |
124 | 123 |
|
125 |
- (handler-bind ((alimenta.feed-archive.encoders::feed-error |
|
126 |
- (lambda (c) (fix-pathname-or-skip c :wrapped-condition (alimenta.feed-archive.encoders::the-condition c)))) |
|
124 |
+ (handler-bind ((alimenta.feed-archive.encoders:feed-error |
|
125 |
+ (lambda (c) |
|
126 |
+ (fix-pathname-or-skip c :wrapped-condition (alimenta.feed-archive.encoders:the-condition c)))) |
|
127 | 127 |
(alimenta:feed-type-unsupported #'feed-type-unsupported) |
128 | 128 |
(error (lambda (c) (fix-pathname-or-skip c :restart 'continue)))) |
129 | 129 |
(multiple-value-bind (*feeds* *feed-base*) (funcall feed-list-initializer) |
130 |
- (alimenta.pull-feed::with-user-agent ("Feed Archiver v0.1b") |
|
130 |
+ (alimenta.pull-feed:with-user-agent ("Feed Archiver v0.1b") |
|
131 | 131 |
(archive-feeds)))))) |
132 |
- |