s4ros icon

Forex.dat ultimate fix

s4ros | PRO | 10/18/16 09:35:50 AM UTC | 0 ⭐ | 470 👁️ | Never ⏰ | []
Python |

3.05 KB

|

None

|

0 👍

/

0 👎

#!/usr/bin/env python
 
import json
import sys
import os
try:
  import xmltodict
except ImportError:
  raise ImportError('Please try to install module: sudo pip install xmltodict')
 
# //////////////////////////////////////////////////////////
xmlpath = '/opt/ang/var/foreximport'
# xmlpath = './'
forexdat = '/opt/ang/mnt/rtb/forex.dat'
# //////////////////////////////////////////////////////////
 
# //////////////////////////////////////////////////////////
def get_xml_files_list(dirpath):
  """
  Get the sorted list of XML files present in <dirpath>
  """
  if not os.path.isdir(dirpath):
    print "%s:ERROR:%s path doesn't exist!" % (sys.argv[0], dirpath)
    sys.exit(1)
  for root, dirs, files in os.walk(dirpath, topdown=True):
    filelist = []
    for f in files:
      if ".xml" in f:
        filepath = os.path.join(root, f)
        filelist.append(filepath)
    return sorted(filelist)
# //////////////////////////////////////////////////////////
 
# //////////////////////////////////////////////////////////
def get_currency():
  latest = get_xml_files_list(xmlpath)[-1]
  fp = open(latest, 'r')
  xp = xmltodict.parse(fp.read())
  currency = xp['currency_market']['@counter_currency']
  return currency
# //////////////////////////////////////////////////////////
 
# //////////////////////////////////////////////////////////
def save_forex_file(content):
  if os.path.exists(forexdat):
    with open(forexdat, 'w') as f:
      f.write(content)
  else:
    print "%s file does not exist!" % forexdat
    sys.exit(1)
# //////////////////////////////////////////////////////////
 
 
#with open() as f:
#  xmldict = xmltodict.parse(f.read())
 
if __name__ == "__main__":
  # get the reference currency from the latest XML
  reference = get_currency()
 
  # we start to build our forex.dat output from here
  output = ""
  output += """institute: ""
reference: "%s"
""" % reference
 
  # get the list of present XML files in <xmlpath>
  present_files = get_xml_files_list(xmlpath)
 
  # we don't need more than 3 last days in forex.dat
  if len(present_files) > 3:
    range = 3
  else:
    range = 1
 
  i=0
  for i in xrange(0,range):
    with open(present_files[len(present_files)-i-1]) as f:
      # for each XML file we extract some data like...
      xmldict = xmltodict.parse(f.read())
      xmldict = xmldict['currency_market']
      # date of the XML file creation
      xml_create_date = xmldict['@created_date']
      # the 'board' section with all the currency factors
      xml_board = xmldict['board']['quotation']
      # each day is a "quotation"
      output += """quotation {
  date: "%s"
""" % (xml_create_date)
      # for each element in 'board' section we extract currency and factor
      for el in sorted(xml_board):
        currency = el['@base_currency']
        factor = el['#text']
        # and we add them as a 'rate' section
        output += """  rate {
    currency: "%s"
    factor: %s
  }
""" % (currency, factor)
      output +="""}
"""
  save_forex_file(output)
  sys.exit(0)

Comments