-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdashboard-hackernews.el
99 lines (80 loc) · 3.7 KB
/
dashboard-hackernews.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
;;; dashboard-hackernews.el --- Display Hacker News on dashboard -*- lexical-binding: t; -*-
;; Copyright (C) 2018 by Hayato KAJIYAMA
;; Author: Hayato KAJIYAMA <kaji1216@gmail.com>
;; URL: https://github.com/hyakt/emacs-dashboard-hackernews
;; Version: 0.0.2
;; Package-Requires: ((emacs "24") (dashboard "1.2.5") (request "0.3.0"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Display Hacker News on dashboard.
;;; Code:
(require 'request)
(require 'dashboard)
(require 'dashboard-widgets)
(add-to-list 'dashboard-item-generators '(hackernews . dashboard-hackernews-insert))
(add-to-list 'dashboard-items '(hackernews) t)
(defvar dashboard-hackernews-items ())
(defconst dashboard-hackernews-api-version "v0"
"Currently supported version of the Hacker News API.")
(defconst dashboard-hackernews-api-top-format
(format "https://hacker-news.firebaseio.com/%s/topstories.json"
dashboard-hackernews-api-version)
"Format of targeted Hacker News API URLs.")
(defconst dashboard-hackernews-site-item-format
(format "https://hacker-news.firebaseio.com/%s/item"
dashboard-hackernews-api-version)
"Format of Hacker News website item URLs.")
(defun dashboard-hackernews-get-ids (callback)
"Get hackernews ids, and execute CALLBACK function."
(request
dashboard-hackernews-api-top-format
:sync t
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
(defun dashboard-hackernews-get-item (id callback)
"Get hackernews article from ID, and execute CALLBACK function."
(request
(format "%s/%s.json" dashboard-hackernews-site-item-format id)
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(funcall callback data)))))
(defun dashboard-hackernews-insert-list (list-display-name list)
"Render LIST-DISPLAY-NAME and items of LIST."
(when (car list)
(dashboard-insert-heading list-display-name)
(mapc (lambda (el)
(insert "\n ")
(widget-create 'push-button
:action `(lambda (&rest ignore)
(browse-url ,(cdr (assoc 'url el))))
:mouse-face 'highlight
:button-face 'dashboard-items-face
:follow-link "\C-m"
:button-prefix ""
:button-suffix ""
:format "%[%t%]"
(format "[%3d] %s" (cdr (assoc 'score el)) (decode-coding-string (cdr (assoc 'title el)) 'utf-8))))
list)))
(defun dashboard-hackernews-insert (list-size)
"Add the list of LIST-SIZE items from hackernews."
(dashboard-hackernews-get-ids
(lambda (ids)
(dotimes (i list-size)
(dashboard-hackernews-get-item
(elt ids i) (lambda (item) (push item dashboard-hackernews-items))))))
(dashboard-hackernews-insert-list "Hackernews:"
(dashboard-subseq dashboard-hackernews-items list-size)))
(provide 'dashboard-hackernews)
;;; dashboard-hackernews.el ends here