1 ;; Twitter Filter A simple filter for custom twitter lists
2 ;; Copyright (C) 2012 Panagiotis Koutsourakis
4 ;; This program is free software: you can redistribute it and/or modify
5 ;; it under the terms of the GNU General Public License as published by
6 ;; the Free Software Foundation, either version 3 of the License, or
7 ;; (at your option) any later version.
9 ;; This program is distributed in the hope that it will be useful,
10 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;; GNU General Public License for more details.
14 ;; You should have received a copy of the GNU General Public License
15 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17 (ql:quickload
"drakma")
18 (ql:quickload
"cl-json")
20 (defpackage :twitter-filter
21 (:use
:cl
:drakma
:json
)
22 (:export
:parse-query
))
24 (in-package :twitter-filter
)
26 (defparameter *search-url
* "http://search.twitter.com/search.json?q=")
28 (defun convert-response-to-string (resp)
29 "The server response is an array of integers. This function converts
31 (coerce (mapcar #'code-char
(coerce resp
'list
)) 'string
))
33 (defun percent-encode (str)
34 "Make the string percent encoded in order to be compatible with
36 (labels ((percent-encode-char-list (char-list)
37 (let ((curr (car char-list
))
38 (rest (cdr char-list
)))
40 ((null rest
) (cons curr nil
))
42 (append '(#\%
#\
2 #\
3) (percent-encode-char-list rest
)))
44 (t (cons curr
(percent-encode-char-list rest
)))))))
45 (coerce (percent-encode-char-list (coerce str
'list
)) 'string
)))
47 (defun make-query (search-term)
48 "Make the actual search query to the twitter API."
49 (http-request (concatenate 'string
*search-url
* (percent-encode search-term
))))
52 (defun parse-query (search-term)
53 "Convert the response to Lisp objects using cl-json library."
54 (with-input-from-string
55 (s (convert-response-to-string (make-query search-term
)))
58 (defparameter *white-list-mode
* nil
)
59 (defparameter *black-list-mode
* nil
)
61 (defvar *white-list
* nil
)
62 (defvar *black-list
* nil
)
64 ;TODO: explore if these two can be written with a macro.
65 (defun black-list (lisp-tweets)
66 "Remove a tweet if the user is a member of *black-list*"
68 (remove-if (lambda (x)
69 (member (cdr (assoc :from--user x
))
70 *black-list
* :test
#'equal
))
74 (defun white-list (lisp-tweets)
75 "Remove a tweet if the user is NOT a member of *white-list*"
77 (remove-if (lambda (x)
78 (not (member (cdr (assoc :from--user x
))
79 *white-list
* :test
#'equal
)))
83 (defun get-tweet-element-list (lisp-tweets element
)
84 "Make a list with one element from each tweet from lisp-tweets."
85 (mapcar (lambda (x) (cdr (assoc element x
))) lisp-tweets
))
87 (defun apply-lists (lisp-tweets)
88 "Apply the functions white-list and black-list"
89 (black-list (white-list lisp-tweets
)))
91 ;Not strictly necessary... Just a reminder.
92 (defun convert-tweets-to-json (lisp-tweets)
93 (encode-json lisp-tweets
))