;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/w3m/w3m-download.el ;; ;; use with: ;; https://dataswamp.org/~incal/conf/.zsh/download (require 'file-write-to) (require 'w3m) (require 'w3m-search) (defun w3m-search-torrent (key) (interactive "smaterial: ") (w3m-search w3m-search-default-engine (format "\"%s\" \"torrent\" magnet" key) )) (defalias 'wet #'w3m-search-torrent) (defun w3m-dl-magnet () (interactive) (goto-char (point-min)) (when (re-search-forward "magnet" (point-max) t) (w3m-dl-dwim) )) (defun write-dl-URL (&optional url append) (or url (setq url (w3m-anchor))) (let*((dl-file (getenv "DL_FILE")) (url-newline (format "%s\n" url)) ) (when (and (file-exists-p dl-file) (not append) ) (delete-file dl-file)) (append-to-file url-newline nil dl-file) )) (defun w3m-img-at-point-p () (w3m-url-valid (w3m-image)) ) (defun w3m-dl-str-p (str) (when (stringp str) (or (string-match-p "^magnet:" str) (string-match-p "^https*://www.youtube.com" str) ))) (defun w3m-dl-dwim (&optional prefer-link-or-append) (interactive "P") (let*((img (w3m-img-at-point-p)) (link-str (w3m-anchor)) (special-link (w3m-dl-str-p link-str)) (url (thing-at-point 'url)) (special-url (w3m-dl-str-p url)) ) (cond (special-link (write-dl-URL link-str prefer-link-or-append)) (special-url (write-dl-URL url prefer-link-or-append)) ((and link-str (or prefer-link-or-append (not img))) (w3m-download link-str)) (img (w3m-download-this-image)) (url (w3m-download url)) (t (w3m-download w3m-current-url)) ))) (defalias 'dl #'w3m-dl-dwim) (provide 'w3m-download)