Ver código fonte

Better expansion of t.co links

Yuchen Pei 11 meses atrás
pai
commit
e0aa1eb8b5
1 arquivos alterados com 25 adições e 16 exclusões
  1. 25 16
      exitter.el

+ 25 - 16
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)