コード例 #1
0
def get_dadata(inn, request):
    dadata = Dadata(token)
    result = dadata.suggest("party", inn, count=20)

    if len(result) == 0:
        res = "Проверьте ИНН. ИНН не найден в базе"
        response_status = status.HTTP_404_NOT_FOUND
    else:
        res = [{
            'desc': '{}, {}. {}, {}'.format(
                r['data']['inn'],
                r['data']['address']['data']['city_type'],
                r['data']['address']['data']['city'],
                r['value']),
            'inn': r['data']['inn'],
            'kpp': r['data'].get('kpp', None),
            'ogrn': r['data']['ogrn'],
            'phone': r['data']['phones'],
            'email': r['data']['emails'],
            'full_name': r['data']['name']['full'],
            'type_customer': r['data']['opf']['short'],
            'head': r['data']['management']['post'] if r['data'].get('management') else r['data']['opf']['full'],
            'head_name': get_name(r['data'])['name'],
            'head_surname': get_name(r['data'])['surname'],
            'head_lastname': get_name(r['data'])['lastname'],
            'legal_address': r['data']['address']['value']
        } for r in result]
        response_status = status.HTTP_200_OK
    return Response(res, status=response_status)
コード例 #2
0
ファイル: utils.py プロジェクト: ProgOleg/m2m_express
 def __call__(self, query: str, token=settings.DADATA_API_TOKEN):
     self._token = token
     self.ddata = Dadata(token=self._token)
     self.query = query
     response = self._get_request()
     data = self._parse_response(response)
     return data
コード例 #3
0
ファイル: innCompany.py プロジェクト: Swatlprus/innCompany
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'])  # Название региона
コード例 #4
0
 def get_address_code(cls, address):
     token = settings.DADATA_TOKEN
     secret = settings.DADATA_SECRET_KEY
     dadata = Dadata(token, secret)
     data = dadata.clean("address", address)
     result = (data.get('geo_lat'), data.get('geo_lon'))
     return result
コード例 #5
0
def get_geo_from_dadata():
    api_key = "Тут секретный API ключ"
    secret_key = "Тут секретный ключ"

    dadata = Dadata(api_key, secret_key)

    counter, good, error = 0, 0, 0
    file_path = 'address.txt'

    with open(file_path, 'r') as file:

        for row in file:
            print('-' * 50)
            print(f'№ {counter}')
            print('address = ', row)
            try:
                result = dadata.clean(name="address",
                                      source="москва сухонская 11")
                print('lat = ', result['geo_lat'])
                print('lng = ', result['geo_lon'])
                good += 1
            except Exception as e:
                error += 1
                print('Не распознан')

            counter += 1

    print('===== Результат =====')
    print(
        f'Всего записей = {counter} \n Распознано = {good} \n Не распознано {error}'
    )

    dadata.close()
コード例 #6
0
ファイル: utils.py プロジェクト: poegva/store
def get_dadata_suggest(address):
    dadata = Dadata(settings.DADATA_APIKEY, settings.DADATA_SECRET)

    try:
        return dadata.suggest(name='address', query=address)
    except Exception as e:
        log.error('Dadata request exception ' + str(e))
        return None
コード例 #7
0
def get_organization_by_inn(inn, request):
    dadata = Dadata(token)
    result = dadata.suggest("party", inn)
    url = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/party"
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json",
        "Authorization": "Token {}".format(token)
    }
    data = {"query": inn}
    r = requests.post(url, headers=headers, json=data)
    try:
        res = r.json()['suggestions'][0]['data']
        try:
            type_customer = TypeOrganisation.objects.get(name=res['opf']['full'])
        except TypeOrganisation.DoesNotExist:
            type_customer = TypeOrganisation.objects.create(name=res['opf']['full'])

        if res['type'] == 'LEGAL':
            head_name = res['management']['name'].split()
            data = {
                'full_name': res['name']['short_with_opf'],
                'head': res['management']['post'],
                'head_name': head_name[1],
                'head_surname': head_name[2],
                'head_last_name': head_name[0],
                'kpp': res['kpp'],
                'ogrn': res['ogrn'],
                'inn': res['inn'],
                'address': res['address']['unrestricted_value'],
                'type_customer': TypeCustomerSer(type_customer, context={'request': request}).data
                # {"id":type_customer.pk,"name":type_customer.name}
            }
        elif res['type'] == 'INDIVIDUAL':
            head_name = res['name']['full'].split()
            data = {
                'full_name': res['name']['short_with_opf'],
                'head': res['opf']['full'],
                'head_name': head_name[1],
                'head_surname': head_name[2],
                'head_last_name': head_name[0],
                'kpp': '',
                'ogrn': res['ogrn'],
                'inn': res['inn'],
                'address': res['address']['unrestricted_value'],
                'type_customer': TypeCustomerSer(type_customer, context={'request': request}).data
            }
        else:
            raise Exception("Не известная форма собственности: {}".format(res['type']))
        response_status = status.HTTP_200_OK
        data['len'] = len(r.json()['suggestions'])
    except IndexError:
        data = "Проверьте ИНН. ИНН не найден в базе"
        response_status = status.HTTP_404_NOT_FOUND

    return Response(data, status=response_status)
コード例 #8
0
ファイル: utils.py プロジェクト: ProgOleg/m2m_express
class ParseDadataAddress:
    def __call__(self, query: str, token=settings.DADATA_API_TOKEN):
        self._token = token
        self.ddata = Dadata(token=self._token)
        self.query = query
        response = self._get_request()
        data = self._parse_response(response)
        return data

    def _get_request(self):
        response = self.ddata.suggest("address", self.query)
        return response

    @staticmethod
    def _parse_response(response):
        try:
            value = response[0].get("data")
        except IndexError:
            return
        postal_code = value.get('postal_code', '')
        city = value.get('city', '')
        geo_lat = value.get('geo_lat', '')
        geo_lon = value.get('geo_lon', '')
        city_fias_id = value.get('city_fias_id', '')
        return {
            'postal_code': postal_code,
            'city': city,
            'city_fias_id': city_fias_id,
            'location': {
                'lat': float(geo_lat),
                'lng': float(geo_lon),
            }
        }
コード例 #9
0
class Geocoder:
    WRONG_GEO_MSG = (
        'Нельзя определить адрес по указанным координатам. '
        'Попробуйте, пожалуйста, выбрать координаты ближе к населённому пункту.'
    )

    def __init__(self):
        self.dadata = Dadata(DADATA_TOKEN)

    @lru_cache(maxsize=10000)
    def get_geo_data(self, geocode: str = None, latitude: float = None, longitude: float = None) -> dict:
        default_result = dict()
        lat = 0.0
        lon = 0.0
        if geocode:
            lon, lat = geocode.split(',')

        if latitude and longitude:
            lat = latitude
            lon = longitude

        raw_data = self.dadata.geolocate(name="address", lat=lat, lon=lon, radius_meters=1000)
        if raw_data:
            result = raw_data[0]['data']
        else:
            result = default_result
        return result
コード例 #10
0
    def __init__(self, logger = None, env='local'):
        self.env = env
        if logger is None:
            self.logger = logging.getLogger("LegalEntitiesValidator")
            self.logger.setLevel(logging.DEBUG)
            handler = RotatingFileHandler("legal_entities_validator.log", mode='a', encoding='utf-8', backupCount=5,
                                          maxBytes=1 * 1024 * 1024)
            formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            handler.setFormatter(formatter)
            self.logger.addHandler(handler)
        else:
            self.logger = logger

        self.dblocal = mysqltool.mysqlTool(self.logger, config.DB_USER, config.DB_PASSWORD,
                                           config.DB_HOST, config.DB_PORT, config.DB_DATABASE)

        self.dadata = Dadata(config.DADATA_TOKEN)
