-
Notifications
You must be signed in to change notification settings - Fork 0
/
koinex-ethereum-price.py
83 lines (68 loc) · 2.51 KB
/
koinex-ethereum-price.py
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
# -*- coding: utf-8 -*-
import signal
import requests
import gi
import os
import time
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk as gtk
from gi.repository import AppIndicator3 as appindicator
from gi.repository import GObject
from threading import Thread
APPINDICATOR_ID = 'koinex_indicator'
class Indicator():
def __init__(self):
# Indicator logic
iconpath = os.getcwd() + '/ethlogo.svg'
self.indicator = appindicator.Indicator.new(APPINDICATOR_ID, iconpath, appindicator.IndicatorCategory.APPLICATION_STATUS)
self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
self.indicator.set_menu(self.build_menu())
self.indicator.set_label('₹ 0 | $ 0', APPINDICATOR_ID)
# Thread to update the price on the label
self.update = Thread(target=self.get_current_price_koinex)
# self.update.setDaemon(True)
self.update.start()
def build_menu(self):
self.menu = gtk.Menu()
menu_item_quit = gtk.MenuItem('Quit')
menu_item_quit.connect('activate', quit)
self.menu.append(menu_item_quit)
self.menu.show_all()
return self.menu
def get_current_price_koinex(self):
while True:
# API requests to Koinex
try:
reply_from_koinex = requests.get('https://koinex.in/api/ticker')
price_INR = ('₹ ' + reply_from_koinex.json().get("prices").get('inr').get('ETH'))
price_USD = self.get_current_price_coinbase()
price = price_INR + ' | ' + price_USD
GObject.idle_add(
self.indicator.set_label,
price, APPINDICATOR_ID,
priority=GObject.PRIORITY_DEFAULT
)
except:
pass
time.sleep(60)
def get_current_price_coinbase(self):
# API requests to Coinbase
try:
reply_from_coinbase = requests.get('\
https://api.coinbase.com/v2/prices/ETH-USD/spot')
price_USD = ('$' + reply_from_coinbase.json().get("data").get("amount"))
return price_USD
except:
pass
# # Check for successfull reply
# except:
# print('Cannot connect to Coinbase Ticker API')
def quit(source):
gtk.main_quit()
Indicator()
# To start the indicator
GObject.threads_init()
# Ctrl+c exit
signal.signal(signal.SIGINT, signal.SIG_DFL)
gtk.main()