-
Notifications
You must be signed in to change notification settings - Fork 0
/
hackernews.el
131 lines (105 loc) · 3.82 KB
/
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
;;; hackernews.el --- Hacker News Client for Emacs
;; Copyright (C) 2012 Lincoln de Sousa <lincoln@comum.org>
;; Author: Lincoln de Sousa <lincoln@comum.org>
;; Keywords: hackernews
;; Version: 0.1
;; 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:
;;
;; This is a very simple extention to search for files and for contents
;; inside files.
;;
;; Enjoy!
;;; Code:
(require 'json)
(require 'url)
;; "http://apihackernews.herokuapp.com/"
(defvar hackernews-url "http://api.ihackernews.com/page"
"The url to grab the list of news")
(defvar hackernews-map (make-sparse-keymap)
"The keymap to use with hackernews")
(if hackernews-map
(define-key hackernews-map (kbd "g") 'hackernews)
(define-key hackernews-map (kbd "q") 'bury-buffer))
;;; Interactive functions
;;;###autoload
(defun hackernews ()
"The entry point of our client"
(interactive)
(condition-case ex
(hackernews-format-results
(hackernews-parse
(hackernews-retrieve hackernews-url)))
('error
(message (format "Bad news, bro: %s" (car (cdr ex)))))))
;;; UI Functions
(defun hackernews-create-link-in-buffer (title url)
"Insert clickable string inside a buffer"
(lexical-let ((title title)
(url url)
(map (make-sparse-keymap)))
(define-key map (kbd "<RET>")
#'(lambda (e) (interactive "p") (browse-url url)))
(define-key map (kbd "<down-mouse-1>")
#'(lambda (e) (interactive "p") (browse-url url)))
(insert
(propertize
title
'face '(:foreground "green")
'keymap map
'mouse-face 'highlight))))
(defun hackernews-space-fill (string n)
"Makes sure that string is at least n characters long, and
if it isn't, it adds SPACE-characters to the end"
(while (< (length string) n)
(setf string (concat string " ")))
(identity string))
(defun hackernews-render-post (post)
"Render a single post to the current buffer
Add the post title as a link, and print the points and number of
comments."
(princ (hackernews-space-fill
(format "[%s]" (cdr (assoc 'points post))) 6))
(hackernews-create-link-in-buffer
(cdr (assoc 'title post))
(cdr (assoc 'url post)))
(insert
(propertize
(format " (%d comments)" (cdr (assoc 'commentCount post)))
'face '(:foreground "gray")))
(princ "\n"))
(defun hackernews-format-results (results)
"Create the buffer to render all the info"
(with-output-to-temp-buffer "*hackernews*"
(switch-to-buffer "*hackernews*")
(setq font-lock-mode nil)
(use-local-map hackernews-map)
(princ "Your hacker News Emacs client\n\n")
(mapcar #'hackernews-render-post
(cdr (assoc 'items results)))))
;;; Retrieving and parsing
(defun hackernews-retrieve (url)
(let ((buffer (url-retrieve-synchronously url))
(json nil))
(save-excursion
(set-buffer buffer)
(goto-char (point-min))
(when (not (string-match "200 OK" (buffer-string)))
(error "Problem connecting to the server"))
(re-search-forward "^$" nil 'move)
(setq json (buffer-substring-no-properties (point) (point-max)))
(kill-buffer (current-buffer)))
json))
(defun hackernews-parse (contents)
(json-read-from-string contents))
(provide 'hackernews)
;;; hackernews.el ends here