reddit whisky exposé

Check-in [d6298163ab]
Login

Check-in [d6298163ab]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:init
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA3-256: d6298163ab34fe8f96c0ed5437ad15def418baab5e0a53c9be2f825bf10cdb10
User & Date: Cthulhux 2019-09-13 22:37:14
Context
2019-09-13
22:37
init Leaf check-in: d6298163ab user: Cthulhux tags: trunk
22:35
initial empty check-in check-in: 0fc5907211 user: Cthulhux tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added README.md.























































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
# reddit whisky exposé

*made by [/u/rhabarba](https://reddit.com/u/rhabarba)*

## License:

[WTFPL](http://www.wtfpl.net/txt/copying/)

## Requirements:

- Racket
- the YAML module (`raco pkg install yaml`)
- a file named `whiskies.yaml` built like this:

    meta:
        - username: your-reddit-username
    
    whiskies:
        - name: Foo Bar
          age: 12
          price: $42
          alcvol: 42.6
          region: Speyside
          subreddit: scotch
          commentsid: 7gg3o9
          rating: 100
    
        - name: Quux
          age: NAS
          subreddit: worldwhisky
          commentsid: abcdef

The `meta` field is mandatory.

All whisky fields except `name`, `subreddit` and `commentsid` (which is the ID
part of the reddit thread URL) are optional, the table will remain empty in the
particular fields.

## Notes:

For efficiency reasons, the list is read once on startup. Restart the server if
you changed your YAML file. (This could be automatic if you use a VCS, e.g. a
post-commit hook.)

Added main.rkt.































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#lang web-server/insta

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; reddit whisky exposé
;; made by /u/rhabarba
;;-----------------------------------------
;; Requirements:
;;  - the YAML module (raco pkg install yaml)
;;  - a file named whiskies.yaml built like this:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; meta:
;;     - username: rhabarba
;;
;; whiskies:
;;     - name: Foo Bar
;;       age: 12
;;       price: $42
;;       alcvol: 42.6
;;       region: Speyside
;;       subreddit: scotch
;;       commentsid: 7gg3o9
;;       rating: 100
;;
;;     - name: Quux
;;       age: NAS
;;       subreddit: worldwhisky
;;       commentsid: abcdef
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; The "meta" field is mandatory.
;;
;; All whisky fields except "name", "subreddit" and "com-
;; mentsid" (which is the ID part of the reddit thread URL)
;; are optional, the table will remain empty in the parti-
;; cular fields.
;;
;; For efficiency reasons, the list is read once on startup.
;; Restart the server if you changed your YAML file. (This
;; could be automatic if you use a VCS, e.g. a post-commit
;; hook.)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require yaml)

(define whiskyfile "whiskies.yaml") ;; you can change this

;; Check for file existence:
(unless (file-exists? whiskyfile)
  (displayln "YAML file not accessible")
  (exit 1))

;; Read the whiskyfile:
(define whisky-list
  (with-handlers ((void (lambda _
                          (displayln "invalid YAML syntax")
                          #f)))
    (file->yaml* whiskyfile)))

;; Check the validity:
(unless (and
         (hash-has-key? (first whisky-list) (string->yaml "meta"))
         (hash-has-key? (first whisky-list) (string->yaml "whiskies")))
  (displayln "missing YAML key - required meta and whiskies.")
  (exit 1))

;; As the lists can come in unstructured, we'll make a struct.
(define-struct whisky (name age alcvol price region subreddit commentsid rating))
(define all-whiskies (list))

(define this-name "")
(define this-age "")
(define this-alcvol "")
(define this-price "")
(define this-region "")
(define this-subreddit "")
(define this-commentsid "")
(define this-rating "")

;; Generate a new table row for a whisky:
(define (render-whisky whisky)
  `(tr
    (td (a ((href
             ,(string-append
               "https://reddit.com/r/"
               (whisky-subreddit whisky)
               "/comments/"
               (whisky-commentsid whisky))))
           ,(whisky-name whisky)))
    (td ,(if (number? (whisky-age whisky))
             (number->string (whisky-age whisky))
             (whisky-age whisky)))
    (td ,(if (number? (whisky-alcvol whisky))
             (number->string (whisky-alcvol whisky))
             (whisky-alcvol whisky)))
    (td ,(whisky-region whisky))
    (td ,(if (number? (whisky-price whisky))
             (number->string (whisky-price whisky))
             (whisky-price whisky)))
    (td ,(if (number? (whisky-rating whisky))
             (number->string (whisky-rating whisky))
             (whisky-rating whisky)))))

;; Generate both lists:
(define meta-data null)
(define whisky-data null)

(for-each
 (lambda (arg)
   (if (string=? (first arg) "meta")
       ;; We either have "meta" or "whiskies".
       ;; if "meta":
       (set! meta-data (hash->list (second arg)))
       ;; else:
       (for ((h (in-list      ;; traverse over "whiskies"
                 (cdr arg)))) ;; filter out the key, keep the values

         ;; Each "h" is a whisky here. We shall put it in a struct.
         (for ((a-whisky (in-list (hash->list h))))
           (when (regexp-match? #rx"name" (yaml->string (car a-whisky)))
             (set! this-name (cdr a-whisky)))
           (when (regexp-match? #rx"age" (yaml->string (car a-whisky)))
             (set! this-age (cdr a-whisky)))
           (when (regexp-match? #rx"alcvol" (yaml->string (car a-whisky)))
             (set! this-alcvol (cdr a-whisky)))
           (when (regexp-match? #rx"price" (yaml->string (car a-whisky)))
             (set! this-price (cdr a-whisky)))
           (when (regexp-match? #rx"region" (yaml->string (car a-whisky)))
             (set! this-region (cdr a-whisky)))
           (when (regexp-match? #rx"subreddit" (yaml->string (car a-whisky)))
             (set! this-subreddit (cdr a-whisky)))
           (when (regexp-match? #rx"commentsid" (yaml->string (car a-whisky)))
             (set! this-commentsid (cdr a-whisky)))
           (when (regexp-match? #rx"rating" (yaml->string (car a-whisky)))
             (set! this-rating (cdr a-whisky))))
   
         ;; Make a complete list of structs.
         (set! all-whiskies
               (append all-whiskies
                       (list
                        (make-whisky
                         this-name
                         this-age
                         this-alcvol
                         this-price
                         this-region
                         this-subreddit
                         this-commentsid
                         this-rating))))

         ;; Reset:
         (set! this-name "")
         (set! this-age "")
         (set! this-alcvol "")
         (set! this-price "")
         (set! this-region "")
         (set! this-subreddit "")
         (set! this-commentsid "")
         (set! this-rating ""))))

 (hash->list (first whisky-list)))

;; We have filled meta-data and all-whiskies now.
;; The user name is a fixed string in our very small meta-data
;; list right now. It only contains a username... if the user
;; wasn't too dumb. :p
(define list-user-name (cdar meta-data))
(define the-table "")

;; Sort the whisky list alphabetically.
(set! all-whiskies (sort all-whiskies string<? #:key whisky-name))

;; Create the-table.
(set! the-table
      `(table
        (tr
         (th "Name")
         (th "Age")
         (th "Vol.%")
         (th "Region")
         (th "Price")
         (th "Rating"))
        ,@(map render-whisky all-whiskies)))

;; Time to process the whiskies while answering HTTP requests!
(define (start request)
  (response/xexpr
   #:preamble #"<!DOCTYPE html>"
   `(html
     (head
      (title ,(string-append list-user-name "'s whisky reviews"))
      (style ((type "text/css"))
             ,(string-append
               "body { font-family: sans-serif }"
               "table { border-collapse:collapse }"
               "tr, th, td { border: 1px solid grey }"
               "th, td { padding: 3px 6px }")))
     (body
      (h1
       (a ((href ,(string-append "https://reddit.com/u/" list-user-name))
           (target "_blank"))
          ,list-user-name) "'s whisky reviews")
      ,the-table))))

Added whiskies.yaml.



































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
meta:
    - username: username
    
whiskies:
    - name: Foo Bar
      age: 12
      alcvol: 42.6
      price: $42
      region: Speyside
      subreddit: scotch
      commentsid: qwerty
      rating: 100
      
    - name: Quux
      age: NAS
      subreddit: worldwhisky
      commentsid: abcdef