~ubuntu-branches/ubuntu/utopic/electrum/utopic-proposed

« back to all changes in this revision

Viewing changes to plugins/exchange_rate.py

  • Committer: Package Import Robot
  • Author(s): Tristan Seligmann
  • Date: 2013-12-11 11:52:51 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131211115251-4r3dhaxvmqlg2ilf
Tags: 1.9.5-1
* New upstream release (closes: #730353).
  - Contacts bugfix included in 1.8.1 (closes: #727232).
* Acknowledge NMU.
* Update watch file.
* Add myself to Uploaders.
* Update mk18n.py patch and ship new translations file.
* Bump dependency on python-ecdsa for secp256k1.
* Remove deprecated CDBS dependency management.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from PyQt4.QtGui import *
 
2
from PyQt4.QtCore import *
 
3
 
 
4
import decimal
 
5
import httplib
 
6
import json
 
7
import threading
 
8
import re
 
9
from decimal import Decimal
 
10
from electrum.plugins import BasePlugin
 
11
from electrum.i18n import _
 
12
from electrum_gui.qt.util import *
 
13
 
 
14
 
 
15
class Exchanger(threading.Thread):
 
16
 
 
17
    def __init__(self, parent):
 
18
        threading.Thread.__init__(self)
 
19
        self.daemon = True
 
20
        self.parent = parent
 
21
        self.quote_currencies = None
 
22
        self.lock = threading.Lock()
 
23
 
 
24
    def exchange(self, btc_amount, quote_currency):
 
25
        with self.lock:
 
26
            if self.quote_currencies is None:
 
27
                return None
 
28
            quote_currencies = self.quote_currencies.copy()
 
29
        if quote_currency not in quote_currencies:
 
30
            return None
 
31
        return btc_amount * quote_currencies[quote_currency]
 
32
 
 
33
    def run(self):
 
34
        try:
 
35
            connection = httplib.HTTPConnection('blockchain.info')
 
36
            connection.request("GET", "/ticker")
 
37
        except Exception:
 
38
            return
 
39
        response = connection.getresponse()
 
40
        if response.reason == httplib.responses[httplib.NOT_FOUND]:
 
41
            return
 
42
        try:
 
43
            response = json.loads(response.read())
 
44
        except Exception:
 
45
            return
 
46
        quote_currencies = {}
 
47
        try:
 
48
            for r in response:
 
49
                quote_currencies[r] = self._lookup_rate(response, r)
 
50
            with self.lock:
 
51
                self.quote_currencies = quote_currencies
 
52
            self.parent.set_currencies(quote_currencies)
 
53
        except KeyError:
 
54
            pass
 
55
 
 
56
            
 
57
    def get_currencies(self):
 
58
        return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
 
59
 
 
60
    def _lookup_rate(self, response, quote_id):
 
61
        return decimal.Decimal(str(response[str(quote_id)]["15m"]))
 
62
 
 
63
 
 
64
class Plugin(BasePlugin):
 
65
 
 
66
    def fullname(self):
 
67
        return "Exchange rates"
 
68
 
 
69
    def description(self):
 
70
        return """exchange rates, retrieved from blockchain.info"""
 
71
 
 
72
 
 
73
    def __init__(self,a,b):
 
74
        BasePlugin.__init__(self,a,b)
 
75
        self.currencies = [self.config.get('currency', "EUR")]
 
76
 
 
77
    def init(self):
 
78
        self.win = self.gui.main_window
 
79
        self.win.connect(self.win, SIGNAL("refresh_currencies()"), self.win.update_status)
 
80
        # Do price discovery
 
81
        self.exchanger = Exchanger(self)
 
82
        self.exchanger.start()
 
83
        self.gui.exchanger = self.exchanger #
 
84
 
 
85
    def set_currencies(self, quote_currencies):
 
86
        self.currencies = sorted(quote_currencies.keys())
 
87
        self.win.emit(SIGNAL("refresh_currencies()"))
 
88
        self.win.emit(SIGNAL("refresh_currencies_combo()"))
 
89
 
 
90
    def set_quote_text(self, btc_balance, r):
 
91
        r[0] = self.create_quote_text(Decimal(btc_balance) / 100000000)
 
92
 
 
93
    def create_quote_text(self, btc_balance):
 
94
        quote_currency = self.config.get("currency", "EUR")
 
95
        quote_balance = self.exchanger.exchange(btc_balance, quote_currency)
 
96
        if quote_balance is None:
 
97
            quote_text = ""
 
98
        else:
 
99
            quote_text = "%.2f %s" % (quote_balance, quote_currency)
 
100
        return quote_text
 
101
 
 
102
 
 
103
    def requires_settings(self):
 
104
        return True
 
105
 
 
106
 
 
107
    def toggle(self):
 
108
        out = BasePlugin.toggle(self)
 
109
        self.win.update_status()
 
110
        return out
 
111
 
 
112
 
 
113
    def settings_widget(self, window):
 
114
        combo = QComboBox()
 
115
 
 
116
        def on_change(x):
 
117
            cur_request = str(self.currencies[x])
 
118
            if cur_request != self.config.get('currency', "EUR"):
 
119
                self.config.set_key('currency', cur_request, True)
 
120
                self.win.update_status()
 
121
 
 
122
        def set_currencies(combo):
 
123
            try:
 
124
                combo.clear()
 
125
            except Exception:
 
126
                return
 
127
            combo.addItems(self.currencies)
 
128
            try:
 
129
                index = self.currencies.index(self.config.get('currency', "EUR"))
 
130
            except Exception:
 
131
                index = 0
 
132
            combo.setCurrentIndex(index)
 
133
 
 
134
        set_currencies(combo)
 
135
        combo.currentIndexChanged.connect(on_change)
 
136
        combo.connect(window, SIGNAL('refresh_currencies_combo()'), lambda: set_currencies(combo))
 
137
        return combo
 
138
 
 
139
 
 
140