Skip to content

Instantly share code, notes, and snippets.

@bennn
Created February 19, 2022 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bennn/f28d46ddeb39f6766add47e3c2f6f029 to your computer and use it in GitHub Desktop.
Save bennn/f28d46ddeb39f6766add47e3c2f6f029 to your computer and use it in GitHub Desktop.
YouTube Live Stream Chat: Collect and Order Names
#lang racket
;; To open a chat window:
;; 1. Open a YouTube livestream
;; 2. Copy the ID part of the url (v=<ID>)
;; 3. Paste ID to the end of the link below
;; https://www.youtube.com/live_chat?v=
;;
;; Tutorial video: https://youtu.be/DjsbmnyXDIo
(define url-template "https://www.youtube.com/live_chat?v=")
(define ignore* '("Ben Greenman"))
(module+ main
(require racket/cmdline)
(command-line
#:args arg*
(if (null? arg*)
(printf "To open a chat window:~n 1. Open a YouTube livestream~n 2. Copy the ID part of the url (v=<ID>)~n 3. Paste ID to the end of the link below~n~a~n" url-template)
(order-and-print-names (car arg*)))))
(define (order-and-print-names fn)
(define name# (with-input-from-file fn read-names))
(define name* (sort (hash->list name#) < #:key cdr))
(for ((ni (in-list name*)))
(printf "~a ~a~n"
(~r (cdr ni) #:min-width 3)
(car ni))))
(define (read-names)
(let loop ((acc (hash))
(n 1))
(define ln (read-line))
(cond
[(eof-object? ln)
acc]
[(or (string-suffix? ln "AM")
(string-suffix? ln "PM"))
(define name (read-line))
(if (and (string? name)
(not (hash-has-key? acc name))
(not (member name ignore*)))
(loop (hash-set acc name n) (+ n 1))
(loop acc n))]
[else
(loop acc n)])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment