Преглед на файлове

Expand t.co links in place.

Using code adapted from traclicker.el
Yuchen Pei преди 11 месеца
родител
ревизия
a1cdbd95a1
променени са 1 файла, в които са добавени 25 реда и са изтрити 1 реда
  1. 25 1
      exitter.el

+ 25 - 1
exitter.el

@@ -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)