-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_fias.py
99 lines (87 loc) · 3.77 KB
/
get_fias.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#Python script
#Using API dadata.ru
#Get fias/oktmo/postal
import dbf, requests, os
table = dbf.Table(r'C:\Users\Chel-uirc\Desktop\Doc\Backup\821405.dbf')
table.open(mode=dbf.READ_WRITE)
bad_addresses = open(r'C:\Users\Chel-uirc\Desktop\Doc\BAD.txt', 'a')
fias = ''
postal_code = 0
oktmo = 0
count_bad = 0
count_good = 0
detail_level = 0
API_KEY = '69d7f18a43362c802eb5113dbe20a4bfe2e2954d'
def get_details(address):
headers = {"Authorization": "Token {}".format(API_KEY), "Content-Type": "application/json"}
json_data = {"query": address, "count": 1}
resp = requests.post('https://dadata.ru/api/v2/suggest/address', json=json_data, headers=headers)
global fias, postal_code, oktmo, detail_level
fias = resp.json()['suggestions'][0]['data']['fias_id']
postal_code = resp.json()['suggestions'][0]['data']['postal_code']
oktmo = resp.json()['suggestions'][0]['data']['oktmo']
detail_level = resp.json()['suggestions'][0]['data']['fias_level']
def FindDetails():
for x in dbf.Process(table):
try:
if 'УЧАСТОК' in x[3].rstrip() or 'УЧ-К' in x[3].rstrip():
bad_addresses.write(str(
x[0].rstrip() + ', ' + x[1].rstrip() + ', ' + x[3].rstrip() + ', ' + x[4].rstrip() + '\n').lower())
else:
prep_addr = x[0].rstrip() + ', ' + x[1].rstrip() + ', ' + x[3].rstrip() + ', ' + x[4].rstrip()
get_details(prep_addr)
if detail_level == '8':
if len(x[8].rstrip()) != 0:
count_good += 1
print('Found: ' + prep_addr.lower())
print('Count good :' + str(count_good))
x[7] = fias
x[6] = oktmo
else:
count_good += 1
print('Found: ' + prep_addr.lower())
print('Count good :' + str(count_good))
x[8] = postal_code
x[7] = fias
x[6] = oktmo
else:
count_bad += 1
print('Not found: ' + prep_addr)
bad_addresses.write(str(x[0].rstrip() + ', ' + x[1].rstrip() + ', ' + x[3].rstrip() + ', ' + x[
4].rstrip() + '\n').lower())
print('Count bad: ' + str(count_bad))
except IndexError:
print('Error: ' + prep_addr)
print('Count bad: ' + str(count_bad))
count_bad += 1
bad_addresses.write(
str(x[0].rstrip() + ', ' + x[1].rstrip() + ', ' + x[3].rstrip() + ', ' + x[4].rstrip() + '\n').lower())
table.close()
def FindPostal():
global count_good, count_bad
for x in dbf.Process(table):
try:
if len(x[8].rstrip()) == 0:
if len(x[1].rstrip()) != 0:
prep_addr = x[0].rstrip() + ', ' + x[1].rstrip() + ', ' + x[3].rstrip() + ', ' + x[4].rstrip()
get_details(prep_addr)
x[8] = postal_code
count_good += 1
print('Count good: ' + str(count_good))
print(postal_code)
else:
prep_addr = x[0].rstrip() + ', ' + x[3].rstrip() + ', ' + x[4].rstrip()
get_details(prep_addr)
x[8] = postal_code
count_good += 1
print('Count good: ' + str(count_good))
print(postal_code)
except IndexError:
print('Bad address: ' + prep_addr)
count_bad += 1
print('Count bad: ' + str(count_bad))
bad_addresses.write(prep_addr + '\n')
table.close()
count_bad = 0
count_good = 0
FindPostal()