summaryrefslogtreecommitdiffstats
path: root/main.hy
blob: 608a6240e8b97a3c43fee6495f371a8d7797002c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env hy3
(import [glob [glob]]
        [os.path [join isfile]]
        [pathlib [Path]]
        [json [loads :as parse-json-string]]
        [re [sub :as replace-regexp match]]
        [requests [get :as http_get]])

(defn trimmed-js-file [filepath]
  (setv text (.read_text (Path filepath))
        lines (.splitlines text))
  (.join "\n" (cut lines 1)))

(defn tweet-text [tweet-dict]
  (setv s (get tweet-dict "text")
        matches (match "\\d{4}\\s?:\\s?(.+?)\\bhttp" s))
  (if matches (.group matches 1) s))

(defn tweet-date [tweet-dict]
  (setv ts (get tweet-dict "created_at"))
  (first (.split ts)))

(defn new-img-filename [tweet-dict n]
  (.format "{}_{}_{}.jpg"
           (tweet-date tweet-dict)
           (get tweet-dict "id")
           n))

(defn tweet-image-url [tweet-dict]
  (setv media (get (get tweet-dict "entities") "media"))
  (if media
      (list (map  (fn [m] (get m "media_url")) media))))

(defn standup-tweet? [tweet-dict]
  (setv matchtext (.upper (get tweet-dict "text")))
  (and (in "2017" matchtext) (in "STANDUP" matchtext)))

(defn make-image-file-unless-exists [filename imgurl]
  (if (isfile filename) 
      filename 
      (with [f (open filename "wb")]
        (setv img (http_get imgurl))
        (.write f (. img content)))))

(defn img-tag [filename title-text]
  (.format
    "<a href=\"/images/standup-selfies/{}\"><img src=\"/images/standup-selfies/thumb_{}\" width=\"50\" title=\"{}\" /></a>"
           filename filename title-text title-text))

(defmain [&rest args]
  (setv datadir (second args)
        globpath (join datadir "*js")
        tweetfiles (glob globpath))
  (setv pics [])
  (print "<html>")
  (for [batch tweetfiles]
    (for [t (filter standup-tweet?
                    (parse-json-string
                      (trimmed-js-file batch)))]
      (setv images (tweet-image-url t))
      (if images
          (do (setv n 0)
              (for [i images]
                (setv n (inc n)
                      filename (new-img-filename t n)
                      imgtag (img-tag filename (tweet-text t)))
                (make-image-file-unless-exists filename i)
                (.append pics imgtag))))))
  (for [x (reversed pics)] (print x))
  (print "</html>"))