;;; livie-channel.el --- Auxiliary major mode for livie -*- lexical-binding: t; -*- ;; Copyright (C) 2018 - 2021 ;;; Authors: ;; Charlie Ritter ;; Jesus E. ;; Gabriele Rastello ;; Pablo BC ;;; Commentary: ;; livie grabs a list of youtube videos based on a search. ;; the user can then select a video to watch through `livie-player' ;;; Code: (defcustom livie-channel-sort-criterion "newest" "Sort videos by 'newest', 'oldest', or 'popular', as used by `livie-channel-search'." :type 'string :options '("newest" "oldest" "popular") :group 'livie-channel) (defvar livie-channel-mode-map (let ((map (make-sparse-keymap))) (set-keymap-parent map text-mode-map) (define-key map "h" #'describe-mode) (define-key map "q" #'livie--quit-channel-buffer) (define-key map ">" #'livie-channel-next-page) (define-key map "<" #'livie-channel-previous-page) (define-key map (kbd "") #'next-line) (define-key map (kbd "") #'previous-line) (define-key map "S" #'livie-channel-sort-videos) (define-key map (kbd "RET") #'livie-open-entry) (define-key map "y" #'livie-watch-this-video) map) "Keymap for `livie-channel-mode'.") (define-derived-mode livie-channel-mode livie-mode "livie-channel-mode" "Mode for displaying livie-channel-videos. \\{livie-channel-mode-map}" (buffer-disable-undo) (make-local-variable 'livie-videos) (make-local-variable 'livie-channel-author) (setq-local livie-type-of-results "video") (setf buffer-read-only t)) (defun livie--channel-query (uid n sort) "Query youtube for UID videos, return the Nth page of results, sorted bv SORT." (let ((videos (livie--API-call (concat "channels/videos/" uid) `(("page" ,n) ("sort_by" ,sort) ("fields" ,livie-default-video-query-fields))))) (dotimes (i (length videos)) (let ((v (aref videos i))) (aset videos i (livie-video--create :title (assoc-default 'title v) :author (assoc-default 'author v) :authorId (assoc-default 'authorId v) :length (assoc-default 'lengthSeconds v) :id (assoc-default 'videoId v) :views (assoc-default 'viewCount v) :published (assoc-default 'published v))))) videos)) (defun livie-channel () "Open a buffer for the channel of the current entry." (let* ((entry (livie-get-current-video)) (author (funcall (livie--get-author-function entry) entry)) (authorId (funcall (livie--get-authorId-function entry) entry))) (get-buffer-create author) (switch-to-buffer author) (unless (eq major-mode 'livie-channel-mode) (livie-channel-mode)) (setf livie-channel-author author) (setf livie-search-term authorId) (livie-channel-get-videos authorId))) (defun livie-channel-get-videos (authorId) "Fetch videos from AUTHORID." (setf livie-current-page 1) (setf livie-videos (livie--channel-query authorId livie-current-page livie-channel-sort-criterion)) (livie--draw-channel-buffer)) (defun livie-channel-next-page () "Fetch videos from AUTHORID." (interactive) (setf livie-current-page (1+ livie-current-page)) (setf livie-videos (livie--channel-query livie-search-term livie-current-page livie-channel-sort-criterion)) (livie--draw-channel-buffer)) (defun livie-channel-previous-page () "Fetch videos from AUTHORID." (interactive) (when (> livie-current-page 1) (setf livie-current-page (1- livie-current-page)) (setf livie-videos (livie--channel-query livie-search-term livie-current-page livie-channel-sort-criterion)) (livie--draw-channel-buffer))) (defun livie-channel-sort-videos () "Sort videos from the current channel, either by newest (default), oldest, or popular." (interactive) (setf livie-channel-sort-criterion (completing-read "Sort videos by (default value is newest): " (get 'livie-channel-sort-criterion 'custom-options))) (setf livie-current-page 1) (setf livie-videos (livie--channel-query livie-search-term livie-current-page livie-channel-sort-criterion)) (livie--draw-channel-buffer)) (defun livie--insert-channel-video (video) "Insert VIDEO in the current buffer." (insert (livie--format-video-published (livie-video-published video)) " " (livie--format-title (livie-video-title video)) " " (livie--format-video-length (livie-video-length video)) " " (livie--format-video-views (livie-video-views video)))) (defun livie--draw-channel-buffer () "Draws the livie channel buffer i.e. clear everything and write down all videos in `livie-videos'." (let ((inhibit-read-only t) (current-line (line-number-at-pos))) (erase-buffer) (setq header-line-format (concat "Displaying videos from " (propertize livie-channel-author 'face 'livie-parameter-face) ", page " (propertize (number-to-string livie-current-page) 'face 'livie-parameter-face) ", sorted by: " (propertize livie-channel-sort-criterion 'face 'livie-parameter-face))) (seq-do (lambda (v) (livie--insert-channel-video v) (insert "\n")) livie-videos) (goto-char (point-min)))) (defun livie--quit-channel-buffer () "Deletes the current buffer." (interactive) (kill-buffer (current-buffer))) (provide 'livie-channel) ;; Local Variables: ;; byte-compile-warnings: (not free-vars) ;; End: ;;; livie-channel.el ends here