From 6468bf806408013d914dc711c31b748795a2c36e Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Sat, 1 Jul 2023 08:30:02 +0300 Subject: yt-search: base function completed - Searches for X amount of videoids and videotitles - Creates a read only org buffer and inserts the results --- yeetube.el | 60 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/yeetube.el b/yeetube.el index df7ca64..82cc3bb 100644 --- a/yeetube.el +++ b/yeetube.el @@ -75,41 +75,41 @@ Example Usage: (message "Opening %s with mpv" url)))) - -;; TODO Break this function into smaller ones, repeat search for unique videoIds +;; TODO: Check if video_type of videoid is short or video (defun yt-search (arg) - "Search for ARG in YouTube." - (interactive "sYoutube Search: ") + "Search for ARG." + (interactive "sYeetube Search: ") (let ((videoIds '()) - (videoTitles '())) + (videoTitles '())) (with-current-buffer (url-retrieve-synchronously (concat yt-search-query arg)) (goto-char (point-min)) + (while (< (length videoIds) yt-search-for) (search-forward "videoId") - (let* ((start (point)) - (end (search-forward ",")) - (videoid (buffer-substring (+ start 3) (- end 2)))) - (if (not (member videoid videoIds)) - (push videoid videoIds))) - (search-forward "text") - (let* ((start (point)) - (end (search-forward ",")) - (title (buffer-substring (+ start 3) (- end 4)))) - (if (not (member title videoTitles)) - (push title videoTitles))) - (with-current-buffer (switch-to-buffer - (get-buffer-create "*Youtube Search*")) - (setq buffer-read-only nil) - (erase-buffer) - (org-mode) - (dolist (videoId videoIds) - (dolist (videoTitle videoTitles) - (insert (format "- [[https://www.youtube.com/watch?v=%s][%s]] \n" - videoId - videoTitle)))) - (setq buffer-read-only t))))) - - -;; TODO: let user decide custom name and path + (let* ((start (point)) + (end (search-forward ",")) + (videoid (buffer-substring (+ start 3) (- end 2)))) + (unless (or (member videoid videoIds) + (not (and (>= (length videoid) 9) + (<= (length videoid) 13) + (string-match-p "^[a-zA-Z0-9_-]*$" videoid)))) + (push videoid videoIds) + (search-forward "text") + (let* ((start (point)) + (end (search-forward ",")) + (title (buffer-substring (+ start 3) (- end 4)))) + (push title videoTitles)))))) + + (with-current-buffer (switch-to-buffer + (get-buffer-create "*Yeetube Search*")) + (setq buffer-read-only nil) + (erase-buffer) + (org-mode) + (insert "\n* Search Results: \n \n") + (cl-loop for (videoId . videoTitle) in (cl-mapcar #'cons (reverse videoIds) (reverse videoTitles)) + do (insert (format "+ [[https://www.youtube.com/watch?v=%s][%s]]\n" + videoId videoTitle))) + (setq buffer-read-only t)))) + (defun yt-download-video () "Download using link at point in an `'org-mode buffer with yt-dlp." (interactive) -- cgit v1.2.3