Changeset 3701

Show
Ignore:
Timestamp:
07/30/08 23:06:43 (4 months ago)
Author:
hans
Message:

Work on JSON handler for news.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/projects/quickhoney/src/handlers.lisp

    r3529 r3701  
    314314                               "function done() { window.opener.do_query(); window.close(); }")) 
    315315                             (:body 
    316                               (:p "Image " (:princ-safe (store-image-name image)) " with " (:princ-safe (hash-table-count color-table)) " colors uploaded") 
     316                              (:p "Image " (:princ-safe (store-image-name image)) " with " 
     317                                  (:princ-safe (hash-table-count color-table)) " colors uploaded") 
    317318                              (:p ((:img :src (format nil "/image/~D" (store-object-id image)) 
    318319                                         :width (round (* ratio width)) :height (round (* ratio height))))) 
     
    425426                      (:p (:princ-safe (apply #'format nil (simple-condition-format-control e) (simple-condition-format-arguments e)))) 
    426427                      (:p ((:a :href "javascript:window.close()") "ok")))))))))))) 
     428 
     429(defclass news-json-handler (object-handler) 
     430  () 
     431  (:default-initargs :object-class 'rss-channel :query-function #'find-rss-channel)) 
     432 
     433(defvar *json-output*) 
     434 
     435(defmacro with-json-output ((stream) &body body) 
     436  `(let ((*json-output* ,stream)) 
     437     ,@body)) 
     438 
     439(defmacro with-json-output-to-string (() &body body) 
     440  `(with-output-to-string (*json-output*) 
     441     ,@body)) 
     442 
     443(defmacro with-json-array (() &body body) 
     444  (with-gensyms (need-comma) 
     445    `(let (,need-comma) 
     446       (princ #\[ *json-output*) 
     447       (prog1 
     448           (labels ((encode-array-element (value) 
     449                      (if ,need-comma 
     450                          (princ #\, *json-output*) 
     451                          (setf ,need-comma t)) 
     452                      (json:encode-json value *json-output*))) 
     453             ,@body) 
     454         (princ #\] *json-output*))))) 
     455 
     456(defmacro with-json-object (() &body body) 
     457  (with-gensyms (need-comma) 
     458    `(let (,need-comma) 
     459       (princ #\{ *json-output*) 
     460       (prog1 
     461           (labels ((encode-object-member (key value) 
     462                      (when value 
     463                        (if ,need-comma 
     464                            (princ #\, *json-output*) 
     465                            (setf ,need-comma t)) 
     466                        (json:encode-json key *json-output*) 
     467                        (princ #\, *json-output*) 
     468                        (json:encode-json value *json-output*)))) 
     469             ,@body) 
     470         (princ #\} *json-output*))))) 
     471 
     472(defmethod handle-object ((handler news-json-handler) (channel rss-channel)) 
     473  (with-http-response (:content-type "application/json") 
     474    (with-json-output-to-string () 
     475      (with-json-array () 
     476        (dolist (item (rss-channel-items channel)) 
     477          (with-json-object () 
     478            (encode-object-member "pubDate" (format-date-time (rss-item-pub-date item) :vms-style t)) 
     479            (encode-object-member "title" (rss-item-title item)) 
     480            (encode-object-member "description" (rss-item-description item)))))))) 
  • trunk/projects/quickhoney/src/image.lisp

    r3683 r3701  
    88   (spider-keywords :update :initform nil) 
    99   (products :update :initform nil))) 
    10  
    11 (defmethod rss-item-pub-date ((item quickhoney-image)) 
    12   (blob-timestamp item)) 
    13  
    14 (defmethod quickhoney-image-explicit ((image quickhoney-image)) 
    15   (member :explicit (store-image-keywords image))) 
    16  
    17 (defmethod rss-item-encoded-content ((image quickhoney-image)) 
    18   (let* ((category (first (intersection (store-image-keywords image) '(:vector :pixel)))) 
    19          (is-vector (eq category :vector))) 
    20     (with-output-to-string (s) 
    21       (html-stream 
    22        s 
    23        ((:div :class (format nil "newsentry news_~(~A~)" category)) 
    24         ((:img :src (format nil "http://~A/image/~A/cutout-button,,~A,98,4" 
    25                             (website-host) 
    26                             (store-object-id image) 
    27                             (if is-vector "00ccff" "ff00ff"))) 
    28         (:div 
    29          (:h1 (:princ (store-image-name image))) 
    30          (:princ (format nil "~A by ~A | " 
    31                          (format-date-time (blob-timestamp image)) 
    32                          (if is-vector "Peter" "Nana"))) 
    33          ((:a :href (make-image-link image)) "permalink"))))) 
    34       (when (quickhoney-image-client image) 
    35         (html-stream s :br "Client: " (:princ (quickhoney-image-client image))))))) 
    3610 
    3711(defvar *last-image-upload-timestamp* 0) 
     
    4822            (store-object-remove-keywords image 'bknr.web::keywords '(:import))) 
    4923        (get-keywords-intersection-store-images '(:import)))) 
    50  
    51 (defmethod rss-item-channel ((item quickhoney-image)) 
    52   "quickhoney") 
    53  
    54 (defmethod rss-item-title ((image quickhoney-image)) 
    55   (store-image-name image)) 
    56  
    57 (defmethod rss-item-description ((image quickhoney-image)) 
    58   (format nil "~A~@[ (Client: ~A)~]" (store-image-name image) (quickhoney-image-client image))) 
    59  
    60 (defmethod rss-item-link ((image quickhoney-image)) 
    61   (make-image-link image)) 
    62  
    63 (defmethod rss-item-guid ((image quickhoney-image)) 
    64   (make-image-link image)) 
    6524 
    6625(defmethod quickhoney-image-category ((image quickhoney-image)) 
     
    8241  (delete-object (quickhoney-animation-image-animation image))) 
    8342 
    84 (define-persistent-class quickhoney-news-item (quickhoney-image) 
    85   ((title :update) 
    86    (text :update))) 
    87  
    88 (defmethod quickhoney-image-spider-keywords ((item quickhoney-news-item)) 
    89   (quickhoney-news-item-title item)) 
    90  
    91 (defmethod rss-item-title ((item quickhoney-news-item)) 
    92   (quickhoney-news-item-title item)) 
    93  
    94 (defmethod rss-item-encoded-content ((item quickhoney-news-item)) 
    95   (concatenate 'string 
    96                (call-next-method) 
    97                (quickhoney-news-item-text item))) 
    98  
    99 (defclass quickhoney-rss-channel (rss-channel) 
    100   () 
    101   (:metaclass persistent-class)) 
    102  
    103 (defmethod rss-channel-items ((channel quickhoney-rss-channel) &key) 
    104   (remove-if (lambda (item) 
    105                (and (typep item 'quickhoney-image) 
    106                     (quickhoney-image-explicit item))) 
    107              (call-next-method))) 
  • trunk/projects/quickhoney/src/quickhoney.asd

    r3648 r3701  
    3030               (:file "config" :depends-on ("packages")) 
    3131               (:file "image" :depends-on ("config")) 
     32               (:file "news" :depends-on ("image")) 
    3233               (:file "layout" :depends-on ("config")) 
    3334               (:file "imageproc" :depends-on ("config")) 
  • trunk/projects/quickhoney/src/webserver.lisp

    r2832 r3701  
    3434                                        ("/upload-news" upload-news-handler) 
    3535                                        ("/digg-image" digg-image-handler) 
     36                                        ("/news-json" news-json-handler) 
    3637                                        ("/" template-handler 
    3738                                         :default-template "frontpage"