Created
December 20, 2011 08:47
-
-
Save youz/1500834 to your computer and use it in GitHub Desktop.
xyttrのAPI関数を使ってツイートをGrowlで表示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; xyzzy起動時に通知を開始するなら .xyzzy に | |
;;; xyttr起動時から通知を開始するなら ~/.xyttr/config.l に書く | |
(require "tweets-notifier") | |
;; Mentionを通知 | |
(xyttr:define-notifier mentions | |
:apifunc #'xyttr:api-mentions-async ; timeline系API関数 (必ず非同期版を指定する事) | |
:sticky t ; 通知ウィンドウをクリックorキー操作で閉じるまで表示し続ける | |
:interval 300 ; 新着取得間隔 (秒) | |
:callback (lambda (tweet) (user::xyttr-mentions))) ; クリック時に実行するコールバック関数 | |
;; "xyzzy -RT" の検索結果を通知 | |
(xyttr:define-notifier search-xyzzy | |
:apifunc #'xyttr:api-search-async | |
:params (:lang "ja" :q "xyzzy -RT" :include_entities "true") ; apifuncに渡すパラメータ | |
:sticky t | |
:interval 300 | |
:callback (lambda (tweet) (user::xyttr-search "xyzzy -RT"))) | |
;; 通知開始 | |
(xyttr:start-notifier mentions) | |
(xyttr:start-notifier search-xyzzy) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; -*- mode:lisp; package:xyttr -*- | |
(eval-when (:compile-toplevel :load-toplevel :execute) | |
(require "cmu_loop") | |
(require "xyttr") | |
(require "growl")) | |
(in-package :xyttr) | |
(export '(define-notifier | |
start-notifier | |
stop-notifier | |
start-all-notifiers | |
stop-all-notifiers)) | |
(defvar *notifiers* nil) | |
(eval-when (:compile-toplevel :load-toplevel :execute) | |
(defstruct notifier | |
"tweets notifier" | |
name fn apifunc params sticky interval callback)) | |
(defmacro define-notifier (name &key apifunc params interval callback sticky) | |
`(progn | |
(setf (getf *notifiers* ',name) | |
(make-notifier | |
:name ',name | |
:apifunc ,apifunc | |
:params (list ,@params) | |
:interval ,(or interval 60) | |
:sticky ,sticky | |
:callback ,callback)) | |
',name)) | |
(defun notify1 (tweet sticky callback) | |
(w/json (user.screen_name user.profile_image_url text) tweet | |
(growl:notify-with-socket-callback | |
(concat "@" user.screen_name) text "xyttr notifier" "xyttr" | |
:name "xyttr" :sticky sticky | |
:icon user.profile_image_url | |
:onclick | |
(and callback | |
(lambda (&rest args) (funcall callback tweet)))))) | |
(defun %start-notifier (tn) | |
(when (notifier-fn tn) | |
(return-from %start-notifier)) | |
(flet ((f () | |
(apply (notifier-apifunc tn) | |
:since_id #1=(getf (notifier-params tn) :since_id) | |
:onsuccess | |
(lambda (res) | |
(when res | |
(unless #1# (setq res (subseq res 0 5))) | |
(dolist (tw res) | |
(notify1 tw (notifier-sticky tn) (notifier-callback tn))) | |
(setf #1# (json-value (car res) id)))) | |
:onfailure | |
(lambda (res status header) | |
(message "HTTP ~D: request failed" status)) | |
(notifier-params tn)))) | |
(f) | |
(prog1 | |
(start-timer (notifier-interval tn) #'f) | |
(setf (notifier-fn tn) #'f)))) | |
(defun %stop-notifier (tn) | |
(whenlet f #0=(notifier-fn tn) | |
(prog1 | |
(stop-timer f) | |
(setf #0# nil)))) | |
(defmacro start-notifier (name) | |
(xyttr-init) | |
`(whenlet #0=#:tn (getf *notifiers* ',name) | |
(%start-notifier #0#))) | |
(defmacro stop-notifier (name) | |
`(whenlet #0=#:tn (getf *notifiers* ',name) | |
(%stop-notifier #0#))) | |
(defun start-all-notifiers () | |
(loop for tn in (cdr *notifiers*) by #'cddr | |
do (%start-notifier tn))) | |
(defun stop-all-notifiers () | |
(loop for tn in (cdr *notifiers*) by #'cddr | |
do (%stop-notifier tn))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment