Skip to content

Commit

Permalink
Merge pull request #193 from pittcsc/mypy-people
Browse files Browse the repository at this point in the history
Fix `mypy` errors in `people.py`
  • Loading branch information
tianyizheng02 authored Aug 25, 2024
2 parents e1589e2 + e3a5616 commit 8ba515b
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pittapi/people.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

from requests_html import HTMLSession
from requests_html import HTMLSession, Element
from typing import Any

# Please note that find.pitt.edu will not accept more than 10 requests within a few minutes
# It will time out if that happens
Expand All @@ -41,7 +42,7 @@
}


def _parse_segments(person: dict, segments: list[str]) -> None:
def _parse_segments(person: dict[str, Any], segments: list[Element]) -> None:
label = None
for segment in segments:
if "class" in segment.attrs and "row-label" in segment.attrs["class"]:
Expand All @@ -60,19 +61,19 @@ def _parse_segments(person: dict, segments: list[str]) -> None:
person[label] = segment.text


def get_person(query: str) -> list[dict[str, str]]:
def get_person(query: str) -> list[dict[str, Any]]:
payload = {"search": query}
session = HTMLSession()
resp = session.post(PEOPLE_SEARCH_URL, data=payload)
if resp.text.__contains__("Too many people matched your criteria."):
return [{"ERROR": "Too many people matched your criteria."}] # Return an error
if "Too many people matched your criteria." in resp.text:
return [{"ERROR": "Too many people matched your criteria."}]
elements = resp.html.xpath("/html/div/section")
result = []
for entry in elements:
name, *segments = entry.find("span")
person = {"name": name.text}
_parse_segments(person, segments)
result.append(person)
if result == []:
return [{"ERROR": "No one found."}] # Return an error
if not result:
return [{"ERROR": "No one found."}]
return result

0 comments on commit 8ba515b

Please sign in to comment.