summaryrefslogtreecommitdiffstats
path: root/hn/g
blob: ae804109ac34f51bd93f8ca92c173009d77720b9 (plain) (blame)
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
#!/usr/bin/env racket
;;
;; SPDX-License-Identifier: ISC
;;
;; Copyright © 2019 rsiddharth <s@ricketyspace.net>
;;

#lang racket/base

(require racket/list)
(require racket/port)

(require net/http-client)
(require openssl)

(require html-parsing)
(require sxml)
(require sxml/sxpath)


(define HOST "news.ycombinator.com")

(define (fp)
  "Fetch HN Front Page."
  (let ((hc (http-conn-open HOST
                            #:ssl? (ssl-make-client-context 'secure)
                            #:port 443)))
    (define-values (status headers port)
      (http-conn-sendrecv! hc "/"))
    (port->string port)))

(define (athings)
  "Get 20 athings from HN front page."
  (let ((x (html->xexp (fp)))
        (s (sxpath "//tr[@class=\"athing\"]")))
    (take (s x) 20)))

(define (athing:id a)
  (let ((id (sxml:attr a 'id)))
    (if (empty? id) (error "athing:id: Unable to get id")
        id)))