blob: cd201b8e7af7614e9ae4237d672856dca97c155e (
plain) (
tree)
|
|
#!/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 top 20 athings from HN front page."
(let ((x (html->xexp (fp)))
(s (sxpath "//tr[@class=\"athing\"]")))
(take (s x) 20)))
(define (athing:id a)
"Return athing id"
(let ((id (sxml:attr a 'id)))
(if (empty? id) (error "athing:id: Unable to get id") id)))
(define (athing:link a)
"Return athing link (HREF DESC)"
(let* ((s (sxpath "//a[@class=\"storylink\"]"))
(l (s a)))
(list (sxml:text l) (sxml:attr (car l) 'href))))
(define (athings:latest h athings:sxml)
(cond ((empty? athings:sxml) h)
(else
(let* ((athing (car athings:sxml))
(id (athing:id athing))
(link (athing:link athing)))
(athings:latest (hash-set h (string->symbol id) link)
(cdr athings:sxml))))))
|