Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mypy errors in people.py #193

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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