コード例 #11
0
def get_dadata_bank(bank, request):
    dadata = Dadata(token)
    result = dadata.suggest("bank", bank)
    if len(result) == 0:
        res = "Проверьте реквизиты банка. Банк не найден в базе"
        response_status = status.HTTP_404_NOT_FOUND
    else:
        res = [{
            'desc': "{}, БИК {}, ИНН {}".format(r['value'], r['data']['bic'], r['data']['inn']),
            'name': r['value'],
            'bic': r['data']['bic'],
            'inn': r['data']['inn'],
            'kpp': r['data']['kpp'],
            'correspondent_account': r['data']['correspondent_account']
        } for r in result]
        response_status = status.HTTP_200_OK
    return Response(res, status=response_status)
コード例 #12
0
def get_coords_dadata(address: str, region_code: int) -> str:
    """
    принимает address
    получает lon, lat из API Dadata
    выдаёт строку с координатами
    """
    try:
        result = Dadata(DA_API, DA_SC).suggest("address", address,
                                               locations=[{"kladr_id": region_code}]
                                               )
        if result[0]['data']['geo_lon'] is None:
            result = Dadata(DA_API, DA_SC).suggest('address', address[:address.rfind(',')],
                                                   locations=[{"kladr_id": region_code}]
                                                   )
        return f",{result[0]['data']['geo_lon']},{result[0]['data']['geo_lat']}"
    except IndexError:
        return ',None,None'
コード例 #13
0
ファイル: extentions.py プロジェクト: DminGerasimov/C-5.6
 def update_currency():
     keys = {}
     r = requests.get(f'https://api.exchangeratesapi.io/latest?')
     currency_codes = json.loads(r.content)['rates']
     for record in currency_codes:
         with Dadata(DADATA_TOKEN, DADATA_SECRET) as dadata:
             result = dadata.find_by_id("currency", record)
             if result != []:
                 _ = result[0]['value'].lower()
         keys[_] = record
     return keys
コード例 #14
0
    def __dadata_address_shop(self, address):
        token = "5c9f229cb333ab8a4dde5de28588d4d0a9aff404"
        secret = "14b621d1269cac31c29384eb76f318ac6d30ae9a"
        dadata = Dadata(token, secret)

        def check_address(address):
            res = dadata.suggest(name="address", query=address, count=1)
            try:
                return f"{res[0]['data']['city']} {res[0]['data']['street']} "  # {res[0]['data']['house']}"
            except IndexError:
                return f'bad {address}'

        address = check_address(address)
        return address
コード例 #15
0
ファイル: utils.py プロジェクト: ProgOleg/m2m_express
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
コード例 #16
0
ファイル: hello.py プロジェクト: 8bv/one_more
                base['okved'][i] = "пусто"
            elif len(result) == 1:
                base['same'][i] = "одна компания"
                base['okved'][i] = result[0]['data']['okved']
            else:
                base['same'][i] = "много подобных компаний"
                base['okved'][i] = result[0]['data']['okved']

    base = pd.merge(base,
                    okved,
                    how='left',
                    left_on='okved',
                    right_on='Новый код ОКВЭД')
    base.drop(['Новый код ОКВЭД'], inplace=True, axis=1)
    base.to_excel(f'new_{file}', index=False)


def main():
    for file in os.listdir():
        parse(file)


if __name__ == '__main__':
    from token_holder import *
    import os
    dadata = Dadata(token)
    okved = pd.read_excel('new_okved.xlsx')
    directory = input('Папка с файлами: ')
    os.chdir(directory)
    main()
コード例 #17
0
ファイル: dataCollection.py プロジェクト: qper/pyEdu
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)
コード例 #18
0
ファイル: address_api.py プロジェクト: andrewfromtver/news_ag
from dadata import Dadata
import app_config
import sys

