aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-wget.el
blob: 53492570078fd93b080c6cbfa716db7e07a7ed96 (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
;;; my-wget.el -- Extensions for emacs-wget -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; This file is part of dotfiles.

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

;; dotfiles 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 Affero General
;; Public License for more details.

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

;;; Commentary:

;; Extensions for emacs-wget.

;;; Code:


;; wget
(require 'wget)
(require 'my-utils)
(defvar my-wget-video-archive-directory)
;; FIXME: this list is rather random...
(setq my-wget-video-extensions '("mp4" "flv" "mkv" "webm" "ogv" "avi"
                                 "rmvb"))
(defun my-wget-ensure-buffer-exists ()
  (get-buffer-create (or wget-process-buffer " *wget*")))
(defun my-eww-wget-save-page ()
  (interactive)
  (my-wget-ensure-buffer-exists)
  (let* ((filename
          (concat (my-make-filename (plist-get eww-data :title)) ".html"))
         (full-path (concat wget-download-directory "/" filename)))
    (wget-uri (plist-get eww-data :url)
              wget-download-directory
              (list (concat "-O" filename)))
    (kill-new full-path)
    (message "Saved webpage to %s (path copied)." full-path)))

(defun my-wget-async (url filename &optional no-tor move-if-video-or-large)
  (set-process-sentinel
   (my-start-process-with-torsocks
    no-tor "wget" "*wget*" "wget" url "-c" "-O" filename)
   (lambda (_process _event)
     (when (and move-if-video-or-large
                (or
                 (> (file-attribute-size (file-attributes filename))
                    my-wget-size-threshold)
                 (member (file-name-extension filename) my-wget-video-extensions)))
       (setq filename
             (my-rename-and-symlink-back
              filename (expand-file-name my-wget-video-archive-directory) nil)))
     (message "Fetched %s and saved to: %s" url filename))))

(defun wget-async-urls-with-prefix (urls prefix &optional no-tor move-if-video-or-large)
  (let ((i 1))
    (dolist (url urls)
      (my-wget-async
       url
       (concat prefix
               (make-string (- 4 (length (number-to-string i))) ?0)
               (number-to-string i)
               "." (file-name-extension url))
       no-tor move-if-video-or-large)
      (setq i (1+ i)))))

(provide 'my-wget)
;;; my-wget.el ends here