55* Author: liuf5
66*/
77"""
8- import datetime
8+ import re
99from colorama import init
1010
1111from zoomeye import config , data , plotlib
@@ -110,11 +110,12 @@ def print_data(data_list):
110110 printf ("total: {}" .format (total ))
111111
112112
113- def print_filter (keys , data ):
113+ def print_filter (keys , data_list , condition = None ):
114114 """
115115 used to display user filtered data on the terminal
116116 :param keys: user input key, is str
117- :param data: filter data ,is list
117+ :param data_list: filter data ,is list
118+ :param condition: list,
118119 :return:
119120 """
120121 total = 0
@@ -125,10 +126,19 @@ def print_filter(keys, data):
125126 printf ("{}" .format (title ), color = "green" )
126127
127128 # print data
128- for i in data :
129+ for i in data_list :
129130 items = ""
130131 for j in i :
131132 j_hex = convert_str (str (j ))
133+ # match to content highlight
134+ if condition :
135+ for item in condition :
136+ k , v = item .split ('=' )
137+ result_re = re .search (v , j_hex , re .I | re .M )
138+ if result_re :
139+ # replace to highlight
140+ color_content = "\033 [31m{}\033 [0m" .format (result_re .group ())
141+ j_hex = j_hex .replace (result_re .group (), color_content )
132142 items += "{:<30}" .format (j_hex )
133143 total += 1
134144 printf (items )
@@ -244,6 +254,9 @@ def print_host_data(host_data):
244254 """
245255 :param host_data, list,
246256 """
257+ # host data is None
258+ if len (host_data ) == 0 :
259+ return
247260 # parser hostname,country,city... information
248261 first_item = host_data [0 ]
249262 all_data , port_count = data .filter_history_data (data .fields_tables_history_host .keys (), host_data )
@@ -272,17 +285,20 @@ def print_host_data(host_data):
272285 printf (content )
273286
274287
275- def print_filter_history (fileds , hist_data ):
288+ def print_filter_history (fileds , hist_data , condition = None ):
276289 """
277290 print user filter history data,
278291 :param fileds list,user input field
279292 :param hist_data dict, from ZoomEye API get data
293+ :param condition list, filter condition
280294 """
281295 filter_title = ''
282296 first_item = hist_data [0 ]
297+ # filter data
283298 all_data , port_count = data .filter_history_data (fileds , hist_data , omit = False )
284299 printf (first_item .get ('ip' ))
285300 dict_first_item = data .ZoomEyeDict (first_item )
301+ # parser filter data title
286302 for dict_item in data .tables_history_info .keys ():
287303 result = dict_first_item .find (data .tables_history_info .get (dict_item ))
288304 if result == "" or result is None or result == "Unknown" :
@@ -299,5 +315,14 @@ def print_filter_history(fileds, hist_data):
299315 for data_item in all_data :
300316 content = ""
301317 for item_item in data_item :
318+ # match to content highlight
319+ if condition :
320+ for condition_item in condition :
321+ k , v = condition_item .split ('=' )
322+ re_result = re .search (str (v ), str (item_item ), re .I | re .M )
323+ content = "\033 [31m{}\033 [0m" .format (re_result .group ())
324+ # replace to highlight
325+ if re_result :
326+ item_item = item_item .replace (re_result .group (), content )
302327 content += "{:<27}" .format (item_item )
303328 printf (content )
0 commit comments