livie-playlist.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;;; livie-playlist.el --- Auxiliary mode to display playlist results
  2. ;; Copyright (C) 2018 - 2021
  3. ;;; Authors:
  4. ;; Charlie Ritter <chewzerita@posteo.net>
  5. ;; Jesus E. <heckyel@hyperbola.info>
  6. ;; Gabriele Rastello <gabriele.rastello@edu.unito.it>
  7. ;; Pablo BC <pablo.barraza@protonmail.com>
  8. ;;; Commentary:
  9. ;; livie grabs a list of youtube videos based on a search.
  10. ;; the user can then select a video to watch through `livie-player'
  11. ;;; Code:
  12. (defvar livie-playlist-mode-map
  13. (let ((map (make-sparse-keymap)))
  14. (set-keymap-parent map text-mode-map)
  15. (define-key map "h" #'describe-mode)
  16. (define-key map "q" #'livie--quit-playlist-buffer)
  17. (define-key map ">" #'livie-playlist-next-page)
  18. (define-key map "<" #'livie-playlist-previous-page)
  19. (define-key map (kbd "<tab>") #'next-line)
  20. (define-key map (kbd "<backtab>") #'previous-line)
  21. (define-key map "A" #'livie--open-channel)
  22. (define-key map (kbd "RET") #'livie-open-entry)
  23. (define-key map "y" #'livie-watch-this-video)
  24. map)
  25. "Keymap for `livie-playlist-mode'.")
  26. (define-derived-mode livie-playlist-mode livie-mode
  27. "livie-playlist-mode"
  28. "Mode for displaying livie-playlists.
  29. \\{livie-playlist-mode-map}"
  30. (buffer-disable-undo)
  31. (make-local-variable 'livie-videos)
  32. (make-local-variable 'livie-playlist-title)
  33. (make-local-variable 'livie-playlistId)
  34. (setq-local livie-type-of-results "video")
  35. (setf buffer-read-only t))
  36. (defun livie--process-playlist-videos (videos)
  37. "Process VIDEOS fetched from a playlist."
  38. (dotimes (i (length videos))
  39. (let* ((v (aref videos i)))
  40. (aset videos i (livie-video--create
  41. :title (assoc-default 'title v)
  42. :author (assoc-default 'author v)
  43. :authorId (assoc-default 'authorId v)
  44. :length (assoc-default 'lengthSeconds v)
  45. :id (assoc-default 'videoId v)))))
  46. videos)
  47. (defun livie-playlist--insert-entry (video)
  48. "Insert VIDEO into the playlist buffer."
  49. (insert (livie--format-author (livie-video-author video))
  50. " "
  51. (livie--format-video-length (livie-video-length video))
  52. " "
  53. (livie--format-title (livie-video-title video))))
  54. (defun livie--get-playlist-videos ()
  55. "Fetch the videos of the current playlist."
  56. (let* ((entry (livie-get-current-video)))
  57. (if (livie-playlist-p entry)
  58. (progn
  59. (switch-to-buffer (livie-playlist-title entry))
  60. (unless (eq major-mode 'livie-playlist-mode)
  61. (livie-playlist-mode))
  62. (setf livie-playlistId (livie-playlist-playlistId entry))
  63. (setf livie-playlist-title (livie-playlist-title entry))
  64. (livie-playlist--query livie-playlistId livie-current-page)
  65. (livie-playlist--draw-buffer)))))
  66. (defun livie-playlist-previous-page ()
  67. "Go to the previous page of playlist."
  68. (interactive)
  69. (setf livie-current-page (1- livie-current-page))
  70. (livie-playlist--query livie-playlistId livie-current-page)
  71. (livie-playlist--draw-buffer))
  72. (defun livie-playlist-next-page ()
  73. "Go to the next page of playlist."
  74. (interactive)
  75. (setf livie-current-page (1+ livie-current-page))
  76. (livie-playlist--query livie-playlistId livie-current-page)
  77. (livie-playlist--draw-buffer))
  78. (defun livie-playlist--draw-buffer ()
  79. "Draw buffer for the current playlist."
  80. (let ((inhibit-read-only t))
  81. (erase-buffer)
  82. (setq header-line-format (concat "Displaying videos from " (propertize livie-playlist-title 'face 'livie-parameter-face)
  83. ", page "
  84. (propertize (number-to-string livie-current-page) 'face 'livie-parameter-face)))
  85. (seq-do (lambda (v)
  86. (livie-playlist--insert-entry v)
  87. (insert "\n"))
  88. livie-videos)
  89. (goto-char (point-min))))
  90. (defun livie-playlist--query (playlistID page)
  91. "Query Invidious for videos from PLAYLISTID on PAGE."
  92. (let* ((results (livie--API-call (concat "playlists/" livie-playlistId) '(("fields" "videos")
  93. ("page" ,livie-current-page)))))
  94. (setf livie-videos (livie--process-playlist-videos (assoc-default 'videos results)))))
  95. (defun livie--quit-playlist-buffer ()
  96. "Deletes the current buffer."
  97. (interactive)
  98. (kill-buffer (current-buffer)))
  99. (provide 'livie-playlist)
  100. ;;; livie-playlist.el ends here