aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-ttrss.el
blob: 3cf68569403367c849c5c248a76849c5def50197 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
;;; my-ttrss.el -- ttrss utilities -*- lexical-binding: t -*-

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

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

;; 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:

;; ttrss utilities.

;;; Code:

(require 'ttrss)

;;; TODO: my-ttrss-save-recent

(defun my-ttrss-hello ()
  (let ((sid (ttrss-login ttrss-address ttrss-user ttrss-password)))
    (message "Server running version %s and API level %d\n"
  		       (ttrss-get-version ttrss-address sid)
  		       (ttrss-get-api-level ttrss-address sid))
    (message "There are %s unread articles in %d feeds"
  		       (ttrss-get-unread ttrss-address sid)
  		       (length (ttrss-get-feeds ttrss-address sid :unread_only t)))))

(defun my-ttrss-fetch-feeds ()
  (let ((sid (ttrss-login ttrss-address ttrss-user ttrss-password)))
    (ttrss-get-feeds ttrss-address sid :cat_id -3)))

(defun my-ttrss-feed-dir (feed-title feed-id)
  (file-name-concat
   my-ttrss-dir
   (my-make-doc-file-name
    (format "%s [ttrss%s]" feed-title feed-id))))

(defun my-ttrss-feed-last-id-file (feed-info)
  (let* ((title (plist-get feed-info :title))
         (id (plist-get feed-info :id)))
    (expand-file-name
     (file-name-concat (my-ttrss-feed-dir title id) "last-id"))))

(defun my-ttrss-feed-get-last-id (feed-info)
  (let* ((file (my-ttrss-feed-last-id-file feed-info)))
    (if (file-exists-p file)
        (with-temp-buffer
          (insert-file-contents file)
          (string-to-number (buffer-string)))
      0)))

(defun my-ttrss-feed-write-last-id (feed-info last-id)
  (let* ((file (my-ttrss-feed-last-id-file feed-info))
         (inhibit-message t))
    (with-temp-buffer
      (insert (format "%d" last-id))
      (write-file file))))

(defun my-ttrss-fetch ()
  "Fetch and save latest articles from all feeds."
  (interactive)
  (let* ((sid (ttrss-login ttrss-address ttrss-user ttrss-password))
         (feeds (ttrss-get-feeds ttrss-address sid :cat_id -3))
         (n (length feeds)))
    (seq-do-indexed
     (lambda (feed i)
       (my-ttrss-fetch-feed-articles
        sid feed
        (format "[my-ttrss] (%d/%d) Fetching articles from %s..."
                (1+ i) n (plist-get feed :title))))
     feeds)))

(defun my-ttrss-fetch-feed-articles (sid feed &optional message-head)
  (unless message-head
    (setq message-head
          (format "[my-ttrss] Fetching articles from %s..."
                  (plist-get feed :title))))
  (message "%s" message-head)
  (let ((articles
         (ttrss-get-headlines
          ttrss-address sid
          :feed_id (plist-get feed :id) :show_content t :include_attachments t
          :since_id (my-ttrss-feed-last-id feed))))
    (seq-do 'my-ttrss-save-article articles)
    (unless (seq-empty-p articles)
      (my-ttrss-feed-write-last-id feed (plist-get (elt articles 0) :id)))
    (message
     "%s Done - total %d articles"
     message-head (length articles))))

(defun my-ttrss-fetch-latest-articles (n &optional since-id)
  "Fetch N latest articles."
  (let ((sid (ttrss-login ttrss-address ttrss-user ttrss-password)))
    (unless since-id (setq since-id 0))
    (if (ttrss-logged-in-p ttrss-address sid)
        (ttrss-get-headlines
         ttrss-address sid
         :feed_id -4 :limit n :show_content t :include_attachments t
         :since_id since-id)
      (message "Login failed"))))

(defun my-ttrss-save-article (info)
  (with-temp-buffer
    (insert "<!--\n Page saved with my-ttrss on "
            (current-time-string) "\n"
            (json-serialize (org-plist-delete info :content))
            "\n-->\n")
    (insert "<h2>" "<a href=\"" (plist-get info :link) "\">"
            (plist-get info :title) "</a>" "</h2>")
    (insert "<p>" "<a href=\"" (plist-get info :site_url) "\">"
            (plist-get info :feed_title) "</a>")
    (when-let ((author (plist-get info :author)))
      (unless (or (string-empty-p author)
                  (equal author (plist-get info :feed_title)))
        (insert " (" author ")")))
    (insert " "
            (format-time-string
             "%Y-%m-%d %a %H:%M:%S"
             (encode-time (decode-time (plist-get info :updated)))))
    (insert "</p>")
    (let ((tags (plist-get info :tags)))
      (unless (seq-empty-p tags)
        (insert "<p>tags: " (string-join tags ";") "</p>")))
    (insert (plist-get info :content))
    (let ((attached (plist-get info :attachments)))
      (unless (seq-empty-p attached)
        (insert "<p>Article attachments:</p>\n<ul>")
        (seq-do (lambda (at)
                  (let ((title (plist-get at :title))
                        (url (plist-get at :content_url)))
                    (insert "\n<li><a href=" url ">"
                            (if (string-empty-p title) url title)
                            "</a></li>")))
                attached)
        (insert "\n</ul>")))
    (let* ((change-major-mode-with-file-name nil)
           (coding-system-for-write 'utf-8)
           (inhibit-message t)
           (file-name (my-ttrss-format-file-name info))
           (dir (file-name-directory file-name)))
      (unless (file-exists-p dir) (make-directory dir t))
      (write-file file-name))))

(defvar my-ttrss-dir "~/Downloads")

(defun my-ttrss-format-file-name (info)
  "Format: $author - $title ($year) [ttrss$id].html"
  (let* ((author (plist-get info :author))
         (feed-title (plist-get info :feed_title))
         (feed-id (plist-get info :feed_id))
         (name
          (expand-file-name
           (file-name-concat
            (my-ttrss-feed-dir feed-title feed-id)
            (my-make-doc-file-name
             (format "%s - %s (%s) [ttrss%s].html"
                     (if (string-empty-p author)
                         feed-title
                       author)
                     (plist-get info :title)
                     (format-time-string
                      "%Y"
                      (encode-time (decode-time (plist-get info :updated))))
                     (plist-get info :id)))))))
    (if (length> name 250)
        (expand-file-name
         (file-name-concat
          (my-ttrss-feed-dir feed-title feed-id)
          (my-make-doc-file-name
           (format "_ - _ (%s) [ttrss%s].html"
                   (format-time-string
                    "%Y"
                    (encode-time (decode-time (plist-get info :updated))))
                   (plist-get info :id)))))
      name)))

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