diff options
author | Yuchen Pei <id@ypei.org> | 2024-12-26 23:19:46 +1100 |
---|---|---|
committer | Yuchen Pei <id@ypei.org> | 2024-12-26 23:19:46 +1100 |
commit | a1cdbd95a1e021f2a4530ff7bf326ebc1b58870e (patch) | |
tree | 1af1818b65b6ff4dd7de9805cbece1f32930a87a /exitter.el | |
parent | c78c62bd4a0eca87ae711d1efbd3f0cca46faf70 (diff) |
Expand t.co links in place.
Using code adapted from traclicker.el
Diffstat (limited to 'exitter.el')
-rw-r--r-- | exitter.el | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -563,7 +563,7 @@ Including ancestors and descendants, if any." .author.name .author.screen_name (exitter--relative-time-description .created_at) - .full_text + (exitter-replace-t-co-links .full_text) (if .quoted (format "\n\n----\n%s----" (replace-regexp-in-string @@ -699,6 +699,30 @@ 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) + (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)))) + (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))))) (require 'sha1) |