a = str(sys.argv[1])
t = app_config.address_api_key

dadata = Dadata(t)

result = dadata.suggest("address", a)
resp = str(result).replace("'", '"').replace("None", "null")
print(resp)
コード例 #19
0
 def get_suggestions(cls, address):
     token = settings.DADATA_TOKEN
     dadata = Dadata(token)
     return dadata.suggest('address', address)
コード例 #20
0
def check_remain() -> int:
    """проверка сколько осталось запросов"""
    return 10_000 - Dadata(DA_API, DA_SC).get_daily_stats()['services']['suggestions']
コード例 #21
0
 def __init__(self):
     self.dadata = Dadata(DADATA_TOKEN)
コード例 #22
0
class LegalEntitiesValidator:

    def __init__(self, logger = None, env='local'):
        self.env = env
        if logger is None:
            self.logger = logging.getLogger("LegalEntitiesValidator")
            self.logger.setLevel(logging.DEBUG)
            handler = RotatingFileHandler("legal_entities_validator.log", mode='a', encoding='utf-8', backupCount=5,
                                          maxBytes=1 * 1024 * 1024)
            formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            handler.setFormatter(formatter)
            self.logger.addHandler(handler)
        else:
            self.logger = logger

        self.dblocal = mysqltool.mysqlTool(self.logger, config.DB_USER, config.DB_PASSWORD,
                                           config.DB_HOST, config.DB_PORT, config.DB_DATABASE)

        self.dadata = Dadata(config.DADATA_TOKEN)

    def search_fns(self, le, status='ACTIVE'):
        result = {}
        result['result'] = False
        result['count'] = 0
        result["companies"] = []
        json_data = self.dadata.suggest(name='party', query=le, count=20, params={'status': status})
        fns_results = self.process_fns_data(json_data)
        # print(json.dumps(json_data,ensure_ascii=False))
        if fns_results["result"]:
            result['result'] = True
            result['count'] = fns_results["count"]
            result["companies"] = fns_results["companies"]
        return result

    def process_fns_data(self, result_json):
        data = {}
        data["result"] = False
        data["count"] = 0
        data["companies"] = []
        any_data_inserted = False
        if len(result_json) == 0:
            return data
        for item in result_json:
            try:
                p_data = {}
                item_data = item["data"]
                p_data["shortname"] = item_data["name"]["short_with_opf"]
                p_data["fullname"] = item_data["name"]["full_with_opf"]
                p_data["address"] = item_data["address"]["unrestricted_value"]
                p_data["ogrn"] = item_data["ogrn"]
                p_data["inn"] = item_data["inn"]
                p_data["status"] = item_data["state"]["status"]
                p_data["bruchtype"] = item_data["branch_type"]
                try:
                    p_data["mng_post"] = item_data["management"]["post"]
                    p_data["mng_name"] = item_data["management"]["name"]
                except:
                    p_data["mng_post"] = ""
                    p_data["mng_name"] = ""
                print(p_data)
                data["companies"].append(p_data)
                if p_data["bruchtype"] != 'BRANCH':
                    self.dblocal.db_execute("insert into legal_bot_companies (inn, ogrn, name, name_t, fullname, fullname_t, address, status, brunch_type, mng_post, mng_name) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
                                            (p_data["inn"],p_data["ogrn"],p_data["shortname"],self.search_preprocess(p_data["shortname"]),
                                     p_data["fullname"],self.search_preprocess(p_data["fullname"]),
                                     p_data["address"],p_data["status"],p_data["bruchtype"],p_data["mng_post"],p_data["mng_name"]), "Insert company")
                    any_data_inserted = True
            except Exception as e:
                self.logger.warning("Process fns data:" + str(e) + ";" + str(item))
        if any_data_inserted:
            data["result"] = True
            data["count"] = len(result_json)
        return data

    def short_to_full_preprocess(self, le_name):
        if le_name.upper().startswith('ООО'):
            le_name = le_name.upper().replace('ООО','ОБЩЕСТВО С ОГРАНИЧЕННОЙ ОТВЕТСТВЕННОСТЬЮ')
        return le_name

    def search_preprocess(self, le_name):
        SIMBOLS_LIST = ['\"', '«', '»', '\'', '.', '–', '-', '+', '!', '№']
        for simbol in SIMBOLS_LIST:
            le_name = le_name.replace(simbol," ")
            le_name = le_name.replace("  ", " ")
        le_name = le_name.replace("  ", " ")
        return le_name

    def postprocess(self, le_name):
        if le_name.startswith("ОБЩЕСТВО С ОГРАНИЧЕННОЙ ОТВЕТСТВЕННОСТЬЮ"):
            le_name = le_name.replace("ОБЩЕСТВО С ОГРАНИЧЕННОЙ ОТВЕТСТВЕННОСТЬЮ", "ООО")
        if le_name.startswith("ПУБЛИЧНОЕ АКЦИОНЕРНОЕ ОБЩЕСТВО"):
            le_name = le_name.replace("ПУБЛИЧНОЕ АКЦИОНЕРНОЕ ОБЩЕСТВО", "ПАО")
        return le_name

    def validate(self, le_name, ogrn_list):
        result = {}
        result["possible_names"] = []
        result["message"] = ""

        le_name_without_simbols = self.search_preprocess(le_name)

        # сначала проверяем данные в нашей БД, ищем по полному совпадению без учета символов
        full_match_query = "select fullname, address, ogrn from legal_bot_companies where fullname_t = \'" + le_name_without_simbols + "\'"
        full_match_results = self.dblocal.db_query(full_match_query, (), "Validate() full match query")
        if len(full_match_results) == 1:
            # нашли в нашей БД точное совпадение, сохраняем
            result["isvalid"] = True
            result["name"] = self.postprocess(full_match_results[0][0])
            result["address"] = full_match_results[0][1]
            result["ogrn"] = full_match_results[0][2]
            result["isshort"] = False
            return result
        elif len(full_match_results) > 1:
            # нашли несколько совпадений, сверяем по ОГРН
            for full_match_result in full_match_results:
                if full_match_result[2] in ogrn_list:
                    # найдено совпадение (здесь возможна маловероятная коллиция, так как берем только первое совпадение
                    #   но наличие двух случайных похожих юр.лиц в тексте и в выборке считаем редким)
                    result["isvalid"] = True
                    result["name"] = self.postprocess(full_match_result[0])
                    result["address"] = full_match_result[1]
                    result["ogrn"] = full_match_result[2]
                    result["isshort"] = False
                    return result
            # не нашли совпадений по ОГРН, но есть результаты
            # передаем неподтвержденные данные
            for full_match_result in full_match_results:
                possible_value = {}
                possible_value["name"] = self.postprocess(full_match_result[0])
                possible_value["address"] = full_match_result[1]
                possible_value["ogrn"] = full_match_result[2]
                result["possible_names"].append(possible_value)
            result["isvalid"] = False
            return result

        # проверяем по короткому имени. TODO Валидный результат только при совпадении ОГРН?
        short_match_query = "select fullname, address, ogrn from legal_bot_companies where name_t = \'" + le_name_without_simbols + "\'"
        short_match_results = self.dblocal.db_query(short_match_query, (), "Validate() short match query")
        if len(short_match_results) == 1:
            # нашли в нашей БД точное совпадение, сохраняем
            result["isvalid"] = True
            result["name"] = self.postprocess(short_match_results[0][0])
            result["address"] = short_match_results[0][1]
            result["ogrn"] = short_match_results[0][2]
            result["isshort"] = True
            return result
        elif len(short_match_results) > 1:
            # нашли несколько совпадений, сверяем по ОГРН
            for short_match_result in short_match_results:
                if short_match_result[2] in ogrn_list:
                    # найдено совпадение (здесь возможна маловероятная коллиция, так как берем только первое совпадение
                    #   но наличие двух случайных похожих юр.лиц в тексте и в выборке считаем редким)
                    result["isvalid"] = True
                    result["name"] = self.postprocess(short_match_result[0])
                    result["address"] = short_match_result[1]
                    result["ogrn"] = short_match_result[2]
                    result["isshort"] = True
                    return result
            # не нашли совпадений по ОГРН, но есть результаты
            # передаем неподтвержденные данные
            for short_match_result in short_match_results:
                possible_value = {}
                possible_value["name"] = self.postprocess(short_match_result[0])
                possible_value["address"] = short_match_result[1]
                possible_value["ogrn"] = short_match_result[2]
                result["possible_names"].append(possible_value)
            result["isvalid"] = False
            return result

        # не нашли в нащей БД, идем в ФНС
        search_fns_result = self.search_fns(le_name)
        if search_fns_result['result']:
            # что-то добавили из ФНС, повторяем поиск
            # сначала проверяем данные в нашей БД, ищем по полному совпадению без учета символов
            full_match_query = "select fullname, address, ogrn from legal_bot_companies where fullname_t = \'" + le_name_without_simbols + "\'"
            full_match_results = self.dblocal.db_query(full_match_query, (), "Validate() full match query")
            if len(full_match_results) == 1:
                # нашли в нашей БД точное совпадение, сохраняем
                result["isvalid"] = True
                result["name"] = self.postprocess(full_match_results[0][0])
                result["address"] = full_match_results[0][1]
                result["ogrn"] = full_match_results[0][2]
                result["isshort"] = False
                return result
            elif len(full_match_results) > 1:
                # нашли несколько совпадений, сверяем по ОГРН
                for full_match_result in full_match_results:
                    if full_match_result[2] in ogrn_list:
                        # найдено совпадение (здесь возможна маловероятная коллиция, так как берем только первое совпадение
                        #   но наличие двух случайных похожих юр.лиц в тексте и в выборке считаем редким)
                        result["isvalid"] = True
                        result["name"] = self.postprocess(full_match_result[0])
                        result["address"] = full_match_result[1]
                        result["ogrn"] = full_match_result[2]
                        result["isshort"] = False
                        return result
                # не нашли совпадений по ОГРН, но есть результаты
                # передаем неподтвержденные данные
                for full_match_result in full_match_results:
                    possible_value = {}
                    possible_value["name"] = self.postprocess(full_match_result[0])
                    possible_value["address"] = full_match_result[1]
                    possible_value["ogrn"] = full_match_result[2]
                    result["possible_names"].append(possible_value)
                result["isvalid"] = False
                return result

            # проверяем по короткому имени. TODO Валидный результат только при совпадении ОГРН?
            short_match_query = "select fullname, address, ogrn from legal_bot_companies where name_t = \'" + le_name_without_simbols + "\'"
            short_match_results = self.dblocal.db_query(short_match_query, (), "Validate() short match query")
            if len(short_match_results) == 1:
                # нашли в нашей БД точное совпадение, сохраняем
                result["isvalid"] = True
                result["name"] = self.postprocess(short_match_results[0][0])
                result["address"] = short_match_results[0][1]
                result["ogrn"] = short_match_results[0][2]
                result["isshort"] = True
                return result
            elif len(short_match_results) > 1:
                # нашли несколько совпадений, сверяем по ОГРН
                for short_match_result in short_match_results:
                    if short_match_result[2] in ogrn_list:
                        # найдено совпадение (здесь возможна маловероятная коллиция, так как берем только первое совпадение
                        #   но наличие двух случайных похожих юр.лиц в тексте и в выборке считаем редким)
                        result["isvalid"] = True
                        result["name"] = self.postprocess(short_match_result[0])
                        result["address"] = short_match_result[1]
                        result["ogrn"] = short_match_result[2]
                        result["isshort"] = True
                        return result
                # не нашли совпадений по ОГРН, но есть результаты
                # передаем неподтвержденные данные
                for short_match_result in short_match_results:
                    possible_value = {}
                    possible_value["name"] = self.postprocess(short_match_result[0])
                    possible_value["address"] = short_match_result[1]
                    possible_value["ogrn"] = short_match_result[2]
                    result["possible_names"].append(possible_value)
                result["isvalid"] = False
                return result

            # если дошли сюда, значит после ФНС ничего
            # попробуем хак: обрабатываем кейс когда имя полное, а форма сокращенная
            le_short_to_full_preporcess = self.short_to_full_preprocess(le_name_without_simbols)
            full_match_query = "select fullname, address, ogrn from legal_bot_companies where fullname_t = \'" + le_short_to_full_preporcess + "\'"
            full_match_results = self.dblocal.db_query(full_match_query, (), "Validate() short-to-full match query")
            if len(full_match_results) == 1:
                # нашли в нашей БД точное совпадение, сохраняем
                result["isvalid"] = True
                result["name"] = self.postprocess(full_match_results[0][0])
                result["address"] = full_match_results[0][1]
                result["ogrn"] = full_match_results[0][2]
                result["isshort"] = False
                return result
            elif len(full_match_results) > 1:
                # нашли несколько совпадений, сверяем по ОГРН
                for full_match_result in full_match_results:
                    if full_match_result[2] in ogrn_list:
                        # найдено совпадение (здесь возможна маловероятная коллиция, так как берем только первое совпадение
                        #   но наличие двух случайных похожих юр.лиц в тексте и в выборке считаем редким)
                        result["isvalid"] = True
                        result["name"] = self.postprocess(full_match_result[0])
                        result["address"] = full_match_result[1]
                        result["ogrn"] = full_match_result[2]
                        result["isshort"] = False
                        return result
                # не нашли совпадений по ОГРН, но есть результаты
                # передаем неподтвержденные данные
                for full_match_result in full_match_results:
                    possible_value = {}
                    possible_value["name"] = self.postprocess(full_match_result[0])
                    possible_value["address"] = full_match_result[1]
                    possible_value["ogrn"] = full_match_result[2]
                    result["possible_names"].append(possible_value)
                result["isvalid"] = False
                return result

            # проверим, а нашли ли что-то в ФНС и иесли нашли, выведем это в possible_names
            if search_fns_result["count"] > 0:
                for company in search_fns_result["companies"]:
                    possible_value = {}
                    possible_value["name"] = company["fullname"]
                    possible_value["address"] = company["address"]
                    possible_value["ogrn"] = company["ogrn"]
                    result["possible_names"].append(possible_value)
                result["isvalid"] = False
                return result

            # если дошли сюда, то точно ничего не нашли
            self.logger.warning("Company not found after FNS update:" + le_name)
            result["isvalid"] = False
            result["message"] = "Company not found after FNS update"
            return result

        # в ФНС ничего не смогли найти, скорре всего ошибка распознавания при парсинге
        self.logger.warning("Company not found in FNS:" + le_name)
        result["isvalid"] = False
        result["message"] = "Company not found in FNS"
        return result
コード例 #23
0
ファイル: utils.py プロジェクト: ProgOleg/m2m_express
 def __init__(self, query, token=settings.DADATA_API_TOKEN):
     self._token = token
     self.ddata = Dadata(token=self._token)
     self.query = query
コード例 #24
0
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'],
        }
コード例 #25
0
def get_gender(full_name):
    token = "f1f65baffababb3c9a5f1f89303aa95b671a10a3"
    secret = "0123b896ae0502c432e990444e0dd1a8b4eb8923"
    dadata = Dadata(token, secret)
    result = dadata.clean("name", full_name)
    return result['gender']
コード例 #26
0
 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)