Created
September 1, 2018 12:27
-
-
Save TakesxiSximada/d531ac579fd46ac74a7fda545303df42 to your computer and use it in GitHub Desktop.
エゴサ用major-mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'dom) | |
(require 's) | |
(require 'url) | |
(defconst mast-search-url "DUMMY") | |
(defconst mast-search-user-agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36") | |
(defvar mast-search-buffer-name "*mast-search*") | |
(defvar mast-search-buffer nil) | |
(defvar mast-search-posts nil) | |
(defun mast-search-request-get () | |
(let ((url-request-method "GET") | |
(url-request-extra-headers `(("User-Agent" . ,mast-search-user-agent)))) | |
(with-current-buffer (url-retrieve-synchronously mast-search-url) | |
(buffer-string)))) | |
(defun mast-search-http-body (str) | |
(car (last (s-split-up-to "\n\n" str 1)))) | |
(defun mast-search-fetch () | |
(interactive) | |
(setq mast-search-buffer (get-buffer-create mast-search-buffer-name)) | |
(with-current-buffer mast-search-buffer | |
(insert (mast-search-http-body | |
(decode-coding-string (mast-search-request-get) 'utf-8))))) | |
(defun mast-search-parse-toot-dom (post-dom) | |
(let* ((mst-ref (car (dom-by-class post-dom "mst_ref"))) | |
(mst-time (car (dom-by-class post-dom "mst_time"))) | |
(mst-user (car (dom-by-class post-dom "mst_user"))) | |
(mst-avater (car (dom-by-class post-dom "avatar"))) | |
(mst-content (car (dom-by-class post-dom "mst_content"))) | |
(content-url (cdr (assoc 'href (dom-attributes (car (dom-non-text-children mst-content)))))) | |
) | |
`((account-name . ,(nth 3 (split-string content-url "/"))) | |
(account-display-name . ,(car (split-string (s-trim (dom-texts mst-user)) " "))) | |
(account-icon-url . ,(cdr (assoc 'src (dom-attributes (car (dom-non-text-children mst-avater)))))) | |
(content-url . ,content-url) | |
(content-id . ,(car (last (split-string content-url "/")))) | |
(content-text . ,(s-trim (dom-texts mst-content))) | |
))) | |
(define-derived-mode mast-search-mode tabulated-list-mode "Process Menu" | |
"Major mode for mastdon (ego)search by Emacs." | |
(setq tabulated-list-format [("Content text" 80 t) | |
("Display name" 20 t) | |
("Account name" 15 t)]) | |
(make-local-variable 'process-menu-query-only) | |
(setq tabulated-list-sort-key (cons "Account name" nil)) | |
(add-hook 'tabulated-list-revert-hook 'mast-search--refresh nil t) | |
(tabulated-list-init-header)) | |
(defun mast-search--refresh () | |
(mast-search-fetch) | |
(with-current-buffer mast-search-buffer | |
(setq mast-search-dom (libxml-parse-html-region (point-min) (point-max)))) | |
(setq mast-search-posts (dom-by-class mast-search-dom "post")) | |
(setq tabulated-list-entries | |
(mapcar (lambda (entry) | |
`(,(cdr (assoc 'content-id entry)) | |
[,(cdr (assoc 'content-text entry)) | |
,(cdr (assoc 'account-display-name entry)) | |
,(cdr (assoc 'account-name entry))])) | |
(mapcar 'mast-search-parse-toot-dom mast-search-posts)))) | |
(defun mast-search (&optional query-only buffer) | |
(interactive) | |
(unless (bufferp buffer) | |
(setq buffer (get-buffer-create "*Mast Search*"))) | |
(with-current-buffer buffer | |
(mast-search-mode) | |
(mast-search--refresh) | |
(tabulated-list-print)) | |
(display-buffer buffer) | |
nil) | |
(provide 'mast-search) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment