summaryrefslogtreecommitdiffstats
path: root/emacsconf-toobnix.el
blob: 7e42d209cf8556d50405761a9a9d6a9621c06b46 (plain) (blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
;;; emacsconf-toobnix.el --- Use the Toobnix REST API  -*- lexical-binding: t; -*-

;; Copyright (C) 2025  Sacha Chua

;; Author: Sacha Chua <sacha@sachachua.com>
;; Keywords: multimedia

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; https://docs.joinpeertube.org/api/rest-getting-started
;; https://docs.joinpeertube.org/api-rest-reference.html
;;
;; Get the access token with M-x `emacsconf-toobnix-api-setup'.

;;; Code:

(defvar emacsconf-toobnix-upload-command "peertube-cli")
(defvar emacsconf-toobnix-channel "EmacsConf")

(defun emacsconf-toobnix-update-video-description (talk &optional type)
  "Update the description for TALK.
TYPE is 'talk or 'answers."
  (interactive
   (let ((talk (emacsconf-complete-talk-info)))
     (list
      talk
      (if (plist-get talk :qa-toobnix-url)
          (intern (completing-read "Type: " '("talk" "answers")))
        'talk))))
  (setq type (or type 'talk))
  (let* ((properties
          (pcase type
            ('answers (emacsconf-publish-answers-video-properties talk 'toobnix))
            (_ (emacsconf-publish-talk-video-properties talk 'toobnix))))
         (id
          (emacsconf-toobnix-id-from-url
           (plist-get talk (pcase type
                             ('answers :qa-toobnix-url)
                             (_ :toobnix-url)))))
         (boundary (format "%s%d" (make-string 20 ?-)
                           (time-to-seconds)))
         (url-request-method "PUT")
         (url-request-extra-headers
          (cons (cons "Content-Type"
                      (concat "multipart/form-data; boundary=" boundary))
                (emacsconf-toobnix-api-header)))
         (url-request-data
          (mm-url-encode-multipart-form-data
                     `(("description" .
                        ,(replace-regexp-in-string
                          "\n" "\r\n"
                          (plist-get properties :description))))
                     boundary))
         (url (concat "https://toobnix.org/api/v1/videos/" id)))
    (with-current-buffer (url-retrieve-synchronously url)
      (prog1 (buffer-string)
        (kill-buffer (current-buffer))))))

