searching for tweets in twtxt

searching for tweets in twtxt

A hacked up little script written in janet to look up IDs for twtxt tweets written to a sqlite database.

Use it in the following way: search_twtxt keyword [maxlen] where keyword is the keyword search term used in the full text search, and maxlen is an optional keyword used to limit how many characters to display in the results. The default is 60 characters.

<<search_twtxt>>=
#!/usr/local/bin/weewiki janet
(def twtxtdb "twtxt/tweets.db")

(defn search (keyword &opt limit)
  (default limit nil)
  (def db (sqlite3/open twtxtdb))
  (var msg (string
            "SELECT *"
            " FROM timeline WHERE message MATCH \""
            keyword
            "\""
            (if (not (nil? limit)) (string "LIMIT " limit) "")
            ";"))
  (var tweets
       (sqlite3/eval db msg))
  (sqlite3/close db)
  tweets)

(def keyword (string/ascii-lower ((dyn :args) 1)))
(def maxlen
  (if-not (nil? ((dyn :args) 2))
    (eval-string ((dyn :args) 2))))
(def tweets (search keyword))

(defn trunc [str key &opt maxlen]
  (default maxlen 60)
  #(if (nil? maxlen) (set maxlen 60))
  (if (> (length str) maxlen)
    (do
      (var start
           ((peg/match
             (peg/compile
              ~(any (+ (* ($) ,key) 1)))
             (string/ascii-lower str)) 0))

      (if (> (+ start maxlen) (length str))
        (set start (- (length str) maxlen)))
      (var end (+ start maxlen))
      (string
       (if (> start 0) "...")
       (string/slice str start end) "..."))

    str))

(each tweet tweets
  (print
   (tweet "id")
   ": "
   (trunc (tweet "message") keyword maxlen)))