forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrency.py
45 lines (39 loc) · 1.63 KB
/
Currency.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
# -*- coding: utf-8 -*-
"""Convert currencies using Google Finance.
Usage: exch <amount> <src currency> <dest currency>
Example: exch 5 usd eur"""
from albertv0 import *
import urllib
import re
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "Currency converter"
__version__ = "1.0"
__trigger__ = "exch "
__author__ = "Manuel Schneider"
__dependencies__ = []
iconPath = iconLookup('accessories-calculator')
if not iconPath:
iconPath = ":python_module"
def handleQuery(query):
if query.isTriggered:
fields = query.string.split()
item = Item(id=__prettyname__, icon=iconPath, completion=query.rawString)
if len(fields) == 3:
url = 'https://finance.google.com/finance/converter?a=%s&from=%s&to=%s' % tuple(fields)
with urllib.request.urlopen(url) as response:
html = response.read().decode("latin-1")
m = re.search('<div id=currency_converter_result>.*<span class=bld>(\d+\.\d+).*</span>', html)
if m:
result = m.group(1)
item.text = result
item.subtext = "Value of %s %s in %s" % tuple([x.upper() for x in fields])
item.addAction(ClipAction("Copy result to clipboard", result))
return item
else:
item.text = "Error: HTTP reply does not contain a result"
item.subtext = "Maybe Google Finance changed their website"
return item
else:
item.text = __prettyname__
item.subtext = "Enter a query in the form of <amount> <from> <to>"
return item