(defun emacsconf-toobnix-upload-all ()
  (interactive)
  (dolist (talk (emacsconf-publish-prepare-for-display (emacsconf-get-talk-info)))
    (unless (or (plist-get talk :toobnix-url)
                (null (plist-get talk :video-file)))
      (message "Uploading %s" (plist-get talk :slug))
      (emacsconf-publish-upload-talk talk 'toobnix))
    (unless (or (plist-get talk :qa-toobnix)
                (null (plist-get talk :qa-video-file)))
      (message "Uploading %s answers" (plist-get talk :slug))
      (emacsconf-publish-upload-answers talk 'toobnix))))

(defun emacsconf-toobnix-upload (properties)
	"Uses peertube-cli: https://github.com/Chocobozzz/PeerTube/blob/develop/support/doc/tools.md"
	(with-temp-buffer
		(let ((arguments
					 (append
						(list "upload" "-f" (plist-get properties :file))
						(when (plist-get properties :title)
							(list "-n" (plist-get properties :title)))
						(when (plist-get properties :description)
							(list "-d" (plist-get properties :description)))
						(list "-L" "en"
									"-C" emacsconf-toobnix-channel
									"-l" "2"
                  "--verbose" "debug"
									"-c" "15"
									"-P" (if (string= (plist-get properties :privacy) "public") "1" "2") "-t"
									(cond
									 ((stringp (plist-get properties :tags))
										(plist-get properties :tags))
									 ((listp (plist-get properties :tags))
										(string-join (plist-get properties :tags) ","))
									 (t "emacs"))))))
			(kill-new (mapconcat
								 #'shell-quote-argument
								 (append (list emacsconf-toobnix-upload-command) arguments)
								 " "))
			(apply #'call-process
						 emacsconf-toobnix-upload-command
						 nil t t arguments)
			(buffer-string))))

(defun emacsconf-toobnix-step-through-publishing-talk (talk)
  (interactive (list (emacsconf-complete-talk-info
                      (seq-remove
                       (lambda (talk)
                         (or (not (plist-get talk :video-file))
                             (plist-get talk :toobnix-url)))
                       (emacsconf-get-talk-info)))))
  (kill-new (plist-get talk :video-file))
  (y-or-n-p
   (format "Video: %s - create video and upload this filename. Done?"
           (plist-get talk :video-file)))
  (kill-new (emacsconf-publish-video-description talk t))
  (y-or-n-p "Copied description. Paste into description, move first line to title, add to playlist. Done?")
  (when (emacsconf-talk-file talk "--main.vtt")
    (kill-new (emacsconf-talk-file talk "--main.vtt"))
    (y-or-n-p (format "Captions: %s. Add to video elements. Done?"
                      (emacsconf-talk-file talk "--main.vtt"))))
  (emacsconf-set-property-from-slug
   (plist-get talk :slug)
   "TOOBNIX_URL"
   (read-string (format "%s - Toobnix URL: " (plist-get talk :scheduled)))))

(defun emacsconf-toobnix-step-through-publishing-all ()
	(interactive)
	(catch 'done
		(while t
			(let ((talk (seq-find (lambda (o)
															(and (not (plist-get o :toobnix-url))
																	 (emacsconf-talk-file o "--main.webm")))
														(emacsconf-publish-prepare-for-display (emacsconf-get-talk-info)))))
				(unless talk
					(message "All done so far.")
					(throw 'done t))
        (emacsconf-toobnix-step-through-publishing-talk talk)))))

(defun emacsconf-toobnix-id-from-url (url)
  (when (string-match "https://toobnix.org/\\(?:w\\|videos/watch\\)/\\(.+\\)" url)
    (match-string 1 url)))

(defun emacsconf-toobnix-add-captions (talk type)
  (interactive (list (emacsconf-complete-talk-info)
                     (intern (completing-read "Type of talk: " '("talk" "answers")))))
  (let* ((url (format "https://toobnix.org/api/v1/videos/%s/captions/en"
						          (emacsconf-toobnix-id-from-url (plist-get talk (pcase type
                                                              ('talk :toobnix-url)
                                                              ('answers :qa-toobnix-url))))))
         (filename (expand-file-name
                    (concat (plist-get talk :file-prefix)
                            (pcase type
                              ('talk "--main.vtt")
                              ('answers "--answers.vtt")))
                    emacsconf-cache-dir))
         (file-arg (concat "captionfile=@" filename))
         (auth-header (emacsconf-toobnix-api-header)))
    (with-temp-buffer
      (call-process "curl" nil t nil
                    "-s"
                    "-i"
                    "--request" "PUT"
                    "--header" (concat (caar auth-header) ": " (cdar auth-header))
                    "--form" file-arg
                    url)
      (buffer-string))))

(defun emacsconf-toobnix-upload-all-captions ()
  (interactive)
  (dolist (talk (emacsconf-publish-prepare-for-display (emacsconf-get-talk-info)))
    (when (plist-get talk :toobnix-url)
      (message "Uploading captions for %s" (plist-get talk :slug))
      (emacsconf-toobnix-add-captions talk 'talk))
    (when (plist-get talk :qa-toobnix-url)
      (message "Uploading captions for %s Q&A" (plist-get talk :slug))
      (emacsconf-toobnix-add-captions talk 'answers))))

(defun emacsconf-toobnix-edit ()
  (interactive)
  (let ((url (org-entry-get (point) "TOOBNIX_URL")))
    (if url
        (when (string-match "/w/\\([A-Za-z0-9]+\\)" url)
          (browse-url (format "https://toobnix.org/videos/update/%s" (match-string 1 url))))
      (when (> (length (org-entry-get (point) "FILE_PREFIX")) 80)
        (copy-file (expand-file-name (concat (org-entry-get (point) "FILE_PREFIX") "--main.webm") emacsconf-cache-dir)
                   (expand-file-name (concat "emacsconf-" emacsconf-year "-" (org-entry-get (point) "SLUG") ".webm") emacsconf-cache-dir) t))
      (browse-url "https://toobnix.org/videos/upload#upload"))))


(defvar emacsconf-toobnix-api-client nil)
(defvar emacsconf-toobnix-api-bearer-token nil)
(defvar emacsconf-toobnix-api-username "bandali")
(defvar emacsconf-toobnix-api-channel-handle "emacsconf")

(defun emacsconf-toobnix-api-header ()
	`(("Authorization" . ,(concat "Bearer "
																(if (stringp emacsconf-toobnix-api-bearer-token)
																		emacsconf-toobnix-api-bearer-token
																	(assoc-default 'access_token emacsconf-toobnix-api-bearer-token))))))

(defun emacsconf-toobnix-api-setup ()
	(interactive)
	(require 'plz)
	(require 'url-http-oauth)
	(setq emacsconf-toobnix-api-client
				(plz 'get "https://toobnix.org/api/v1/oauth-clients/local" :as #'json-read))
	(setq emacsconf-toobnix-api-bearer-token
				(assoc-default
				 'access_token
				 (plz 'post "https://toobnix.org/api/v1/users/token"
					 :body (mm-url-encode-www-form-urlencoded
									`(("client_id" . ,(assoc-default 'client_id emacsconf-toobnix-api-client))
										("client_secret" . ,(assoc-default 'client_secret emacsconf-toobnix-api-client))
										("grant_type" . "password")
										("username" . ,emacsconf-toobnix-api-username)
										("password" . ,(auth-info-password (car (auth-source-search :host "https://toobnix.org"))))))
					 :as #'json-read)
				 )
)
	(setq emacsconf-toobnix-api-channels
				(plz 'get (format "https://toobnix.org/api/v1/accounts/%s/video-channels"
													emacsconf-toobnix-api-username)
					:headers (emacsconf-toobnix-api-header)
					:as #'json-read))
	(setq emacsconf-toobnix-api-videos
				(plz 'get
					(format "https://toobnix.org/api/v1/accounts/%s/videos?count=100&sort=-createdAt"
									emacsconf-toobnix-api-username)
					:headers (emacsconf-toobnix-api-header)
					:as #'json-read))
	(setq emacsconf-toobnix-api-playlists
				(append
				 (assoc-default 'data
												(plz 'get
													(format "https://toobnix.org/api/v1/video-channels/%s/video-playlists?sort=-createdAt"
																	emacsconf-toobnix-api-channel-handle)
													:headers (emacsconf-toobnix-api-header)
													:as #'json-read))
				 nil)))

(defun emacsconf-toobnix-latest-video-url (&optional props)
  (if props
      (alist-get 'url
                 (seq-find (lambda (o)
                             (string= (alist-get 'name o)
                                      (plist-get props :title)))
                  (alist-get 'data
                             (plz 'get
		                           (format "https://toobnix.org/api/v1/accounts/%s/videos?count=100&sort=-createdAt"
						                           emacsconf-toobnix-api-username)
		                           :headers (emacsconf-toobnix-api-header)
		                           :as #'json-read)
                             )))
    (alist-get 'url
               (car (alist-get 'data
                               (plz 'get
                                 (format "https://toobnix.org/api/v1/accounts/%s/videos?count=1&sort=-createdAt"
                                         emacsconf-toobnix-api-username)
                                 :headers (emacsconf-toobnix-api-header)
                                 :as #'json-read))))))



(defun emacsconf-toobnix-video-captions (url)
  (when (string-match "https://toobnix.org/\\(?:w\\|videos/watch\\)/\\(.+\\)" url)
    (let ((id (match-string 1 url)))
      (alist-get 'data (plz 'get
		                     (format "https://toobnix.org/api/v1/videos/%s/captions"
						                     id)
		                     :headers (emacsconf-toobnix-api-header)
		                     :as #'json-read)))))

(defun emacsconf-toobnix-publish-video-from-edit-page ()
	"Messy hack to set a video to public and store the URL."
	(interactive)
	(spookfox-js-injection-eval-in-active-tab "document.querySelector('label[for=privacy]').scrollIntoView(); document.querySelector('label[for=privacy]').closest('.form-group').querySelector('input').dispatchEvent(new Event('input'));" t)
	(sit-for 1)
	(spookfox-js-injection-eval-in-active-tab "document.querySelector('span[title=\"Anyone can see this video\"]').click()" t)
	(sit-for 1)
	(spookfox-js-injection-eval-in-active-tab "document.querySelector('button.orange-button').click()" t)(sit-for 3)
	(emacsconf-extract-store-url)
	(shell-command "xdotool key Alt+Tab sleep 1 key Ctrl+w Alt+Tab"))

(defun emacsconf-toobnix-set-up-playlist ()
	(interactive)
	(mapcar
	 (lambda (o)
		 (when (plist-get o :toobnix-url)
			 (browse-url (plist-get o :toobnix-url))
			 (read-key "press a key when page is loaded")
			 (spookfox-js-injection-eval-in-active-tab "document.querySelector('.action-button-save').click()" t)
			 (spookfox-js-injection-eval-in-active-tab "document.querySelector('my-peertube-checkbox').click()" t)
			 (read-key "press a key when saved to playlist"))
		 (when (plist-get o :qa-toobnix-url)
			 (browse-url (plist-get o :qa-toobnix-url))
			 (read-key "press a key when page is loaded")
			 (spookfox-js-injection-eval-in-active-tab "document.querySelector('.action-button-save').click()" t)
			 (spookfox-js-injection-eval-in-active-tab "document.querySelector('my-peertube-checkbox').click()" t)
			 (read-key "press a key when saved to playlist")))
	 (emacsconf-publish-prepare-for-display (emacsconf-get-talk-info))))

(defun emacsconf-toobnix-view (talk)
	(interactive (list (emacsconf-complete-talk-info)))
	(browse-url (plist-get (emacsconf-resolve-talk talk) :toobnix-url)))

(defun emacsconf-toobnix-view-qa (talk)
	(interactive (list (emacsconf-complete-talk-info)))
	(browse-url (plist-get (emacsconf-resolve-talk talk) :qa-toobnix-url)))


(provide 'emacsconf-toobnix)
;;; emacsconf-toobnix.el ends here