-
Notifications
You must be signed in to change notification settings - Fork 1
/
gnus-twit.el
296 lines (268 loc) · 11.9 KB
/
gnus-twit.el
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
;;; gnus-twit.el --- Read a Twitter thread in Gnus -*- lexical-binding: t -*-
;; Copyright (C) 2020 Lars Magne Ingebrigtsen
;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
;; Keywords: idiocy
;; gnus-twit 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 2, or (at your option)
;; any later version.
;; gnus-twit 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.
;;; Commentary:
;; Usage: M-x gnus-group-twitter RET https://twitter.com/FiveThirtyEight/status/1299178217983619073 RET
;; Then weep.
;; TODO: Ignore registry?
(require 'dom)
(require 'gnus-group)
(require 'seq)
(defgroup gnus-twit nil
"Read a Twitter thread in Gnus."
:group 'gnus)
(defcustom gnus-twit-nitter-instance "https://unofficialbird.com/"
"URL of the Nitter instance to use."
:group 'gnus-twit
:type '(radio (const :tag "Nitter.net" "https://nitter.net/")
(string :tag "URL to a Nitter instance")))
(defcustom gnus-twit-cache nil
"If non-nil, cache the results.
This is mostly for debugging -- if you cache the data, you won't
see any new responses in a thread."
:type 'boolean)
(defun gnus-group-twitter (url)
"Visit an ephemeral Twitter group based on URL."
(interactive "sTwitter URL: ")
(let ((tmpfile (make-temp-file "gnus-temp-group-")))
(unwind-protect
;; Add the debbugs address so that we can respond to reports easily.
(let* ((status (gnus-twit-status url))
(threads (gnus-twit-build url))
(group-name (concat "nndoc+ephemeral:twit#" status)))
(when (boundp 'gnus-global-groups)
(push group-name gnus-global-groups))
(gnus-twit-make-mbox status threads tmpfile)
(gnus-group-read-ephemeral-group
group-name
`(nndoc ,tmpfile
(nndoc-article-type mbox))))
(delete-file tmpfile))))
(defun gnus-twit-expand-relative (maybe-relative)
(when maybe-relative
(let ((url (url-generic-parse-url maybe-relative))
(base (url-generic-parse-url gnus-twit-nitter-instance)))
(if (url-host url)
maybe-relative
(setf (url-filename base)
(concat (string-trim-right (car (url-path-and-query base)) "/+")
"/"
(string-trim-left (url-filename url) "/+")))
(url-recreate-url base)))))
(defun gnus-twit-fix-url (url)
(replace-regexp-in-string "https?://.*?/"
(gnus-twit-expand-relative "/") url))
(defun gnus-twit-pp (url)
(setq url (gnus-twit-fix-url url))
(dom-pp
(with-current-buffer (url-retrieve-synchronously url)
(goto-char (point-min))
(prog1
(when (re-search-forward "\r?\n\r?\n" nil t)
(libxml-parse-html-region (point) (point-max)))
(kill-buffer (current-buffer))))
t))
(defun gnus-twit-retrieve (url)
(if gnus-twit-cache
(if (url-is-cached url)
(url-fetch-from-cache url)
(with-current-buffer (url-retrieve-synchronously url t)
(url-store-in-cache (current-buffer))
(current-buffer)))
(url-retrieve-synchronously url t)))
(defun gnus-twit-build (url)
(setq url (gnus-twit-fix-url url))
(let ((threads (make-hash-table :test #'equal)))
(with-current-buffer (gnus-twit-retrieve url)
(goto-char (point-min))
(prog1
(when (re-search-forward "\r?\n\r?\n" nil t)
(gnus-twit-check url
(libxml-parse-html-region (point) (point-max))
threads))
(kill-buffer (current-buffer))))))
(defun gnus-twit-check (url dom threads)
(let ((tweets (gnus-twit-tweets dom)))
(when tweets
(if (equal (gnus-twit-status url) (gnus-twit-status (car tweets)))
;; We have the data for the first tweet in the thread; start
;; building.
(gnus-twit-build-main dom threads)
(gnus-twit-build (gnus-twit-expand-relative (car tweets)))))))
(defun gnus-twit-build-main (dom threads)
(let* ((main (dom-by-class dom "main-tweet"))
(data (list :text `(div nil
,(dom-by-class main "tweet-content")
,(dom-by-class main "card-container")
,(dom-by-class main "attachments"))
:date (dom-attr
(dom-by-tag (dom-by-class main "tweet-date") 'a)
'title)
:from (dom-attr (dom-by-class main "^fullname$") 'title)
:user-name (dom-attr
(dom-by-class main "^username$") 'title)
:url (dom-attr
(dom-by-tag (dom-by-class main "tweet-date") 'a)
'href)
:avatar
(gnus-twit-expand-relative
(dom-attr
(dom-by-tag (dom-by-class main "avatar") 'img)
'src))
:replies nil))
(show-mores (dom-by-tag (dom-by-class dom "show-more") 'a))
(next-page
(when (string-match "Load more" (dom-text (last show-mores)))
(dom-attr (last show-mores) 'href)))
(is-continuation (string-match "Load newest"
(dom-text (car show-mores))))
;; "After tweets" are replies by the author to
;; split long text into consecutive tweets.
;; TODO: Do these also get truncated/paginated
;; at some point?
(after-tweets
;; Ignore after tweets on continuation pages, as they have been
;; processed already.
(unless is-continuation
(dom-by-class (dom-by-class dom "after-tweet") "tweet-link")))
(replies
(cl-loop with replies
for tweet in (append
after-tweets
;; "reply" divs also contain timelines, but
;; only the first link (reply itself) is
;; extracted below.
(dom-by-class dom (rx "reply" eow)))
for reply = (dom-attr
(car (dom-by-class tweet "tweet-link"))
'href)
when reply
do (push reply replies)
finally (return (nreverse replies))))
(status (gnus-twit-status (plist-get data :url)))
(data (gethash status threads data)))
;; Expand relative URLs.
(cl-loop
for (tag . attr) in '((a . href) (img . src))
do (dolist (elem (dom-by-tag (plist-get data :text) tag))
(dom-set-attribute
elem attr (gnus-twit-expand-relative (dom-attr elem attr)))))
(setf (gethash status threads) data)
(plist-put data :replies (append
(when is-continuation
(plist-get data :replies))
(mapcar #'gnus-twit-status replies)))
(when next-page
(push
(setq next-page
(concat
(car (url-path-and-query
(url-generic-parse-url (plist-get data :url))))
"?"
(cdr
(url-path-and-query
(url-generic-parse-url next-page)))))
replies))
(dolist (reply replies)
(message "Fetching %s" (gnus-twit-expand-relative reply))
(unless (and (gethash (gnus-twit-status reply) threads)
(not (string-equal reply next-page)))
(with-current-buffer (gnus-twit-retrieve
(gnus-twit-expand-relative reply))
(let ((buffer (current-buffer)))
(goto-char (point-min))
(when (re-search-forward "\r?\n\r?\n" nil t)
(gnus-twit-build-main
(libxml-parse-html-region (point) (point-max))
threads))
(kill-buffer buffer)))))
threads))
(defun gnus-twit-tweets (dom)
(cl-loop for tweet in (dom-by-class dom "timeline-item")
collect (or (dom-attr
(dom-by-tag (dom-by-class tweet "tweet-date") 'a)
'href)
(dom-attr (dom-by-class tweet "tweet-link")
'href))))
(defun gnus-twit-status (url)
(and (string-match "/status/\\([0-9]+\\)" url)
(match-string 1 url)))
(defun gnus-twit-make-mbox (status threads file)
(setq threads (copy-hash-table threads))
(with-temp-buffer
(gnus-twit-make-mbox-1 status threads nil)
(let ((coding-system-for-write 'binary))
(write-region (point-min) (point-max) file))))
(defun gnus-twit-shorten (string)
(if (> (length string) 63)
(concat (substring string 0 60) "...")
string))
(defun gnus-twit-make-mbox-1 (status threads parent)
(when-let ((data (gethash status threads)))
(remhash status threads)
(insert (format "From twit@localhost Wed Aug 12 21:34:56 2020\n"))
(insert (format "From: %s <%s@twitter.com>\n"
(mail-encode-encoded-word-string (plist-get data :from))
(replace-regexp-in-string "[@ ]+" ""
(plist-get data :user-name))))
(insert (format "Subject: %s\n"
(let ((rfc2047-encoding-type 'mime))
(rfc2047-encode-string
(gnus-twit-shorten
(string-trim
(replace-regexp-in-string
"[ \t\n]+" " "
(dom-texts (plist-get data :text)))))))))
(insert (format "Message-ID: <%s@twitter.com>\n" status))
(when parent
(insert (format "References: <%s@twitter.com>\n" parent)))
(insert (format "Date: %s\n" (gnus-twit-format-date
(plist-get data :date))))
(insert "Content-Transfer-Encoding: quoted-printable\n")
(insert "Content-Type: text/html; charset=utf-8\n\n")
(let ((start (point)))
(dom-print (plist-get data :text))
(insert "\n\n")
(insert (format "<p><img src=\"%s\">\n"
(plist-get data :avatar)))
(insert (format "<p><a href=\"https://twitter.com%s\">[Link]</a>\n\n"
(plist-get data :url)))
(insert "\n\n\n")
(encode-coding-region start (point) 'utf-8)
;; There should not be any multibyte characters in the buffer at this
;; point. As `encode-coding-region' converts 8-bit bytes to their
;; multibyte representation in multibyte buffers, switch to unibyte mode
;; temporarily, to make `quoted-printable-encode-region' work (?).
;; For example `’' should be encoded as `=E2=80=99', not
;; `=3FFFE2=3FFF80=3FFF99'. (This has been fixed in Emacs 28, and
;; is not necessary there.)
(set-buffer-multibyte nil)
(quoted-printable-encode-region start (point))
(set-buffer-multibyte 'to))
(dolist (reply (plist-get data :replies))
(gnus-twit-make-mbox-1 reply threads status))))
(defun gnus-twit-format-date (string)
(if (string-match
(rx (group (+ num)) "/" (group (+ num)) "/" (group (+ num)) ", "
(group (+ num)) ":" (group (+ num)) ":" (group (+ num)))
string)
(message-make-date
(encode-time
(make-decoded-time :day (string-to-number (match-string 1 string))
:month (string-to-number (match-string 2 string))
:year (string-to-number (match-string 3 string))
:hour (string-to-number (match-string 4 string))
:minute (string-to-number (match-string 5 string))
:second (string-to-number (match-string 6 string)))))
string))
(provide 'gnus-twit)
;;; gnus-twit.el ends here