Skip to content
Open
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
44 changes: 40 additions & 4 deletions AO3/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@
DESCENDING = "desc"
ASCENDING = "asc"

# Category IDs
CATEGORY_FF = 116
CATEGORY_FM = 22
CATEGORY_GEN = 21
CATEGORY_MM = 23
CATEGORY_MULTI = 2246
CATEGORY_OTHER = 24

# Warning IDs
WARNING_CHOOSE_NOT_TO_USE = 14
WARNING_VIOLENCE = 17
WARNING_DEATH = 18
WARNING_NONE = 16
WARNING_NONCON = 19
WARNING_UNDERAGE = 20


class Search:
def __init__(
Expand All @@ -51,7 +67,10 @@ def __init__(
characters="",
relationships="",
tags="",
session=None):
session=None,
category_ids=None,
warning_ids=None,
):

self.any_field = any_field
self.title = title
Expand All @@ -75,7 +94,9 @@ def __init__(
self.sort_column = sort_column
self.sort_direction = sort_direction
self.revised_at = revised_at

self.category_ids = category_ids
self.warning_ids = warning_ids

self.session = session

self.results = None
Expand All @@ -93,7 +114,7 @@ def update(self):
self.word_count, self.language, self.fandoms, self.rating, self.hits,
self.kudos, self.crossovers, self.bookmarks, self.excluded_tags, self.comments, self.completion_status, self.page,
self.sort_column, self.sort_direction, self.revised_at, self.session,
self.characters, self.relationships, self.tags)
self.characters, self.relationships, self.tags, self.category_ids, self.warning_ids)

results = soup.find("ol", {"class": ("work", "index", "group")})
if results is None and soup.find("p", text="No results found. You may want to edit your search to make it less specific.") is not None:
Expand Down Expand Up @@ -139,7 +160,10 @@ def search(
session=None,
characters="",
relationships="",
tags=""):
tags="",
category_ids=None,
warning_ids=None,
):
"""Returns the results page for the search as a Soup object

Args:
Expand Down Expand Up @@ -214,6 +238,18 @@ def search(
query.add_field(f"work_search[sort_direction]={sort_direction}")
if revised_at != "":
query.add_field(f"work_search[revised_at]={revised_at}")
if category_ids is not None:
if isinstance(category_ids, (list, tuple)):
for cat in category_ids:
query.add_field(f"work_search[category_ids][]={cat}")
else:
query.add_field(f"work_search[category_ids][]={category_ids}")
if warning_ids is not None:
if isinstance(warning_ids, (list, tuple)):
for warning in warning_ids:
query.add_field(f"work_search[archive_warning_ids][]={warning}")
else:
query.add_field(f"work_search[archive_warning_ids][]={warning_ids}")

url = f"https://archiveofourown.org/works/search?{query.string}"

Expand Down