(in-package :twapper)

(defvar *twapper-api-key* "") ;your twapper api key here
(defconstant +twapper-host+ "api.twapperkeeper.com")
(defconstant +twapper-protocol+ "http")
(defconstant +twapper-port+ 80)

(defun create-twapper-archive (type name created-by description &key tags n1 n2 n3 n4 n5)
  ;;http://api.twapperkeeper.com/notebook/create/?apikey=xxxxx&name=abcdefg&type=hashtag&description=This is a test.&created_by=jobrieniii&user_id=1234
  ;;POST arguments
  ;;apikey [required], type (hashtag, keyword, person, collection, person-collection) [required], name [required], tags[optional], created_by [required], description [required],
  ;; n1,n2,n3,n4,n5 [notebook 1,2,3,4,5 - only for type collection or person-collection - collections must have at least one archive - and do not mix together person and hashtag / keyword archives or errors will occur!]
  (with-open-stream (hs (comm:open-tcp-stream +twapper-host+ +twapper-port+))
    (format-to-http hs
                    "POST http://api.twapperkeeper.com/notebook/create/?apikey=~A&type=~A&name=~A&created_by=~A&description=~A~@[&tags=~A~]~@[&n1=~A~]~@[&n2=~A~]~@[&n3=~A~]~@[&n4=~A~]~@[&n5=~A~] HTTP/1.1~A"
                    *twapper-api-key*
                    (url-encode type) (url-encode name) (url-encode created-by) (url-encode description) (and tags (url-encode tags))
                    (and n1 (url-encode n1)) (and n2 (url-encode n2)) (and n3 (url-encode n3)) (and n4 (url-encode n4)) (and n5 (url-encode n5))
                    +crlf+)
    (format-to-http hs
                    "Host: ~A~A" +twapper-host+ +crlf+)
    (format-to-http hs "User-Agent: ~A~A" *gdft-user-agent* +crlf+)
    (format-to-http hs "Content-length: 0~A" +crlf+)
    (format-to-http hs +crlf+)
    (force-output hs) 
    (skip-http-headers hs)
    (json:parse-json (read-line-from-http hs))))

(defun get-twapper-archive-info (&key type name namelike desclike)
  ;;http://api.twapperkeeper.com/notebook/info/?apikey=xxxx&type=xxxx&name=xxxx&namelike=xxxx&desclike=xxxx
  ;;GET parmaters
  ;;apikey [required], type (hashtag, keyword, person, collection, person-collection) [optional], name [optional], namelike [optional], desclike [optional]
  (with-open-stream (hs (comm:open-tcp-stream +twapper-host+ +twapper-port+))
    (format-to-http hs
                    "GET http://api.twapperkeeper.com/notebook/info/?apikey=~A~@[&name=~A~]~@[&type=~A~]~@[&namelike=~A~]~@[&desclike=~A~] HTTP/1.1~A"
                    *twapper-api-key*
                    (and name (url-encode name))
                    (and type (url-encode type))
                    (and namelike (url-encode namelike))
                    (and desclike (url-encode desclike))
                    +crlf+)
    (format-to-http hs
                    "Host: ~A~A" +twapper-host+ +crlf+)
    (format-to-http hs "User-Agent: ~A~A" *gdft-user-agent* +crlf+)
    (format-to-http hs "Content-length: 0~A" +crlf+)
    (format-to-http hs +crlf+)
    (force-output hs)
    (skip-http-headers hs)
    (json:parse-json (read-line-from-http hs))))

(defun get-twapper-archive-tweets (type name &key lang max-id since-id since until order-by nort text from-user latitude longitude radius rpp page)
  ;;http://api.twapperkeeper.com/2/notebook/tweets/?apikey=xxxx&name=xxxxx&type=xxxx[&optional parameters] 
  ;;GET parmaters
  ;;apikey [required]
  ;;type (hashtag, keyword, person, collection, person-collection) [required]
  ;;name [required]
  ;;lang [optional - ISO-639-1 2 letter code included in tweet metadata]
  ;;max_id [optional - maximum twitter id]
  ;;since_id [optional - minimum twitter id]
  ;;since [optional - start date - format = YY-MM-DD]
  ;;until [optional - end date - format = YY-MM-DD]
  ;;order_by [optional - a = ascending, d = descending (default)]
  ;;nort [optional set to 1 to remove all tweets starting with RT, default = 0]
  ;;text [optional - tweet text to search for]
  ;;from_user [optional - twitter username of sender]
  ;;latitude | longitude | radius [optional - must include each parameters individually - radius in km]
  ;;rpp = results per page [optional - but default set to 10, max allowed = 1000]
  ;;page [optional - default = 1]
  (with-open-stream (hs (comm:open-tcp-stream +twapper-host+ +twapper-port+))
    (format-to-http hs
                    "GET http://api.twapperkeeper.com/2/notebook/tweets/?apikey=~A&type=~A&name=~A~@[&lang=~A~]~@[&max_id=~A~]~@[&since_id=~A~]~@[&since=~A~]~@[&until=~A~]~@[&order_by=~A~]~@[&nort=~A~]~@[&text=~A~]~@[&from_user=~A~]~@[&latitude=~A~]~@[&longitude=~A~]~@[&radius=~A~]~@[&rpp=~A~]~@[&page=~D~] HTTP/1.1~A"
                    *twapper-api-key*
                    (url-encode type) (url-encode name)
                    (and lang (url-encode lang))
                    max-id
                    since-id
                    (and since (url-encode since))
                    (and until (url-encode until))
                    (and order-by (url-encode order-by))
                    (and nort (url-encode nort))
                    (and text (url-encode text))
                    (and from-user (url-encode from-user))
                    (and latitude (url-encode latitude))
                    (and longitude (url-encode longitude))
                    (and radius (url-encode radius))
                    rpp
                    page
                    +crlf+)
    (format-to-http hs
                    "Host: ~A~A" +twapper-host+ +crlf+) 
    (format-to-http hs "User-Agent: ~A~A" *gdft-user-agent* +crlf+)
    (format-to-http hs "Content-length: 0~A" +crlf+)
    (format-to-http hs +crlf+)
    (force-output hs)
    (skip-http-headers hs)
    (loop for line = (read-line-from-http hs)
          for json-obj = (json:parse-json line)
          when (json:json-object-p json-obj)
          return json-obj
          finally (return line))))
