aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/reddio.el
blob: f8bc77f234873d5b3c69612bb34591f16d9969c0 (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
80
;;; reddio.el -- reddit client through reddio -*- lexical-binding: t -*-

;; Copyright (C) 2024  Free Software Foundation, Inc.

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

;; This file is part of dotted.

;; dotted 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.

;; dotted 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 dotted.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; reddit client through reddio.

;;; Code:

(defvar reddio-buffer "*reddio*")

(defvar reddio-dir (locate-user-emacs-file "reddio")
  "Path to local directory of saved threads.")

(defun reddio-make-filename (url)
  (string-match "/r/\\([^/]+\\)/comments/\\([^/]+\\)/\\([^/]+\\)" url)
  (file-name-concat
   reddio-dir
   (format "%s.%s.%s.txt"
           (match-string 1 url)
           (match-string 3 url)
           (match-string 2 url))))

(defun reddio-save-text-and-switch-to-buffer (text file-name)
  "Save TEXT to FILE-NAME and switch to buffer."
  (let ((buffer (find-file-noselect file-name))
        (coding-system-for-write 'utf-8))
    (with-current-buffer buffer
      (let ((inhibit-read-only t))
        (erase-buffer)
        (insert text))
      (goto-char (point-min))
      (save-buffer)
      (revert-buffer t t))
    (switch-to-buffer buffer)))

(defun reddio-open-url (url)
  (interactive "sReddit link: ")
  (let ((text
         (when (string-match "/\\(comments/[^/]+\\)/" url)
           (with-temp-buffer
             (if (= 0 (call-process "reddio" nil (current-buffer) nil
                                    "print" "-l" "500"
                                    (match-string 1 url)))
                 (goto-char (point-min))
               (error "reddio process failed: %s" (buffer-string)))
             (delete-trailing-whitespace)
             (buffer-string)))))
    (reddio-save-text-and-switch-to-buffer
     text
     (reddio-make-filename url))))

(defun reddio-reddit-url-p (url)
  "e.g.
https://www.reddit.com/r/linux/comments/cs3os6/introducing_reddio_a_commandline_interface_for/"
  (let ((urlobj (url-generic-parse-url url)))
    (and (string-match-p "^.*\\<reddit.com$" (url-host urlobj))
         (string-match-p "^/r/[^/]+/comments/[^/]+/.+$" (url-filename urlobj)))))

(provide 'reddio)
;;; reddio.el ends here