-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctrlFind.py
More file actions
167 lines (127 loc) · 3.92 KB
/
Copy pathctrlFind.py
File metadata and controls
167 lines (127 loc) · 3.92 KB
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
from bs4 import BeautifulSoup as BS
from bs4 import NavigableString
import requests
import pprint
import re
import argparse
pp = pprint.PrettyPrinter(indent=4)
globalStorage = []
def processCapture(elementArray):
res = []
i = 0
while(i < len(elementArray)):
for child in elementArray[i].descendants:
if isinstance(child, NavigableString):
stripChild = child.string.strip()
if ("Entity" or "Individual" in stripChild):
stripChild = stripChild.replace("-", "").strip()
if (stripChild != '' and stripChild != ','):
res.append(stripChild)
i += 1
return (res)
def processIndividualsNames(tempDictionary, array, index, individualName = ""):
if "name" not in tempDictionary:
while array[index] != "Name Type:":
individualName += array[index] + " "
#Names can have multiple or single tags
index += 1
tempDictionary["name"] = [individualName.strip()]
else:
while array[index] != "Name Type:":
PrimaryNameMatcher = re.match("Primary Name", array[index])
AliasMatcher = re.match("Primary Name", array[index])
if (not PrimaryNameMatcher and not AliasMatcher):
individualName += array[index] + " "
index += 1
tempDictionary["name"].append(individualName.strip())
return (tempDictionary, index)
def dictify(arr):
tempD = dict()
i = 0
while i < len(arr):
uniqueIdMatch = re.fullmatch("Unique ID:", arr[i])
if (uniqueIdMatch):
tempD["id"] = arr[i + 1]
tempD["type"] = arr[i + 2]
i += 2
#Since everything here on has a lookahead
elif ((i + 1) < len(arr)):
a = arr[i].split('Name:')
pattern = "Name:"
t = re.fullmatch(pattern, arr[i])
#Either we get a pattern match of whole word or None
if (t):
if tempD["type"] and tempD["type"] != "Individual":
if "name" not in tempD:
tempD["name"] = [arr[i + 1]]
else:
tempD["name"].append(arr[i + 1])
i += 1
if tempD["type"] == "Individual":
#Since this tag will be Name
i = i + 1
tempD, i = processIndividualsNames(tempD, arr, i)
if ("Address Country:" in arr[i]):
if "country" not in tempD:
tempD["country"] = [arr[i + 1]]
else:
if (arr[i+1]) not in tempD["country"]:
tempD["country"].append(arr[i + 1])
i += 1
#Addtional check before doing id lookahead
if (i != (len(arr) - 1) and "Unique ID:" in arr[i+1]):
globalStorage.append(tempD)
tempD = {}
i += 1
#For the very last element
elif ((i + 1) >= len(arr)):
globalStorage.append(tempD)
tempD = {}
i += 1
else:
i += 1
#normal increment
else:
i += 1
def load():
source = requests.get("https://docs.fcdo.gov.uk/docs/UK-Sanctions-List.html").text[:]
soup = BS(source, "lxml")
capturedDivs = soup.find_all("div")[1:]
processedElements = processCapture(capturedDivs)
dictify(processedElements)
def search(keyword: str, searchType: str):
result: (str, str, str) = []
for i in globalStorage:
for j in i["name"]:
match = re.search(keyword, j) if searchType == "substring" else re.match(keyword, j)
if (match):
result.append((i["id"], i["type"], keyword))
break
else:
pass
return result
def main():
parser = argparse.ArgumentParser(description='Find Unique ID, Name and Type from UK Sanctions List')
parser.add_argument('--keyword', type=str, nargs="+", help="a keyword/name to search for")
parser.add_argument('--searchType', choices=["substring", "start"], default="substring")
args = parser.parse_args()
keyword = args.keyword
searchType = args.searchType
try:
word = " ".join(keyword)
print ("Searching for: ", word)
print ("Search Type: ", searchType)
boolArrayAlpha = [x.isalpha() for x in keyword]
if (all(boolArrayAlpha)):
load()
result = search(word, searchType)
if not result:
print ("No results found!")
else:
pp.pprint(result)
else:
print ("Please provide a valid string that only contains letters")
except Exception as e:
print (e)
if __name__ == "__main__":
main()