Advertisement
s4ros

Forex.dat ultimate fix

Oct 18th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import json
  4. import sys
  5. import os
  6. try:
  7.   import xmltodict
  8. except ImportError:
  9.   raise ImportError('Please try to install module: sudo pip install xmltodict')
  10.  
  11. # //////////////////////////////////////////////////////////
  12. xmlpath = '/opt/ang/var/foreximport'
  13. # xmlpath = './'
  14. forexdat = '/opt/ang/mnt/rtb/forex.dat'
  15. # //////////////////////////////////////////////////////////
  16.  
  17. # //////////////////////////////////////////////////////////
  18. def get_xml_files_list(dirpath):
  19.   """
  20.  Get the sorted list of XML files present in <dirpath>
  21.  """
  22.   if not os.path.isdir(dirpath):
  23.     print "%s:ERROR:%s path doesn't exist!" % (sys.argv[0], dirpath)
  24.     sys.exit(1)
  25.   for root, dirs, files in os.walk(dirpath, topdown=True):
  26.     filelist = []
  27.     for f in files:
  28.       if ".xml" in f:
  29.         filepath = os.path.join(root, f)
  30.         filelist.append(filepath)
  31.     return sorted(filelist)
  32. # //////////////////////////////////////////////////////////
  33.  
  34. # //////////////////////////////////////////////////////////
  35. def get_currency():
  36.   latest = get_xml_files_list(xmlpath)[-1]
  37.   fp = open(latest, 'r')
  38.   xp = xmltodict.parse(fp.read())
  39.   currency = xp['currency_market']['@counter_currency']
  40.   return currency
  41. # //////////////////////////////////////////////////////////
  42.  
  43. # //////////////////////////////////////////////////////////
  44. def save_forex_file(content):
  45.   if os.path.exists(forexdat):
  46.     with open(forexdat, 'w') as f:
  47.       f.write(content)
  48.   else:
  49.     print "%s file does not exist!" % forexdat
  50.     sys.exit(1)
  51. # //////////////////////////////////////////////////////////
  52.  
  53.  
  54. #with open() as f:
  55. #  xmldict = xmltodict.parse(f.read())
  56.  
  57. if __name__ == "__main__":
  58.   # get the reference currency from the latest XML
  59.   reference = get_currency()
  60.  
  61.   # we start to build our forex.dat output from here
  62.   output = ""
  63.   output += """institute: ""
  64. reference: "%s"
  65. """ % reference
  66.  
  67.   # get the list of present XML files in <xmlpath>
  68.   present_files = get_xml_files_list(xmlpath)
  69.  
  70.   # we don't need more than 3 last days in forex.dat
  71.   if len(present_files) > 3:
  72.     range = 3
  73.   else:
  74.     range = 1
  75.  
  76.   i=0
  77.   for i in xrange(0,range):
  78.     with open(present_files[len(present_files)-i-1]) as f:
  79.       # for each XML file we extract some data like...
  80.       xmldict = xmltodict.parse(f.read())
  81.       xmldict = xmldict['currency_market']
  82.       # date of the XML file creation
  83.       xml_create_date = xmldict['@created_date']
  84.       # the 'board' section with all the currency factors
  85.       xml_board = xmldict['board']['quotation']
  86.       # each day is a "quotation"
  87.       output += """quotation {
  88.  date: "%s"
  89. """ % (xml_create_date)
  90.       # for each element in 'board' section we extract currency and factor
  91.       for el in sorted(xml_board):
  92.         currency = el['@base_currency']
  93.         factor = el['#text']
  94.         # and we add them as a 'rate' section
  95.         output += """  rate {
  96.    currency: "%s"
  97.    factor: %s
  98.  }
  99. """ % (currency, factor)
  100.       output +="""}
  101. """
  102.   save_forex_file(output)
  103.   sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement