2021-01-08 18:21:43 +01:00
|
|
|
;; tinmop: an humble gemini and pleroma client
|
|
|
|
;; Copyright (C) 2020 cage
|
|
|
|
|
|
|
|
;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(in-package :gemini-subscription)
|
|
|
|
|
|
|
|
(defun link-post-timestamp (link-text)
|
|
|
|
"Returns a local-time object parsing a gemlog entry's link text
|
|
|
|
|
|
|
|
A link text entry is like 'aaaa-mm-dd post title'
|
|
|
|
|
|
|
|
This function parses the 'aaaa-mm-dd' part.
|
|
|
|
"
|
2021-01-09 16:27:40 +01:00
|
|
|
(when (>= (length link-text) 10)
|
|
|
|
(local-time:parse-timestring link-text :start 0 :end 10 :fail-on-error nil)))
|
2021-01-08 18:21:43 +01:00
|
|
|
|
|
|
|
(defun link-post-title (link-text)
|
|
|
|
"Returns the title of gemlog entry from link text
|
|
|
|
|
|
|
|
A link text entry is like 'aaaa-mm-dd post title'
|
|
|
|
|
|
|
|
This function return the 'post-title' substring."
|
|
|
|
(misc:safe-subseq link-text 10))
|
|
|
|
|
|
|
|
(defun link-post-timestamp-p (link-text)
|
|
|
|
"Is this a valid gemlog link text?"
|
|
|
|
(link-post-timestamp link-text))
|
|
|
|
|
|
|
|
(defun subscribe (url)
|
|
|
|
"Subscribe to a gemlog that can be found at 'url'"
|
|
|
|
(labels ((subtitle-p (nodes h2-pos)
|
|
|
|
(when h2-pos
|
|
|
|
(let ((res t))
|
2021-05-14 14:49:37 +02:00
|
|
|
(loop for i from (1- h2-pos) downto 0 do
|
2021-01-08 18:21:43 +01:00
|
|
|
(let ((node (elt nodes i)))
|
|
|
|
(when (and node
|
|
|
|
(not (html-utils:tag= :h1
|
2021-05-14 14:49:37 +02:00
|
|
|
node)))
|
2021-01-08 18:21:43 +01:00
|
|
|
(return-from subtitle-p nil))))
|
|
|
|
res))))
|
|
|
|
(when-let* ((data (slurp-gemini-url url))
|
2022-07-02 10:55:11 +02:00
|
|
|
(page (text-utils:to-s data))
|
2022-12-29 17:24:53 +01:00
|
|
|
(parsed (parse-gemini-file page :initialize-parser t))
|
2021-01-09 11:01:10 +01:00
|
|
|
(iri (iri:iri-parse url))
|
2021-08-16 14:22:47 +02:00
|
|
|
(title (gemini-first-h1 parsed)))
|
2021-01-09 11:01:10 +01:00
|
|
|
(let* ((maybe-subtitle-pos (html-utils:position-tag :h2 parsed))
|
2021-01-08 18:21:43 +01:00
|
|
|
(subtitle (when (subtitle-p parsed maybe-subtitle-pos)
|
|
|
|
(first (html-utils:children (elt parsed
|
|
|
|
maybe-subtitle-pos))))))
|
2021-01-09 11:01:10 +01:00
|
|
|
(when (not (db:gemini-find-subscription url))
|
|
|
|
(db:gemini-subscribe-url url title subtitle))
|
|
|
|
t))))
|
2021-01-08 18:21:43 +01:00
|
|
|
|
2023-06-18 14:48:40 +02:00
|
|
|
(defun refresh-subscription-low-level (url)
|
|
|
|
(when-let* ((data (slurp-gemini-url url))
|
|
|
|
(page (text-utils:to-s data))
|
|
|
|
(parsed (parse-gemini-file page :initialize-parser t))
|
|
|
|
(gemlog-iri (iri:iri-parse url)))
|
|
|
|
(let ((links (remove-if-not (lambda (a) (link-post-timestamp-p (name a)))
|
|
|
|
(sexp->links parsed
|
|
|
|
(uri:host gemlog-iri)
|
|
|
|
(uri:port gemlog-iri)
|
|
|
|
(uri:path gemlog-iri)
|
|
|
|
(uri:query gemlog-iri))))
|
|
|
|
(new-posts-count 0))
|
|
|
|
(loop for link in links do
|
|
|
|
(when (not (db:find-gemlog-entry (to-s (target link))))
|
|
|
|
(incf new-posts-count)
|
|
|
|
(let ((date (link-post-timestamp (name link))))
|
|
|
|
(db:add-gemlog-entries (to-s gemlog-iri)
|
|
|
|
(target link)
|
|
|
|
(link-post-title (name link))
|
|
|
|
date
|
|
|
|
nil))))
|
|
|
|
new-posts-count)))
|
|
|
|
|
2021-01-08 18:21:43 +01:00
|
|
|
(defun refresh (url)
|
|
|
|
"Refresh gemlog entries that can be found at 'url'. The gemlog must
|
|
|
|
be subscribed before (see: 'gemini-subscription:subcribe'"
|
2021-06-18 17:01:11 +02:00
|
|
|
(handler-case
|
2023-06-18 14:48:40 +02:00
|
|
|
(refresh-subscription-low-level url)
|
2021-06-18 17:01:11 +02:00
|
|
|
(gemini-client:gemini-tofu-error (e)
|
2021-06-18 17:48:56 +02:00
|
|
|
(ui:ask-input-on-tofu-error e (lambda () (refresh url))))))
|