add webpage to collection wrapper/internal

This commit is contained in:
Levi Neely 2024-09-17 15:23:37 +02:00
parent 277d15d4bd
commit a301634140
1 changed files with 17 additions and 43 deletions

View File

@ -13,6 +13,7 @@
(require 'url)
(require 'time-date)
(require 'eww)
(defvar collect/store-path "~/collections")
@ -163,37 +164,6 @@
(file-name-nondirectory file-to-add)
selected-collection)))))
(defun collect/add-webpage-to-collection ()
"Add a webpage to a specified collection."
(interactive)
(let* ((url (read-string "Enter URL to add: "))
(all-files (directory-files collect/store-path t "^[^.]"))
(collections (cl-remove-if
(lambda (dir)
(or (not (file-directory-p dir))
(string= (file-name-nondirectory dir) "no-name")))
all-files))
(collection-names
(mapcar (lambda (dir)
(let ((metadata-file (expand-file-name ".collection-metadata" dir)))
(if (file-exists-p metadata-file)
(with-temp-buffer
(insert-file-contents metadata-file)
(if (re-search-forward "^name: \\(.*\\)$" nil t)
(match-string 1)
(file-name-nondirectory dir)))
(file-name-nondirectory dir))))
collections))
(selected-collection (completing-read "Select collection to add to: " collection-names nil t))
(target-dir (nth (cl-position selected-collection collection-names :test 'string=) collections)))
(when (and url target-dir)
(plz 'get url
:as 'response
:then (lambda (response)
(collect/process-webpage-response url response target-dir selected-collection))
:else (lambda (error)
(message "Error fetching webpage: %s" error))))))
(defun collect/get-collections ()
"Get a list of collections with their names and directories."
(let ((all-dirs (directory-files collect/store-path t "^[^.]")))
@ -222,19 +192,23 @@
(let* ((url (read-string "Enter URL to add: "))
(collections (collect/get-collections))
(selected-collection (completing-read "Select collection to add to: " (mapcar #'car collections) nil t))
(target-dir (cdr (assoc selected-collection collections)))
(filename (collect/generate-filename url))
(target-file (expand-file-name filename target-dir)))
(target-dir (cdr (assoc selected-collection collections))))
(when (and url (file-directory-p target-dir))
(url-retrieve url
(lambda (status target-file filename collection-name)
(if (plist-get status :error)
(message "Error fetching webpage: %s" (plist-get status :error))
(goto-char (point-min))
(re-search-forward "\n\n")
(write-region (point) (point-max) target-file)
(message "Webpage '%s' added to collection '%s'." filename collection-name)))
(list target-file filename selected-collection)))))
(collect/add-webpage-to-collection-internal url target-dir selected-collection))))
(defun collect/add-webpage-to-collection-internal (url target-dir collection-name)
"Internal function to add a webpage to a collection."
(let* ((filename (collect/generate-filename url))
(target-file (expand-file-name filename target-dir)))
(url-retrieve url
(lambda (status target-file filename collection-name)
(if (plist-get status :error)
(message "Error fetching webpage: %s" (plist-get status :error))
(goto-char (point-min))
(re-search-forward "\n\n")
(write-region (point) (point-max) target-file)
(message "Webpage '%s' added to collection '%s'." filename collection-name)))
(list target-file filename collection-name))))
(defun collect/generate-filename (url)
"Generate a filename for the given URL."