-
Notifications
You must be signed in to change notification settings - Fork 0
/
duns_crawler.py
178 lines (148 loc) · 5.92 KB
/
duns_crawler.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2016 Peng Liu <liupeng@imscv.com>
#
# Distributed under terms of the GNU GPL3 license.
"""
This is a crawler to get duns.
"""
import Tkinter
import ttk
import tkMessageBox
import traceback
import string
import itertools
from static_content import *
import duns_utils
import ui_utility
def main():
"""
This is the main function.
"""
countries = sorted(get_country_dict().keys())
# states are useless if we don't need US company.
# states = sorted(get_state_dict().keys())
root = Tkinter.Tk()
root.title("duns助手")
# Frame layout level one.
f_permilinary = Tkinter.LabelFrame(root, text="必填项")
f_permilinary.grid(column=0, row=0)
f_trival = Tkinter.LabelFrame(root, text="可空项")
f_trival.grid(column=1, row=0, sticky="NW")
f_action = Tkinter.Frame(root)
f_action.grid(column=2, row=0)
f_result = Tkinter.LabelFrame(root, text="结果")
f_result.grid(column=0, row=1, columnspan=3, sticky="WE")
ff_country = Tkinter.Frame(f_permilinary)
ff_country.grid(column=0, row=0)
ff_name = Tkinter.Frame(f_permilinary)
ff_name.grid(column=0, row=1)
ff_city = Tkinter.Frame(f_trival)
ff_city.grid(column=0, row=1)
ff_zip = Tkinter.Frame(f_trival)
ff_zip.grid(column=0, row=2)
ff_address = Tkinter.Frame(f_trival)
ff_address.grid(column=0, row=3)
ff_button = Tkinter.Frame(f_action)
ff_button.grid()
ff_treeview = Tkinter.Frame(f_result)
ff_treeview.grid(sticky="WE")
label_country = Tkinter.Label(ff_country, text="国家")
label_country.grid(column=0, row=0, sticky="WE")
list_country = Tkinter.Listbox(ff_country, exportselection=False)
for country in reversed(countries):
list_country.insert(0, country)
list_country.grid(column=0, row=1, sticky="WE")
label_name = Tkinter.Label(ff_name, text="名称:")
label_name.grid(column=0, row=0)
entry_name = Tkinter.Entry(ff_name)
entry_name.grid(column=1, row=0)
label_city = Tkinter.Label(ff_city, text="城市:")
label_city.grid(column=0, row=0)
entry_city = Tkinter.Entry(ff_city)
entry_city.grid(column=1, row=0)
label_zip = Tkinter.Label(ff_zip, text="邮编:")
label_zip.grid(column=0, row=0)
entry_zip = Tkinter.Entry(ff_zip)
entry_zip.grid(column=1, row=0)
label_address = Tkinter.Label(ff_address, text="地址:")
label_address.grid(column=0, row=0)
entry_address = Tkinter.Entry(ff_address)
entry_address.grid(column=1, row=0)
tree = ttk.Treeview(ff_treeview)
def search_callback_wrapper():
try:
country, name, city, zip_code, address = ui_utility.get_query_info(
list_country, entry_name, entry_city, entry_zip, entry_address)
url = duns_utils.get_search_url(country, name, city, zip_code,
address)
result = duns_utils.get_search_result(url)
result = duns_utils.filter_result(result)
ui_utility.update_tree_content(tree, result)
except Exception as e:
traceback.print_exc()
tkMessageBox.showerror("错误", e)
butt_search = Tkinter.Button(ff_button,
text="查询",
command=search_callback_wrapper)
butt_search.grid()
def enum_callback_wrapper():
try:
country, name, city, zip_code, address = ui_utility.get_query_info(
list_country, entry_name, entry_city, entry_zip, entry_address)
assert len(name.split(" ")) == 1 or len(name.split(" ")) == 2
def enum_name(name):
alphabet = list(string.ascii_lowercase)
name_sep = name.split(" ")
for c in alphabet:
yield "".join([name_sep[0]] + [c] + ([name_sep[1]] if len(
name_sep) == 2 else []))
result_list = duns_utils.pool_enum_search(country, enum_name(name),
city, zip_code, address)
result_list = list(itertools.chain.from_iterable(result_list))
ui_utility.update_tree_content(tree, result_list)
except Exception as e:
traceback.print_exc()
tkMessageBox.showerror("错误", e)
butt_enum = Tkinter.Button(ff_button,
text="穷举",
command=enum_callback_wrapper)
butt_enum.grid()
def limit_callback_wrapper():
try:
city = ui_utility.get_city_info(entry_city)
if city == "":
raise Exception("城市名称为空!")
ui_utility.treeview_limit_entry(tree, city)
except Exception as e:
traceback.print_exc()
tkMessageBox.showerror("错误", e)
butt_limit = Tkinter.Button(ff_button, text="筛除", command=limit_callback_wrapper)
butt_limit.grid()
tree.grid()
cols = ["duns", "country", "city", "name", "address_line1", "address_line2", "zip_code"]
tree["columns"] = cols
tree.column("#0", width=0)
# http://stackoverflow.com/questions/20079989
def sort_wrapper(col):
_col = col
return lambda: ui_utility.treeview_sort_column(tree, _col, False)
for col in cols:
tree.heading(col, command=sort_wrapper(col))
tree.heading("duns", text="duns")
tree.heading("country", text="国家")
tree.heading("city", text="城市")
tree.heading("name", text="名称")
tree.heading("address_line1", text="地址行一")
tree.heading("address_line2", text="地址行二")
tree.heading("zip_code", text="邮编")
# tree.heading("address", text="地址")
# Treeview copy.
def treeview_copy_wrapper(event):
ui_utility.treeview_copy_rows(tree)
tree.bind("<Control-c>", treeview_copy_wrapper)
root.mainloop()
if __name__ == '__main__':
main()