# -*- coding: UTF-8 -*-
# pylint: disable=C0111,R0903

"""Displays currency exchange rates. Currently, displays currency between GBP and USD/EUR only.

Requires the following python packages:
    * requests

Parameters:
    * currency.interval: Interval in minutes between updates, default is 1.
"""

import bumblebee.input
import bumblebee.output
import bumblebee.engine
import json
import time
try:
    import requests
    from requests.exceptions import RequestException
except ImportError:
    pass

class Module(bumblebee.engine.Module):
    def __init__(self, engine, config):
        super(Module, self).__init__(engine, config,
            bumblebee.output.Widget(full_text=self.price)
        )
        self._price = "-"
        self._interval = int(self.parameter("interval", "1"))
        self._nextcheck = 0
        self._valid = False

    def price(self, widget):
        if not self._valid:
            return u"?"
        return self._price

    def update(self, widgets):
        timestamp = int(time.time())
        if self._nextcheck < int(time.time()):
            try:
                self._nextcheck = int(time.time()) + self._interval*60
                price_url = "http://api.fixer.io/latest?symbols=USD,EUR&base=GBP" 
                try:
                    price_json = json.loads( requests.get(price_url).text )
                    gbpeur = str(price_json['rates']['EUR'])
                    gbpusd = str(price_json['rates']['USD'])
                except ValueError:
                    gbpeur = "-"
                    gbpusd = "-"

                self._price = u"£/€ " + gbpeur + u" | £/$ " + gbpusd
                self._valid = True
            except RequestException:
                self._price = u"£/€ - | £/$ -"
                self._valid = True

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4


@@ -289,16 +289,284 @@
 t is 1.%0A
+    * currency.source: Source currency (defaults to %22GBP%22)%0A    * currency.destination: Comma-separated list of destination currencies (defaults to %22USD,EUR%22)%0A%0ANote: source and destination names right now must correspond to the names used by the API of http://fixer.io%0A
 %22%22%22%0A%0Aimp
@@ -765,16 +765,72 @@
   pass%0A%0A
+SYMBOL = %7B%0A    %22GBP%22: u%22%C2%A3%22, %22EUR%22: u%22%E2%82%AC%22, %22USD%22: u%22$%22%0A%7D%0A%0A
 class Mo
@@ -1039,19 +1039,17 @@
 lf._
-price = %22-%22
+data = %7B%7D
 %0A   
@@ -1105,11 +1105,9 @@
 l%22, 
-%221%22
+1
 ))%0A 
@@ -1123,49 +1123,139 @@
 lf._
-nextcheck = 0%0A        self._valid = False
+base = self.parameter(%22source%22, %22GBP%22)%0A        self._symbols = self.parameter(%22destination%22, %22USD,EUR%22)%0A        self._nextcheck = 0
 %0A%0A  
@@ -1296,23 +1296,24 @@
  if 
-not 
 self._
-valid
+data == %7B%7D
 :%0A  
@@ -1333,39 +1333,288 @@
 urn 
-u%22?%22%0A        return self._price
+%22?%22%0A%0A        rates = %5B%5D%0A        for sym in self._data%5B%22rates%22%5D:%0A            rates.append(u%22%7B%7D%7B%7D%22.format(self._data%5B%22rates%22%5D%5Bsym%5D, SYMBOL%5Bsym%5D if sym in SYMBOL else sym))%0A%0A        return u%22%7B%7D: %7B%7D%22.format(SYMBOL%5Bself._base%5D if self._base in SYMBOL else self._base, u%22/%22.join(rates))
 %0A%0A  
@@ -1734,33 +1734,40 @@
             
-try:%0A    
+self._data = %7B%7D%0A
             
@@ -1833,26 +1833,16 @@
         
-    
-price_
 url = %22h
@@ -1879,31 +1879,54 @@
 ols=
-USD,EUR&base=GBP%22 %0A    
+%7B%7D&base=%7B%7D%22.format(self._symbols, self._base)%0A
     
@@ -1958,22 +1958,18 @@
     
-    price_json
+self._data
  = j
@@ -1978,17 +1978,16 @@
 n.loads(
- 
 requests
@@ -1991,22 +1991,16 @@
 sts.get(
-price_
 url).tex
@@ -2000,17 +2000,16 @@
 rl).text
- 
 )%0A      
@@ -2018,442 +2018,46 @@
     
-        gbpeur = str(price_json%5B'rates'%5D%5B'EUR'%5D)%0A                    gbpusd = str(price_json%5B'rates'%5D%5B'USD'%5D)%0A                except ValueError:%0A                    gbpeur = %22-%22%0A                    gbpusd = %22-%22%0A%0A                self._price = u%22%C2%A3/%E2%82%AC %22 + gbpeur + u%22 %7C %C2%A3/$ %22 + gbpusd%0A                self._valid = True%0A            except RequestException:%0A                self._price = u%22%C2%A3/%E2%82%AC - %7C %C2%A3/$ -%22%0A                self._valid = True
+except Exception:%0A                pass
 %0A%0A# 
@@ -2108,9 +2108,8 @@
 bstop=4%0A
-%0A

Enable base and symbol configuration