-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpetrol_cijena.py
56 lines (47 loc) · 1.71 KB
/
petrol_cijena.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
import sys
import EUR_2_HRK
import urllib
from xml.dom import minidom
def auth():
'''The method to do HTTPBasicAuthentication'''
if len(sys.argv) < 3:
print "Usage: python " + sys.argv[0] + " country currencycode"
exit()
country = sys.argv[1]
currencycode = sys.argv[2]
_URL = "http://www.petrol.si/api/gas_prices.xml"
opener = urllib.FancyURLopener()
f = opener.open(_URL)
feed = f.read()
return feed, country, currencycode
def map_country_code(country_code):
if country_code == 'HR':
return 'Croatia'
elif country_code == 'SI':
return 'Slovenia'
return None
def getChildByAttribute(node, attribute, value):
for n in node.childNodes:
if n.attributes[attribute].value == value:
return n
return None
def parseXML(xml, country):
xmldoc = minidom.parseString(xml)
slovenia = getChildByAttribute(xmldoc.firstChild, 'label', country)
f95 = getChildByAttribute(slovenia, 'type', '95')
priceType = getChildByAttribute(f95, 'type', 'normal')
price = getChildByAttribute(priceType, 'type', 'price')
return price.firstChild.toxml()
def cijena(xml, country):
return float(parseXML(xml, country))
if __name__ == "__main__":
xml, country, currencycode = auth()
base_price = float(cijena(xml, map_country_code(country)))
if (country, currencycode) == ('HR', 'HRK'):
print "%.2f" % base_price
elif (country, currencycode) == ('HR', 'EUR'):
print "%.2f" % (base_price / EUR_2_HRK.getEUR_2_HRK())
elif (country, currencycode) == ('SI', 'HRK'):
print "%.2f" % (base_price * EUR_2_HRK.getEUR_2_HRK())
elif (country, currencycode) == ('SI', 'EUR'):
print "%.2f" % base_price