livie-channel.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ;;; livie-channel.el --- Auxiliary major mode for livie -*- lexical-binding: t; -*-
  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. (defcustom livie-channel-sort-criterion "newest"
  13. "Sort videos by 'newest', 'oldest', or 'popular', as used by `livie-channel-search'."
  14. :type 'string
  15. :options '("newest" "oldest" "popular")
  16. :group 'livie-channel)
  17. (defvar livie-channel-mode-map
  18. (let ((map (make-sparse-keymap)))
  19. (set-keymap-parent map text-mode-map)
  20. (define-key map "h" #'describe-mode)
  21. (define-key map "q" #'livie--quit-channel-buffer)
  22. (define-key map ">" #'livie-channel-next-page)
  23. (define-key map "<" #'livie-channel-previous-page)
  24. (define-key map (kbd "<tab>") #'next-line)
  25. (define-key map (kbd "<backtab>") #'previous-line)
  26. (define-key map "S" #'livie-channel-sort-videos)
  27. (define-key map (kbd "RET") #'livie-open-entry)
  28. (define-key map "y" #'livie-watch-this-video)
  29. map)
  30. "Keymap for `livie-channel-mode'.")
  31. (define-derived-mode livie-channel-mode livie-mode
  32. "livie-channel-mode"
  33. "Mode for displaying livie-channel-videos.
  34. \\{livie-channel-mode-map}"
  35. (buffer-disable-undo)
  36. (make-local-variable 'livie-videos)
  37. (make-local-variable 'livie-channel-author)
  38. (setq-local livie-type-of-results "video")
  39. (setf buffer-read-only t))
  40. (defun livie--channel-query (uid n sort)
  41. "Query youtube for UID videos, return the Nth page of results, sorted bv SORT."
  42. (let ((videos (livie--API-call (concat "channels/videos/" uid)
  43. `(("page" ,n)
  44. ("sort_by" ,sort)
  45. ("fields" ,livie-default-video-query-fields)))))
  46. (dotimes (i (length videos))
  47. (let ((v (aref videos i)))
  48. (aset videos i
  49. (livie-video--create :title (assoc-default 'title v)
  50. :author (assoc-default 'author v)
  51. :authorId (assoc-default 'authorId v)
  52. :length (assoc-default 'lengthSeconds v)
  53. :id (assoc-default 'videoId v)
  54. :views (assoc-default 'viewCount v)
  55. :published (assoc-default 'published v)))))
  56. videos))
  57. (defun livie-channel ()
  58. "Open a buffer for the channel of the current entry."
  59. (let* ((entry (livie-get-current-video))
  60. (author (funcall (livie--get-author-function entry) entry))
  61. (authorId (funcall (livie--get-authorId-function entry) entry)))
  62. (get-buffer-create author)
  63. (switch-to-buffer author)
  64. (unless (eq major-mode 'livie-channel-mode)
  65. (livie-channel-mode))
  66. (setf livie-channel-author author)
  67. (setf livie-search-term authorId)
  68. (livie-channel-get-videos authorId)))
  69. (defun livie-channel-get-videos (authorId)
  70. "Fetch videos from AUTHORID."
  71. (setf livie-current-page 1)
  72. (setf livie-videos (livie--channel-query authorId livie-current-page livie-channel-sort-criterion))
  73. (livie--draw-channel-buffer))
  74. (defun livie-channel-next-page ()
  75. "Fetch videos from AUTHORID."
  76. (interactive)
  77. (setf livie-current-page (1+ livie-current-page))
  78. (setf livie-videos (livie--channel-query livie-search-term livie-current-page livie-channel-sort-criterion))
  79. (livie--draw-channel-buffer))
  80. (defun livie-channel-previous-page ()
  81. "Fetch videos from AUTHORID."
  82. (interactive)
  83. (when (> livie-current-page 1)
  84. (setf livie-current-page (1- livie-current-page))
  85. (setf livie-videos (livie--channel-query livie-search-term livie-current-page livie-channel-sort-criterion))
  86. (livie--draw-channel-buffer)))
  87. (defun livie-channel-sort-videos ()
  88. "Sort videos from the current channel, either by newest (default), oldest, or popular."
  89. (interactive)
  90. (setf livie-channel-sort-criterion (completing-read "Sort videos by (default value is newest): " (get 'livie-channel-sort-criterion 'custom-options)))
  91. (setf livie-current-page 1)
  92. (setf livie-videos (livie--channel-query livie-search-term livie-current-page livie-channel-sort-criterion))
  93. (livie--draw-channel-buffer))
  94. (defun livie--insert-channel-video (video)
  95. "Insert VIDEO in the current buffer."
  96. (insert (livie--format-video-published (livie-video-published video))
  97. " "
  98. (livie--format-title (livie-video-title video))
  99. " "
  100. (livie--format-video-length (livie-video-length video))
  101. " "
  102. (livie--format-video-views (livie-video-views video))))
  103. (defun livie--draw-channel-buffer ()
  104. "Draws the livie channel buffer i.e. clear everything and write down all videos in `livie-videos'."
  105. (let ((inhibit-read-only t)
  106. (current-line (line-number-at-pos)))
  107. (erase-buffer)
  108. (setq header-line-format (concat "Displaying videos from " (propertize livie-channel-author 'face 'livie-parameter-face)
  109. ", page "
  110. (propertize (number-to-string livie-current-page) 'face 'livie-parameter-face)
  111. ", sorted by: "
  112. (propertize livie-channel-sort-criterion 'face 'livie-parameter-face)))
  113. (seq-do (lambda (v)
  114. (livie--insert-channel-video v)
  115. (insert "\n"))
  116. livie-videos)
  117. (goto-char (point-min))))
  118. (defun livie--quit-channel-buffer ()
  119. "Deletes the current buffer."
  120. (interactive)
  121. (kill-buffer (current-buffer)))
  122. (provide 'livie-channel)
  123. ;; Local Variables:
  124. ;; byte-compile-warnings: (not free-vars)
  125. ;; End:
  126. ;;; livie-channel.el ends here