Advertisement
Guest User

VidyaScript

a guest
Sep 2nd, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.76 KB | None | 0 0
  1. #!/usr/bin/clisp
  2. ; requires curl and jq packages
  3. ; need to write my own http web socket to replace curl
  4. ; need to write a json->s-expression parser to replace jq
  5. ; (run-shell-command) is deprecated? what function to use instead
  6.  
  7. ;;; list of global variables
  8. (defvar *file-name* "file.json")
  9. (defvar *accept-json* "Accept: application/vnd.twitchtv.v2+json")
  10. (defvar *req1* "https://api.twitch.tv/kraken/games/top?limit=100&offset=")
  11. (defvar *req2* "https://api.twitch.tv/kraken/streams?limit=100&game=")
  12. (defvar *req3* "https://api.twitch.tv/kraken/search/streams?type=suggest&live=true&q=")
  13.  
  14. ;;; display a list of every game being streamed now
  15. (defun get-games()
  16.   (run-shell-command
  17.     (format nil "curl -s -H \"~A\" -X GET \"~A2001\""
  18.         *accept-json* *req1*) :output *file-name*)
  19.   (let ((x 0) (d (with-open-file (stream *file-name*)
  20.       (let ((data (make-string (file-length stream))))
  21.             (read-sequence data stream)
  22.             (parse-integer (remove #\,
  23.               (subseq data 10 14)))))))
  24.   (let ((a 0) (lim (ceiling (/ d 100))))
  25.   (loop for i from 1 to lim do
  26.   (run-shell-command
  27.     (concatenate 'string "curl -s -H \"" *accept-json*
  28.          "\" -X GET \"" *req1*
  29.          (write-to-string a) "\"")
  30.     :output *file-name*)
  31.     (setq a (* 100 i))
  32.     (if (= i lim)
  33.           (setq x (- d a))
  34.         (setq x 99))
  35.   (loop for j from 0 to x do
  36.   (run-shell-command
  37.     (format nil "jq -r .top[~A].game.name" j)
  38.     :input *file-name*))))
  39.   (print "Finished looking for games."))
  40.   (main))
  41.  
  42. ;;; look up every channel streaming game X
  43. (defun find-channels()
  44.   (print "What game do you even want?")
  45.   (let ((game (substitute #\+ #\SPACE (read-line))))
  46.   (if (equal game "return") (main)
  47.   (progn
  48.   (print "Searching for channels...")
  49.   (run-shell-command
  50.    (format nil "curl -s -H \"~A\" -X GET \"~A~A\""
  51.        *accept-json* *req2* game)
  52.    :output *file-name*)
  53.   (format t "~%Here's all I could muster, if anything.~%")
  54.   (run-shell-command
  55.    (format nil "jq -C '.streams[].channel | .status + \" \" + .url'")
  56.    :input *file-name*)
  57.   (find-channels)))))
  58.  
  59. ;;; find any channel that contains string X
  60. (defun search-query()
  61.   (print "Enter a search query, please!")
  62.   (let ((q (substitute #\+ #\SPACE (read-line))))
  63.   (if (equal q "return") (main)
  64.   (progn
  65.   (format t "Searching by a query string...~%")
  66.   (run-shell-command
  67.     (format nil "curl -s -H \"~A\" -X GET \"~A~A\""
  68.     *accept-json* *req3* q)
  69.     :output *file-name*)
  70.   (run-shell-command
  71.     (format nil "jq -r '.streams[] | \"[ Game: \" + .game ~
  72.        + \", Status: \" + .channel.status + \", \" + \"Vi~
  73.        ewers: \\(.viewers)\" + \", Link: \" ~
  74.        + .channel.url + \" ]\"'")
  75.     :input *file-name*)
  76.     (search-query)))))
  77.  
  78. ;;; helpme eirin
  79. (defun info()
  80.   (print "update: dumps a list of every game available being streamed on Twitch.")
  81.   (print "game: searches for a channel streaming that game- type return to stop.")
  82.   (print "query: looks for any channel that has that query in status, game, etc.")
  83.   (print "close: terminates the program itself and cleans everything up...")
  84.   (print "return: inside game and query functions, go back up a level.")
  85.   (print "info: show this helpful glossary when in the main level.")
  86.   (main))
  87.  
  88. ;;; output to file
  89. (defun save() ())
  90.  
  91. ;;; the main function
  92. (defun main()
  93.   (print "So what do you wanna do next? (enter info for help)")
  94.   (let ((answer (read-line)))
  95.   (cond ((equal answer "update")
  96.         (get-games))
  97.     ((equal answer "close")
  98.         1)
  99.     ((equal answer "info")
  100.         (info))
  101.     ((equal answer "game")
  102.         (find-channels))
  103.     ((equal answer "query")
  104.         (search-query))
  105.     (t (print "Invalid input. Try again, pal.") (main)))))
  106.  
  107. (main) ; initiate the program to run as a script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement