Skip to content

Instantly share code, notes, and snippets.

@jkachmar
Last active August 29, 2015 14:25
Show Gist options
  • Save jkachmar/57357c1e25a487a6af5b to your computer and use it in GitHub Desktop.
Save jkachmar/57357c1e25a487a6af5b to your computer and use it in GitHub Desktop.
A little Racket script to grab titles for the top 'n' submissions in a subreddit.
#lang racket
(require net/url json)
;; Make a valid URL to retrieve a JSON payload for the top
;; entries in a given subreddit over some range of time
(define make-reddit-url
(λ (sub time limit)
(string->url
(string-append "http://www.reddit.com/r/"
sub "/top.json?"
"t=" time "&"
"limit=" limit))))
;; Generate a hash table from JSON information
(define (get-json link)
(let-values
([(status header response) (http-sendrecv/url link)])
(read-json response)))
;; Filter post titles out of the hash table of JSON information
(define (filter-json json-data)
(let ([filtered
(hash-ref (hash-ref json-data 'data) 'children)])
(map
(λ (elem) (hash-ref (hash-ref elem 'data) 'title))
filtered)))
;; Glue all the helper functions together and default to
;; pulling the top 5 entries from /r/listentothis for today
(define get-titles
(λ (#:sub [sub "listentothis"]
#:time [time "today"]
#:limit [limit "5"])
(filter-json
(get-json
(make-reddit-url sub time limit)))))
(get-titles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment