Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/clisp
- ; requires curl and jq packages
- ; need to write my own http web socket to replace curl
- ; need to write a json->s-expression parser to replace jq
- ; (run-shell-command) is deprecated? what function to use instead
- ;;; list of global variables
- (defvar *file-name* "file.json")
- (defvar *accept-json* "Accept: application/vnd.twitchtv.v2+json")
- (defvar *req1* "https://api.twitch.tv/kraken/games/top?limit=100&offset=")
- (defvar *req2* "https://api.twitch.tv/kraken/streams?limit=100&game=")
- (defvar *req3* "https://api.twitch.tv/kraken/search/streams?type=suggest&live=true&q=")
- ;;; display a list of every game being streamed now
- (defun get-games()
- (run-shell-command
- (format nil "curl -s -H \"~A\" -X GET \"~A2001\""
- *accept-json* *req1*) :output *file-name*)
- (let ((x 0) (d (with-open-file (stream *file-name*)
- (let ((data (make-string (file-length stream))))
- (read-sequence data stream)
- (parse-integer (remove #\,
- (subseq data 10 14)))))))
- (let ((a 0) (lim (ceiling (/ d 100))))
- (loop for i from 1 to lim do
- (run-shell-command
- (concatenate 'string "curl -s -H \"" *accept-json*
- "\" -X GET \"" *req1*
- (write-to-string a) "\"")
- :output *file-name*)
- (setq a (* 100 i))
- (if (= i lim)
- (setq x (- d a))
- (setq x 99))
- (loop for j from 0 to x do
- (run-shell-command
- (format nil "jq -r .top[~A].game.name" j)
- :input *file-name*))))
- (print "Finished looking for games."))
- (main))
- ;;; look up every channel streaming game X
- (defun find-channels()
- (print "What game do you even want?")
- (let ((game (substitute #\+ #\SPACE (read-line))))
- (if (equal game "return") (main)
- (progn
- (print "Searching for channels...")
- (run-shell-command
- (format nil "curl -s -H \"~A\" -X GET \"~A~A\""
- *accept-json* *req2* game)
- :output *file-name*)
- (format t "~%Here's all I could muster, if anything.~%")
- (run-shell-command
- (format nil "jq -C '.streams[].channel | .status + \" \" + .url'")
- :input *file-name*)
- (find-channels)))))
- ;;; find any channel that contains string X
- (defun search-query()
- (print "Enter a search query, please!")
- (let ((q (substitute #\+ #\SPACE (read-line))))
- (if (equal q "return") (main)
- (progn
- (format t "Searching by a query string...~%")
- (run-shell-command
- (format nil "curl -s -H \"~A\" -X GET \"~A~A\""
- *accept-json* *req3* q)
- :output *file-name*)
- (run-shell-command
- (format nil "jq -r '.streams[] | \"[ Game: \" + .game ~
- + \", Status: \" + .channel.status + \", \" + \"Vi~
- ewers: \\(.viewers)\" + \", Link: \" ~
- + .channel.url + \" ]\"'")
- :input *file-name*)
- (search-query)))))
- ;;; helpme eirin
- (defun info()
- (print "update: dumps a list of every game available being streamed on Twitch.")
- (print "game: searches for a channel streaming that game- type return to stop.")
- (print "query: looks for any channel that has that query in status, game, etc.")
- (print "close: terminates the program itself and cleans everything up...")
- (print "return: inside game and query functions, go back up a level.")
- (print "info: show this helpful glossary when in the main level.")
- (main))
- ;;; output to file
- (defun save() ())
- ;;; the main function
- (defun main()
- (print "So what do you wanna do next? (enter info for help)")
- (let ((answer (read-line)))
- (cond ((equal answer "update")
- (get-games))
- ((equal answer "close")
- 1)
- ((equal answer "info")
- (info))
- ((equal answer "game")
- (find-channels))
- ((equal answer "query")
- (search-query))
- (t (print "Invalid input. Try again, pal.") (main)))))
- (main) ; initiate the program to run as a script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement