def check(name, query): dadata = Dadata(token, secret) # Данные для авторизации в сервисе DaData infoCompany = dadata.find_by_id(name, query) # Поиск информации о компании if infoCompany[0]['data']['type'] == 'LEGAL': print(infoCompany[0]['data']['name']['full']) # Названия компании print(infoCompany[0]['data']['type']) # Тип компании print(infoCompany[0]['data']['opf']['full']) # Полный префикс компании print(infoCompany[0]['data']['opf'] ['short']) # Короткий префикс компании print(infoCompany[0]['data']['address']['data'] ['region_type_full']) # Тип региона print(infoCompany[0]['data']['address']['data'] ['region']) # Название региона print(infoCompany[0]['data']['address']['data'] ['city_type']) # Название города print(infoCompany[0]['data']['address']['data'] ['city']) # Название города print(infoCompany[0]['data']['address']['data'] ['street_type_full']) # Тип улица/проспект print(infoCompany[0]['data']['address']['data'] ['street']) # Название улицы print(infoCompany[0]['data']['address']['data'] ['house_type_full']) # Тип улица/проспект print(infoCompany[0]['data']['address']['data'] ['house']) # Название улицы else: print(infoCompany[0]['data']['name']['full']) # Названия компании print(infoCompany[0]['data']['type']) # Тип компании print(infoCompany[0]['data']['opf']['full']) # Полный префикс компании print(infoCompany[0]['data']['opf'] ['short']) # Короткий префикс компании print(infoCompany[0]['data']['address']['data'] ['region_type_full']) # Тип региона print(infoCompany[0]['data']['address']['data'] ['region']) # Название региона
class EntrepreneurInfo: errors = { 'data not found': { 'error': 'По данному ИНН данных не обнаруженно' }, 'unknown error': { 'error': 'Неизвестная ошибка' }, } count_resp = 3 def __init__(self, query, token=settings.DADATA_API_TOKEN): self._token = token self.ddata = Dadata(token=self._token) self.query = query def entrepreneur_info(self): data = self._attempt_response() data = self._parse_entrepreneur_info(data) return data def _parse_entrepreneur_info(self, data): try: name_company = data[0]['value'] ogrn = data[0]['data']['ogrn'] kpp = data[0]['data']['kpp'] address = data[0]['data']['address']['value'] except KeyError: return self.errors['data not found'] except TypeError: return self.errors['unknown error'] return { "entrepreneur_info": { "name_company": name_company, "ogrn": ogrn, "kpp": kpp, "address": address } } def _attempt_response(self, count=count_resp): for _ in range(count): data = self._resp() if data: return data return def _resp(self): response = self.ddata.find_by_id("party", query=self.query) return response
from tqdm import tqdm from json import dump, dumps, load, loads data = {} current_date_and_time = format_time('%Y-%m-%d-%H-%M-%S') data_file = f'{current_date_and_time}.json' file_dir = path.dirname(path.realpath(__file__)) data_dir = f'{file_dir}/data' assert path.isdir(data_dir), 'Data directory is absent' config = ConfigParser() config.read('config.ini') token = config['CREDENTIALS']['token'] search = Dadata(token) with open('tax_numbers.txt', 'r') as file: tax_numbers_list = file.read().split('\n') for item in tqdm(tax_numbers_list): # find_by_id returns a list response = search.find_by_id("party", str(item)) print(response) data.update({f'{item}': response}) print(data) with open(f'{data_dir}/{data_file}', 'w') as json_file: dump(data, json_file)
class DadataService: def __init__(self, dadata_api_key=None): if dadata_api_key is None: dadata_api_key = environ.get('DADATA_API_KEY') self.dadata = Dadata(dadata_api_key) def get_legal_entity_info_by_inn(self, inn): result = self.dadata.find_by_id('party', inn) if result is None: return { 'error_code': 1, 'message': 'Legal entity with INN ' + inn + ' not found', 'status': 'error' } if len(result) != 1: return { 'error_code': 2, 'message': 'Multiple Legal entities with INN ' + inn + ' found', 'status': 'error' } result = result[0] ceo = result['unrestricted_value'] if 'management' in result['data']: ceo = result['data']['management']['name'] return { 'error_code': 0, 'status': 'success', 'name': result['unrestricted_value'], 'inn': result['data']['inn'], 'kpp': result['data'].get('kpp', 'not availible'), 'ogrn': result['data']['ogrn'], 'address': result['data']['address']['data']['source'], 'ceo': ceo } def get_bank_info_by_bik(self, bik): result = self.dadata.find_by_id('bank', bik) if result is None: return { 'error_code': 1, 'message': 'Bank with BIK ' + bik + ' not found', 'status': 'error' } if len(result) != 1: return { 'error_code': 2, 'message': 'Multiple banks with BIK ' + bik + ' found', 'status': 'error' } result = result[0] return { 'error_code': 0, 'status': 'success', 'name': result['unrestricted_value'], 'inn': result['data']['inn'], 'kpp': result['data']['kpp'], 'correspondent_account': result['data']['correspondent_account'], 'city': result['data']['address']['data']['city'], }