phenny-1/modules/hs.py

61 lines
1.7 KiB
Python
Raw Normal View History

2011-10-19 18:56:04 -04:00
#!/usr/bin/python3
2011-02-18 18:12:09 -05:00
"""
hs.py - hokie stalker module
author: mutantmonkey <mutantmonkey@mutantmonkey.in>
2011-02-18 18:12:09 -05:00
"""
2011-10-19 18:56:04 -04:00
import web
import lxml.etree
2011-02-18 18:12:09 -05:00
2011-10-19 18:56:04 -04:00
SEARCH_URL = "https://webapps.middleware.vt.edu/peoplesearch/PeopleSearch?query={0}&dsml-version=2"
RESULTS_URL = "http://search.vt.edu/search/people.html?q={0}"
PERSON_URL = "http://search.vt.edu/search/person.html?person={0:d}"
2011-10-19 18:56:04 -04:00
NS = NS = '{urn:oasis:names:tc:DSML:2:0:core}'
2011-02-18 18:12:09 -05:00
2011-10-19 18:56:04 -04:00
"""Search the people search database using the argument as a query."""
2011-02-18 18:12:09 -05:00
def search(query):
2011-10-19 18:56:04 -04:00
query = web.quote(query)
try:
req = web.get(SEARCH_URL.format(query))
except (HTTPError, IOError):
phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.")
return
xml = lxml.etree.fromstring(req.encode('utf-8'))
results = xml.findall('{0}searchResponse/{0}searchResultEntry'.format(NS))
if len(results) <= 0:
return False
2011-02-18 18:12:09 -05:00
2011-10-19 18:56:04 -04:00
ret = []
for entry in results:
entry_data = {}
for attr in entry:
entry_data[attr.attrib['name']] = attr[0].text
ret.append(entry_data)
return ret
2011-02-18 18:12:09 -05:00
def hs(phenny, input):
""".hs <pid/name/email> - Search for someone on Virginia Tech People Search."""
q = input.group(2)
if q is None:
return
q = q.strip()
2011-10-19 18:56:04 -04:00
results = RESULTS_URL.format(web.quote(q))
2011-10-19 18:56:04 -04:00
s = search(q)
if s:
if len(s) >1:
phenny.reply("Multiple results found; try {0}".format(results))
else:
2011-10-19 18:56:04 -04:00
for entry in s:
person = PERSON_URL.format(int(entry['uid']))
phenny.reply("{0} - {1}".format(entry['cn'], person))
else:
phenny.reply("No results found")
2011-02-18 18:12:09 -05:00
hs.rule = (['hs'], r'(.*)')
if __name__ == '__main__':
2011-09-22 14:17:27 -04:00
print(__doc__.strip())