chuuupa

companies_codes

Jul 15th, 2021
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. def companies_codes(response):
  2.     bytes = etree.fromstring(
  3.         Environment.remove_prefix(response, '<?xml version="1.0" encoding="UTF-8"?>'),
  4.         etree.XMLParser(huge_tree=True)
  5.     )[0][0].text
  6.  
  7.     parser = etree.XMLParser(huge_tree=True)
  8.  
  9.     with open(os.path.join(Environment.tmp_dir, 'GetCompaniesCodes.zip'), 'wb') as fout:
  10.         fout.write(base64.b64decode(bytes))
  11.  
  12.     with ZipFile(os.path.join(Environment.tmp_dir, 'GetCompaniesCodes.zip'), 'r') as zipObj:
  13.         zipObj.extractall(Environment.tmp_dir)
  14.  
  15.     xml_files = []
  16.     for filename in os.listdir(Environment.tmp_dir):
  17.         if filename.endswith('.xml'):
  18.             xml_files.append(
  19.                 etree.parse(os.path.join(Environment.tmp_dir, filename), parser))
  20.    
  21.     attrs = []
  22.  
  23.     for _file in xml_files:
  24.         root = _file.getroot().find('Data')
  25.         for company in root.iter('Company'):
  26.             attrs.append(
  27.                 {
  28.                     'sparkId': company.attrib.get('SparkID'),
  29.                     'inn': company.attrib.get('INN'),
  30.                     'ogrn': company.attrib.get('OGRN')
  31.                 }
  32.             )
  33.  
  34.     for filename in os.listdir(Environment.tmp_dir):
  35.         if filename.endswith('.zip') or filename.endswith('.xml'):
  36.             os.remove(os.path.join(Environment.tmp_dir, filename))
  37.  
  38.     return attrs
Add Comment
Please, Sign In to add comment