elfeed-nitter

Download Nitter RSS feeds in such a way that for each feed it will choose a random nitter instance.
git clone https://kaka.farm/~git/elfeed-nitter
Log | Files | Refs

elfeed-nitter.el (4628B)


      1 ;;; elfeed-nitter.el --- Download Nitter RSS feeds from random instances. -*- lexical-binding: t; -*-
      2 
      3 ;; SPDX-FileCopyrightText: 2023 Yuval Langer <yuval.langer@gmail.com>
      4 ;;
      5 ;; SPDX-License-Identifier: GPL-3.0-or-later
      6 
      7 ;; Copyright (C) 2023  Yuval Langer
      8 
      9 ;; Author: Yuval Langer <yuval.langer@gmail.com>
     10 ;; Version: 0.0.0
     11 ;; Keywords:
     12 ;; URL: https://kaka.farm/stagit/elfeed-nitter/log.html
     13 
     14 ;; This program is free software; you can redistribute it and/or modify
     15 ;; it under the terms of the GNU General Public License as published by
     16 ;; the Free Software Foundation, either version 3 of the License, or
     17 ;; (at your option) any later version.
     18 
     19 ;; This program is distributed in the hope that it will be useful,
     20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     22 ;; GNU General Public License for more details.
     23 
     24 ;; You should have received a copy of the GNU General Public License
     25 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     26 
     27 ;;; Commentary:
     28 
     29 ;; Usage:
     30 ;;
     31 ;; None yet.
     32 
     33 ;;; Code:
     34 
     35 (require 'cl-lib)
     36 (require 'dash)
     37 (require 'elfeed)
     38 
     39 (defcustom en-default-tags
     40   '(elfeed-nitter)
     41   "Tags added by default to the elfeed-nitter entries."
     42   :group 'elfeed-nitter
     43   :type '(repeat symbol))
     44 
     45 (defcustom en-hosts
     46   '("nitter.net")
     47   "Nitter instance domains used in making the elfeed-nitter entries."
     48   :group 'elfeed-nitter
     49   :type '(repeat string))
     50 
     51 (defcustom en-paths-and-tags
     52   '(("/emacs/rss" without_replies)
     53     ("/emacs/with_replies/rss" with_replies))
     54   "Wanted Nitter feeds."
     55   :group 'elfeed-nitter
     56   :type '(repeat (choice string
     57                          (cons string
     58                                (repeat symbol)))))
     59 
     60 (defcustom en-wiki-url
     61   "https://github.com/zedeus/nitter.wiki.git"
     62   "The URL of the wiki.  For git clone-ing purposes."
     63   :group 'elfeed-nitter
     64   :type 'string)
     65 
     66 (defvar en-path
     67   (locate-user-emacs-file "elfeed-nitter/"))
     68 
     69 (defvar en-wiki-path
     70   (concat en-path
     71           "nitter-wiki/"))
     72 
     73 (defvar en-instances-list-path
     74   (concat en-path
     75           "nitter-wiki/Instances.md"))
     76 
     77 (defun en-make-random-feeds-list ()
     78   (cl-loop
     79    with hosts-list =
     80    (en-make-instances-hosts-list)
     81 
     82    for entry in en-paths-and-tags
     83 
     84    for host =
     85    (nth (random (length hosts-list))
     86         hosts-list)
     87 
     88    for url =
     89    (concat "https://"
     90            host
     91            (car entry))
     92 
     93    for host-tag =
     94    (intern (string-replace "."
     95                            "-"
     96                            host))
     97 
     98    for entry-tags =
     99    (cdr entry)
    100 
    101    collect
    102    (append (list url
    103                  host-tag)
    104            en-default-tags
    105            entry-tags)))
    106 
    107 (defun en-make-feeds-alist ()
    108   (cl-loop
    109    for host in en-hosts
    110 
    111    collect
    112    (cl-loop
    113     for entry in en-paths-and-tags
    114 
    115     for url =
    116     (concat "https://"
    117             host
    118             "/"
    119             (car entry))
    120 
    121     for host-tag =
    122     (intern (string-replace "."
    123                             "-"
    124                             host))
    125 
    126     for rest-of-the-tags =
    127     (cdr entry)
    128 
    129     append
    130     (append (list url
    131                   host-tag)
    132             en-default-tags
    133             rest-of-the-tags))))
    134 
    135 (defun en-update-random-instances ()
    136   (let ((elfeed-feeds (en-make-random-feeds-list)))
    137     (elfeed-update)))
    138 
    139 (defun en--clone-nitter-wiki ()
    140   (ignore-error 'file-already-exists
    141     (make-directory en-path))
    142   (shell-command-to-string (format "git clone %s %s"
    143                                    en-wiki-url
    144                                    en-wiki-path)))
    145 
    146 (defun en--update-nitter-wiki ()
    147   (ignore-error 'file-already-exists
    148     (make-directory en-path))
    149   (defvar default-directory)
    150   (let ((default-directory en-wiki-path))
    151     (shell-command-to-string "git pull --ff-only")))
    152 
    153 (defun en-make-instances-hosts-list ()
    154   (cl-loop
    155    with instances-file-contents =
    156    (with-temp-buffer
    157      (insert-file-contents en-instances-list-path)
    158      (buffer-string))
    159 
    160    with lines =
    161    (string-split instances-file-contents
    162                  "\n")
    163 
    164    for line in lines
    165 
    166    when
    167    (string-match "white_check_mark:\s*|\s*:white_check_mark"
    168                  line)
    169 
    170    unless
    171    (string-match "auth required"
    172                  line)
    173 
    174    collect
    175    (->> line
    176         (replace-regexp-in-string
    177          "|\s*\\[.*\\](\\(.*\\)).*white_check_mark:\s*|\s*:white_check_mark.*" "\\1")
    178         url-generic-parse-url
    179         url-host
    180         ;; XXX: Will this sanitise the URL thingimajig?
    181         url-hexify-string)))
    182 
    183 (provide 'elfeed-nitter)
    184 
    185 ;; Local Variables:
    186 ;; read-symbol-shorthands: (("en-" . "elfeed-nitter-"))
    187 ;; End:
    188 ;;; elfeed-nitter.el ends here