From 7b7dc00de551543a102ad7baff9e234cb0da8cb2 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sat, 11 Jan 2025 21:32:19 +1100 Subject: [emacs] Add ytdl to infobox --- emacs/.emacs.d/init/ycp-web.el | 2 ++ emacs/.emacs.d/lisp/my/infobox.el | 13 +++++++++++-- emacs/.emacs.d/lisp/my/my-ytdl.el | 19 +++++++++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/emacs/.emacs.d/init/ycp-web.el b/emacs/.emacs.d/init/ycp-web.el index a4fa8f5..7df2857 100644 --- a/emacs/.emacs.d/init/ycp-web.el +++ b/emacs/.emacs.d/init/ycp-web.el @@ -257,6 +257,8 @@ . ,(lambda (url &rest _) (hnreader-comment url))) (my-gitlab-project-url-p . ,(lambda (url &rest _) (my-gitlab-project-infobox url))) + (my-ytdl-video-url-p + . ,(lambda (url &rest _) (my-ytdl-video-infobox url))) (my-mastodon-url-p . ,(lambda (url &rest _) (mastorg-open url))) (my-newscorp-au-url-p diff --git a/emacs/.emacs.d/lisp/my/infobox.el b/emacs/.emacs.d/lisp/my/infobox.el index 81a66ce..0b84810 100644 --- a/emacs/.emacs.d/lisp/my/infobox.el +++ b/emacs/.emacs.d/lisp/my/infobox.el @@ -27,6 +27,15 @@ ;;; Code: +(defun infobox-default-specs (info) + (seq-map + (lambda (pair) + (cons (car pair) + (replace-regexp-in-string + "[-_]" " " + (capitalize (format "%s" (car pair)))))) + info)) + (defun infobox-translate (info specs) "Translate INFO according to SPECS. @@ -45,7 +54,7 @@ something like (defun infobox-render (info item &optional interactive-p) "Render and display a help buffer of INFO." (help-setup-xref item interactive-p) - (with-help-window (help-buffer) + (with-help-window "*infobox*" (with-current-buffer standard-output (let ((n-rows 0)) (seq-do @@ -54,7 +63,7 @@ something like (when (stringp (car pair)) (insert (car pair) ": ") (setq n-rows (1+ n-rows))) - (insert (cdr pair) "\n"))) + (insert (format "%s" (cdr pair)) "\n"))) info) (align-regexp (point-min) diff --git a/emacs/.emacs.d/lisp/my/my-ytdl.el b/emacs/.emacs.d/lisp/my/my-ytdl.el index d3998ee..2811793 100644 --- a/emacs/.emacs.d/lisp/my/my-ytdl.el +++ b/emacs/.emacs.d/lisp/my/my-ytdl.el @@ -79,15 +79,30 @@ (defun my-ytdl-video-info (url) "Given a video URL, return an alist of its properties." (with-temp-buffer - (call-process my-ytdl-program nil t nil "-j" url) + (call-process my-ytdl-program nil t nil "--no-warnings" "-j" url) (let ((start (point))) (call-process-region nil nil "jq" nil t nil - "pick(.webpage_url, .fulltitle, .description, .channel_url, .channel, .channel_follower_count, .thumbnail, .duration_string, .view_count, .upload_date, .like_count, .is_live, .was_live, .categories, .tags, .chapters, .availability)") + "pick(.webpage_url, .fulltitle, .channel_url, .channel, .channel_follower_count, .thumbnail, .duration_string, .view_count, .upload_date, .like_count, .is_live, .was_live, .categories, .tags, .chapters, .availability, .uploader, .description)") (goto-char start) (json-read))) ) +(defun my-ytdl-video-url-p (url) + (let ((urlobj (url-generic-parse-url url))) + (or (and (string-match-p "^\\(www\\.\\)?youtube.com" (url-host urlobj)) + (string-match-p "^/watch\\?v=.*" (url-filename urlobj))) + (equal "youtu.be" (url-host urlobj))))) + +(defun my-ytdl-video-infobox (url) + (interactive "sytdl video url: ") + (let* ((info (my-ytdl-video-info url)) + (specs (infobox-default-specs info))) + (infobox-render + (infobox-translate info specs) + `(my-ytdl-video-infobox ,url) + (called-interactively-p 'interactive)))) + ;;; fixme: autoload (defun my-ytdl-video (urls) "Download videos with ytdl." -- cgit v1.2.3