IBM Support

Fetching Daily Exchange Rates from xml feed provided by Turkish National Bank

Technical Blog Post


Abstract

Fetching Daily Exchange Rates from xml feed provided by Turkish National Bank

Body

I will present you a simple Jython script to fetch daily exchange rates using the xml feed provided by Turkish National Bank via the following url:

http://www.tcmb.gov.tr/kurlar/today.xml

image

 

 

 

 

 

 

 

Figure 1 - Caption of Web Page for Exchange Rates

Let me show the partial page source which will present the xml structure clearly:

image

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 2 - Caption of Source xml for Exchange Rates

Now, I want to show a screenshot for the Exchange Rates application of Maximo which also shows the result of our development.

image

 

 

 

 

 

 

 

 

 

 

 

Figure 3 - Exchange Rates Application of Maximo

In Figure 3, you can see exchange rates from EURO to Turkish Lira (TL or TRY) and US Dollar to Turkish Lira for today. One can compare these rates to the values provided by national bank and figure out that these values are Banknote Selling rates.

 

DEVELOPMENT:

First, we add a system property for defining the URL provided from Turkish National Bank.

image

 

 

 

 

 

 

 

 

Figure 4 - System Properties Application showing the URL Property

Then, we create an Automation Script with action type of launch point. It works on ORGANIZATION object. The script loads the xml document from the url defined in property and then stores currency codes and banknote selling rates of each currency element as a pair of a python dictionary. Lastly, we select the currencies we want to save from the dictionary and add them to Maximo EXCHANGE table.

 

  # Action to Fetch Exchange Rates and Save to Maximo   from java.net import URL  from java.text import DateFormat, SimpleDateFormat  from java.util import Date, Properties  from javax.xml.parsers import DocumentBuilder, DocumentBuilderFactory  from org.w3c.dom import Document, Element, Node, NodeList  from psdi.mbo import Mbo, MboRemote, MboSet, MboSetRemote  from psdi.app.currency import Exchange, ExchangeRemote, ExchangeSet, ExchangeSetRemote  from psdi.server import MXServer  from sys import *    # function to load the xml document from URL  def loadXMLDocument():      properties = MXServer.getMXServer().getConfig()      property = properties.getProperty("tcmb.exchange.url")      url = URL(property)      factory = DocumentBuilderFactory.newInstance()      factory.setNamespaceAware(True)      return factory.newDocumentBuilder().parse(url.openStream())    # function to get exchange rates for all currencies provided  def getCurrencies(doc):      currencies = {}      nodes = doc.getElementsByTagName("Currency")      cnt = nodes.getLength()      print str(cnt)      for i in range(cnt):          element = nodes.item(i)          code = element.getAttribute("CurrencyCode")          subelement = element.getElementsByTagName("BanknoteSelling").item(0)          rate = float(subelement.getTextContent()) if subelement.getFirstChild() != None else 0.0          currencies[code] = rate      return currencies    # function to save a currency and its exhange rate for Turkish Lira to Maximo  def setCurrency(code, rate):      todayBegin = MXServer.getMXServer().getDate()      todayBegin.setHours(0)      todayBegin.setMinutes(0)      todayBegin.setSeconds(0)      todayEnd = MXServer.getMXServer().getDate()      todayEnd.setHours(23)      todayEnd.setMinutes(59)      todayEnd.setSeconds(59)        exchSet = mbo.getMboSet("EXCHANGE")      exchange = exchSet.add()      exchange.setValue("ORGID", "TEST", 11L)      exchange.setValue("CURRENCYCODE", code, 11L)      exchange.setValue("CURRENCYCODETO", "TL", 11L)      exchange.setValue("EXCHANGERATE", rate, 11L)      exchange.setValue("ACTIVEDATE", todayBegin, 11L)      exchange.setValue("EXPIREDATE", todayEnd, 11L)      exchSet.save(11L)    # Main - save exchange rates for USD and EUR to Turkish Lira   doc = loadXMLDocument()  curr = getCurrencies(doc)  setCurrency("USD", curr['USD'])  setCurrency("EUR", curr['EUR'])  

Code 1 - Jython Script for Action

Finally, the action for the code piece above is substituted in an escalation which runs automatically on daily basis in order to fetch the exchange rates and save them to Maximo database. Do not forget to tick "Repeat?" check box for the escalation point.

image

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 5 - Escalation for Exchange Rates Action in Maximo

All in all, this implementation is a practice of parsing an xml document provided via a url. One can alter the code piece due to their specific needs for other urls and currencies.

[{"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SSLKT6","label":"IBM Maximo Asset Management"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"","Edition":"","Line of Business":{"code":"LOB59","label":"Sustainability Software"}}]

UID

ibm11129851