Skip to content

Commit d10c6ee

Browse files
authored
Merge pull request #37 from b3yc0d3/feature/ai-filter
AI generated content can now be excluded from the search, by setting the ``exclude_ai`` parameter to **True** at the `rule34Py.search` method.
2 parents 9522998 + efef38d commit d10c6ee

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88

9-
## [unreleased] - 2025-08-24
9+
## [unreleased] - 2025-08-30
1010

1111
### Added
12-
- Added a `rule34.autocomplete` method.
12+
- Added a `rule34Py.autocomplete` method.
1313
- Added `AutocompleteTag` class.
14+
- Added parameter to `rule34Py.search` method for excluding ai generated content.
15+
- Added unit test
1416

1517
### Changed
1618
- Updated API wrapper to support website’s new authentication system.
1719
- The underlying website API now **requires authentication** (`api_key` and `user_id`) for all requests.
18-
- Updated unit tests
20+
- Updated unit tests.
1921

2022
## [3.0.0] - 2025-06-09
2123

rule34Py/rule34.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ def _get(self, *args, **kwargs) -> requests.Response:
146146

147147
# check if api credentials are set
148148
if is_api_request and self.user_id == None and self.api_key == None:
149-
raise ValueError("API credentials must be supplied, api_key and user_id can not be None!\nSee https://api.rule34.xxx/ for more information.")
149+
raise ValueError(
150+
"API credentials must be supplied, api_key and user_id can not be None!\nSee https://api.rule34.xxx/ for more information."
151+
)
150152

151153
# headers
152154
kwargs.setdefault("headers", {})
@@ -361,13 +363,16 @@ def random_post_id(self) -> int:
361363
def search(
362364
self,
363365
tags: list[str] = [],
366+
exclude_ai: bool = False,
364367
page_id: Union[int, None] = None,
365368
limit: int = SEARCH_RESULT_MAX,
366369
) -> list[Post]:
367370
"""Search for posts.
368371
369372
Args:
370373
tags: A list of tags to search for.
374+
exclude_ai: Exclude ai generated content from the results.
375+
Default is False.
371376
page_id: The search page number to request, or None.
372377
If None, search will eventually return all pages.
373378
limit: The maximum number of post results to return per page.
@@ -386,6 +391,12 @@ def search(
386391
if limit < 0 or limit > SEARCH_RESULT_MAX:
387392
raise ValueError(f"Search limit must be between 0 and {SEARCH_RESULT_MAX}.")
388393

394+
# exclude all tags starting with ai if user whishes so
395+
if exclude_ai == True:
396+
# filter out any ai tag
397+
tags = [tag for tag in tags if not tag.lower().startswith("ai")]
398+
tags.append("-ai*")
399+
389400
params = [
390401
["TAGS", "+".join(tags)],
391402
["LIMIT", str(limit)],

tests/fixtures/mock34/responses.yml

Lines changed: 13 additions & 0 deletions
Large diffs are not rendered by default.

tests/unit/test_rule34Py.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ def test_rule34Py_search(rule34):
207207
with pytest.raises(ValueError):
208208
rule34.search([], limit=SEARCH_RESULT_MAX + 1)
209209

210+
def test_rule34Py_search_exclude_ai(rule34):
211+
"""The client can search for posts by tags, with excluding ai generated content."""
212+
# search by single tag
213+
results1 = rule34.search(["neko"], exclude_ai=True)
214+
ids1 = [post.id for post in results1]
215+
print(f"ids1={ids1[:10]}...")
216+
assert isinstance(results1, list) # return type is list
217+
218+
assert len(results1) == SEARCH_RESULT_MAX
219+
assert isinstance(results1[0], Post) # return list contains Post objects
220+
210221

211222
def test_rule34Py_tag_map(rule34):
212223
"""The client tag_map() method should return a map of tags.

0 commit comments

Comments
 (0)