summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2024-12-26 23:47:28 +1100
committerYuchen Pei <id@ypei.org>2024-12-26 23:47:28 +1100
commite0aa1eb8b5dd2696f92f90348cb9e8aedd798008 (patch)
treeabe00bfe839dd267db4dce79ef2307e678c8d9cc
parenta1cdbd95a1e021f2a4530ff7bf326ebc1b58870e (diff)
Better expansion of t.co links
-rw-r--r--exitter.el41
1 files changed, 25 insertions, 16 deletions
diff --git a/exitter.el b/exitter.el
index b38c273..e40fc50 100644
--- a/exitter.el
+++ b/exitter.el
@@ -507,6 +507,7 @@ ONEPLUS A3010 Build/PKQ1.181203.001)")
((id_str . ,.id_str)
(created_at . ,.created_at)
(full_text . ,.full_text)
+ (urls . ,(vconcat .entities.urls .entities.media))
(reply_count . ,.reply_count)
(retweet_count . ,.retweet_count)
(quote_count . ,.quote_count)
@@ -550,7 +551,7 @@ Including ancestors and descendants, if any."
"\n")))
(defun exitter-save-posts (filtered-details id)
- ;; (pp filtered-details)
+ (pp filtered-details)
(exitter-save-text-and-switch-to-buffer
(exitter-format-posts filtered-details)
(format "~/Downloads/%s.org" id)))
@@ -563,7 +564,7 @@ Including ancestors and descendants, if any."
.author.name
.author.screen_name
(exitter--relative-time-description .created_at)
- (exitter-replace-t-co-links .full_text)
+ (exitter-replace-t-co-links .full_text .urls)
(if .quoted
(format "\n\n----\n%s----"
(replace-regexp-in-string
@@ -699,30 +700,38 @@ TIME-STAMP is assumed to be in the past."
(base64-encode-string
(alist-get 'bs (bindat-unpack '((bs str 32)) (vconcat xs)))))))
-(defun exitter-replace-t-co-links (text)
+(defun exitter-replace-t-co-links (text urls)
(with-temp-buffer
(insert text)
(goto-char (point-min))
(while (re-search-forward "https://t.co" nil t)
(pcase-let* ((`(,beg . ,end) (bounds-of-thing-at-point 'url))
(new-url (exitter-get-redirect-url
- (buffer-substring-no-properties beg end))))
+ (buffer-substring-no-properties beg end)
+ urls)))
(delete-region beg end)
(insert new-url)))
(buffer-string)))
-(defun exitter-get-redirect-url (url)
- "Get redirect link of URL.
-
-Sends a HEAD request."
- (let* ((url-request-method "HEAD")
- (url-max-redirections 0)
- (buffer (url-retrieve-synchronously url))
- (inhibit-message t))
- (with-current-buffer buffer
- (goto-char (point-min))
- (when (re-search-forward "^Location: \\(.*\\)$" nil t)
- (match-string 1)))))
+(defun exitter-get-redirect-url (url urls)
+ (let-alist (seq-find
+ (lambda (info) (equal url (alist-get 'url info)))
+ urls)
+ (or .media_url_https .expanded_url url)))
+
+;;; Probably not needed...
+;; (defun exitter-get-redirect-url (url)
+;; "Get redirect link of URL.
+
+;; Sends a HEAD request."
+;; (let* ((url-request-method "HEAD")
+;; (url-max-redirections 0)
+;; (buffer (url-retrieve-synchronously url))
+;; (inhibit-message t))
+;; (with-current-buffer buffer
+;; (goto-char (point-min))
+;; (when (re-search-forward "^Location: \\(.*\\)$" nil t)
+;; (match-string 1)))))
(require 'sha1)