-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcoin-ticker.el
167 lines (130 loc) · 4.78 KB
/
coin-ticker.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
;;; coin-ticker.el --- Show a cryptocurrency price ticker
;; Copyright (C) 2017 Evan Klitzke
;; Author: Evan Klitzke <evan@eklitzke.org>
;; URL: https://github.com/eklitzke/coin-ticker-mode
;; Version: 0.1.0
;; Package-Requires: ((request "0.3.0") (emacs "25"))
;; Keywords: news
;;; Commentary:
;;
;; Provides a ticker of cryptocurrency prices (Bitcoin, Ethereum, etc.) using
;; the coinmarketcap.com API.
;;; License:
;;
;; 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/>.
;;; Code:
(require 'json)
(require 'request)
(defgroup coin-ticker nil
"coin-ticker extension"
:group 'comms
:prefix "coin-ticker-")
;;; Constants
(defconst coin-ticker-version "0.1.0")
(defconst coin-ticker-url "https://api.coinmarketcap.com/v1/ticker/")
;;; Customize variables
(defcustom coin-ticker-api-poll-interval 300
"Default interval to poll to the coinmarketcap api (in seconds)."
:type 'number
:group 'coin-ticker)
(defcustom coin-ticker-api-limit 10
"Number of cryptocurrencies to fetch price data for (0 for all)."
:type 'number
:group 'coin-ticker)
(defcustom coin-ticker-syms '("BTC" "ETH")
"Coins to show."
:group 'coin-ticker)
(defcustom coin-ticker-show-syms t
"If non-nil, symbols will be shown alongside prices."
:group 'coin-ticker)
(defcustom coin-ticker-price-convert "USD"
"Used to convert prices to some base unit (USD, EUR, BTC, etc)."
:group 'coin-ticker)
(defcustom coin-ticker-price-symbol "$"
"The symbol to show for the price."
:group 'coin-ticker)
;;; Internal variables
(defvar coin-ticker-timer nil
"Coin API poll timer.")
(defvar coin-ticker-mode-line ""
"Displayed on mode-line.")
;; users shouldn't directly modify coin-ticker-mode-line
(put 'coin-ticker-mode-line 'risky-local-variable t)
;;; Methods
(defun coin-ticker-start ()
"Start the ticker."
(unless coin-ticker-timer
(setq coin-ticker-timer
(run-at-time "0 sec"
coin-ticker-api-poll-interval
#'coin-ticker-fetch))
(coin-ticker-fetch)))
(defun coin-ticker-stop()
"Stop the ticker."
(when coin-ticker-timer
(cancel-timer coin-ticker-timer)
(setq coin-ticker-timer nil)
(if (boundp 'mode-line-modes)
(delete '(t coin-ticker-mode-line) mode-line-modes))))
(defun coin-ticker-price-fmt (sym price)
"Format SYM so that its PRICE is shown."
(if coin-ticker-show-syms
(format "%s %s%s" sym coin-ticker-price-symbol price)
(format "%s%s" coin-ticker-price-symbol price)))
(defun coin-ticker-modeline-update (prices)
"Update the modeline with a PRICES dictionary."
(setq coin-ticker-mode-line
(format "[%s]"
(string-join
(cl-loop for sym in coin-ticker-syms
collect
(coin-ticker-price-fmt sym (gethash sym prices))) " "))))
(defun coin-ticker-build-params ()
"Build the HTTP params."
(let ((params '()))
(if (/= coin-ticker-api-limit 0)
(add-to-list 'params `("limit" . ,coin-ticker-api-limit)))
(if (> (length coin-ticker-price-convert) 0)
(add-to-list 'params `("convert" . ,coin-ticker-price-convert)))
params))
(defun coin-ticker-price-key()
"Get the JSON key to use when accessing a price."
(if (= (length coin-ticker-price-convert) 0)
'price_usd
(intern (concat "price_" (downcase coin-ticker-price-convert)))))
(defun coin-ticker-fetch ()
"Fetch new price data."
(request
coin-ticker-url
:params (coin-ticker-build-params)
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(let ((prices (make-hash-table :test 'equal)))
(cl-loop for tick across data
do (let ((sym (alist-get 'symbol tick))
(price (alist-get (coin-ticker-price-key) tick)))
(puthash sym price prices)))
(coin-ticker-modeline-update prices))))))
;;; Mode
(define-minor-mode coin-ticker-mode
"Minor mode to show cryptocurrency prices."
:init-value nil
:global t
:lighter coin-ticker-mode-line
(if coin-ticker-mode
(coin-ticker-start)
(coin-ticker-stop)))
(provide 'coin-ticker)
;;; coin-ticker.el ends here