aboutsummaryrefslogtreecommitdiffstats
path: root/livie.el
blob: 6187552bb74871180aca9085625d8d7114dd5fee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
;;; livie.el --- Livie is Video in Emacs -*- lexical-binding: t; -*-

;; Copyright (C) 2018

;;; Authors:

;; Charlie Ritter <chewzerita@posteo.net>
;; Jesus E. <heckyel@hyperbola.info>

;;; 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:

(require 'f)

(defgroup livie '()
  "Livie is Video in Emacs"
  :prefix "livie-"
  :group 'livie)

(defcustom livie-player "mpv"
  "Default video player to use with livie."
  :group 'livie
  :type 'string)

(defcustom livie-player-args '()
  "Command line arguments for `livie-player'."
  :group 'livie
  :type '(list)
  :tag "Livie Player Arguments")

(defcustom livie-python-name "python3"
  "Name of the python executable."
  :group 'livie
  :type 'string)

(defcustom livie-script-path nil
  "Full path of livie.py."
  :group 'livie
  :type 'file)

(defcustom livie-buffer-name "*livie*"
  "Name of the buffer to show results."
  :group 'livie
  :type 'string)

(defvar livie-youtube-regexp
  "https://invidious.snopyta.org/latest_version\\?id=[A-Za-z0-9_\\-]\\{11\\}&itag=\\<\\([0-9]*\\.[0-9]+\\|[0-9]+\\)[df]?\\>&local=true")

(define-derived-mode livie-mode
  special-mode "livie"
  "Major mode for livie.")

(defun livie-close-window ()
  "Close the livie window and bury the buffer."
  (interactive)
  (bury-buffer)
  (delete-window)
  (message nil))

(defun livie-next-video ()
  "Goto the next video in the buffer."
  (interactive)
  (forward-line 1)
  (search-forward-regexp livie-youtube-regexp)
  (livie-prev-video))

(defun livie-prev-video ()
  "Goto the previous video in the buffer."
  (interactive)
  (search-backward-regexp livie-youtube-regexp))

(defun livie-this-video ()
  "Go to the start of the current video."
  (interactive)
  (move-beginning-of-line nil)
  (ignore-errors (forward-line -1))
  (livie-next-video))

(defun livie-copy-video ()
  "Copy the currently selected video into the kill ring."
  (interactive)
  (livie-this-video)
  (push-mark)
  (move-end-of-line nil)
  (kill-ring-save nil nil t)
  (livie-this-video))

(defun livie-watch-this-video ()
  "Watch video under the cursor."
  (interactive)
  (livie-copy-video)
  (livie-watch (car kill-ring))
  (delete-other-windows))

(defun livie-watch (url)
  "Watch video at URL using `livie-player' and `livie-player-args'."
  (interactive "sURL: ")
  (apply 'start-process
         (cl-concatenate
          'list
          (list "livie"
                nil
                livie-player)
          livie-player-args
          (list url)))
  (message "Loading video..."))

(defun livie (query)
  "Livie is Video in Emacs.

Livie will prompt the user for a QUERY.  This is fed into a
python script that scrapes youtube.com for search results.  The
results are displayed in an Emacs buffer.

See also: `livie-mode'."
  (interactive "sSearch: ")
  (unless livie-script-path
    (error "Please set `livie-script-path'"))
  (if (equal (buffer-name) livie-buffer-name)
      (progn
        (read-only-mode -1)
        (erase-buffer))
    (progn
      (select-window (split-window))
      (ignore-errors (kill-buffer livie-buffer-name))
      (switch-to-buffer (generate-new-buffer livie-buffer-name))))
  (call-process livie-python-name
                nil t t
                livie-script-path
                query)
  (livie-mode)
  (font-lock-ensure)
  (goto-char (point-min))
  (message (concat "Results for: " query)))

(font-lock-add-keywords
 'livie-mode
 `((,livie-youtube-regexp . 'link)
   ("title: \\(.*\\)" 1 'bold)
   ("channel: \\(.*\\)" 1 'italic)
   ("^ +[a-zA-Z]+:" . 'shadow)))

(define-key livie-mode-map "s" 'livie)
(define-key livie-mode-map "q" 'livie-close-window)
(define-key livie-mode-map (kbd "<tab>") 'livie-next-video)
(define-key livie-mode-map (kbd "<backtab>") 'livie-prev-video)
(define-key livie-mode-map "n" 'livie-next-video)
(define-key livie-mode-map "p" 'livie-prev-video)
(define-key livie-mode-map (kbd "<return>") 'livie-watch-this-video)

(provide 'livie)

;;; livie.el ends here