-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhs
executable file
·116 lines (88 loc) · 3.5 KB
/
hs
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
#!/usr/bin/python3
###############################################################################
# hs.py - Hokie Stalker
# Query the Virginia Tech people search service for information about a person.
# Available under the ISC License.
#
# https://github.com/mutantmonkey/hokiestalker
# author: mutantmonkey <[email protected]>
###############################################################################
import sys
import urllib.parse
import urllib.request
import json
from hokiestalker import parse_addr
SEARCH_URL = "https://apps.middleware.vt.edu/ws/v1/persons/ldap/fuzzysearch?"\
"query={0}"
def row(rows, name, data):
"""Return a formatted row for printing."""
if data is None:
return
if type(data) == str:
rows.append("{0:20s}{1}".format(name + ':', data))
else:
rows.append("{0:20s}{1}".format(name + ':', data[0]))
# print additional lines if necessary, trimming off the first row
if len(data) > 1:
for line in data[1:]:
rows.append("{0:20s}{1}".format('', line))
def jhasattr(obj, attr):
return attr in obj.keys()
def search(query):
"""Search LDAP using the argument as a query. Argument must be
a valid LDAP query."""
query = urllib.parse.quote(query)
r = urllib.request.Request(SEARCH_URL.format(query), headers={
'User-agent': 'hokiestalker/2.0',
})
f = urllib.request.urlopen(r)
has_results = False
results = json.loads(f.read())
for entry in results:
has_results = True
rows = []
names = []
if jhasattr(entry, 'displayName'):
names.append(entry.get("displayName")[0])
if jhasattr(entry, 'givenName') and jhasattr(entry, 'sn'):
if jhasattr(entry, 'middleName'):
names.append('{0} {1} {2}'.format(
entry.get("givenName")[0],
entry.get("middleName")[0],
entry.get("sn")[0]))
else:
names.append('{0} {1}'.format(
entry.get("givenName")[0],
entry.get("sn")[0]))
row(rows, 'Name', names)
if jhasattr(entry, 'uid'):
row(rows, 'UID', entry.get("uid")[0])
if jhasattr(entry, 'uupid'):
row(rows, 'PID', entry.get("uupid")[0])
if jhasattr(entry, 'major'):
row(rows, 'Major', entry.get("major")[0])
elif jhasattr(entry, 'department'):
row(rows, 'Department', entry.get("department"))
if jhasattr(entry, 'title'):
row(rows, 'Title', entry.get("title")[0])
if jhasattr(entry, 'postalAddress'):
row(rows, 'Office', parse_addr(entry.get("postalAddress")[0]))
if jhasattr(entry, 'mailStop'):
row(rows, 'Mail Stop', entry.get("mailStop")[0])
if jhasattr(entry, 'telephoneNumber'):
row(rows, 'Office Phone', entry.get("telephoneNumber")[0])
if jhasattr(entry, 'localPostalAddress'):
row(rows, 'Mailing Address', parse_addr(
entry.get("localPostalAddress")[0]))
if jhasattr(entry, 'localPhone'):
row(rows, 'Phone Number', entry.get("localPhone")[0])
if jhasattr(entry, 'mailPreferredAddress'):
row(rows, 'Email Address', entry.get("mailPreferredAddress")[0])
print("\n".join(rows))
print()
return has_results
if __name__ == '__main__':
q = sys.argv[1:]
s = search(' '.join(q))
if not s:
print("No results found")