From 9d3b5150798fdebb958d0411cfb324d4d177ba2e Mon Sep 17 00:00:00 2001 From: Danipulok Date: Tue, 14 Jan 2025 00:32:40 +0200 Subject: [PATCH 1/4] feat(enums): add enums --- demo.py | 12 ++-- download_illusts.py | 4 +- example_bypass_sni.py | 4 +- example_tag_translations.py | 4 +- pixivpy3/aapi.py | 114 +++++++++++++++--------------------- pixivpy3/enums.py | 55 +++++++++++++++++ 6 files changed, 117 insertions(+), 76 deletions(-) create mode 100644 pixivpy3/enums.py diff --git a/demo.py b/demo.py index d014e73..2298eef 100644 --- a/demo.py +++ b/demo.py @@ -3,7 +3,7 @@ import sys import time -from pixivpy3 import AppPixivAPI, PixivError +from pixivpy3 import AppPixivAPI, PixivError, enums sys.dont_write_bytecode = True @@ -131,7 +131,7 @@ def appapi_search(aapi): first_tag = trend_tag.tag print("{} - {}(id={})".format(trend_tag.tag, trend_tag.illust.title, trend_tag.illust.id)) - json_result = aapi.search_illust(first_tag, search_target="partial_match_for_tags") + json_result = aapi.search_illust(first_tag, search_target=enums.SearchTarget.PARTIAL_MATCH_FOR_TAGS) print(json_result) illust = json_result.illusts[0] print(">>> {}, origin url: {}".format(illust.title, illust.image_urls["large"])) @@ -145,7 +145,7 @@ def appapi_search(aapi): print(">>> {}, origin url: {}".format(illust.title, illust.image_urls["large"])) # novel - json_result = aapi.search_novel("FGO", search_target="keyword") + json_result = aapi.search_novel("FGO", search_target=enums.SearchTarget.KEYWORD) print(json_result) novel = json_result.novels[0] print(">>> {}, origin url: {}".format(novel.title, novel.image_urls["large"])) @@ -158,7 +158,11 @@ def appapi_search(aapi): novel = json_result.novels[0] print(">>> {}, origin url: {}".format(novel.title, novel.image_urls["large"])) - json_result = aapi.search_illust("AI生成", search_target="exact_match_for_tags", search_ai_type=0) + json_result = aapi.search_illust( + "AI生成", + search_target=enums.SearchTarget.EXACT_MATCH_FOR_TAGS, + search_ai_type=0, + ) # 关闭AI搜索选项后,将过滤掉所有illust_ai_type=2的插画,而illust_ai_type=1 or 0 的插画将被保留 # 但是,加入了"AI生成"的tag却没有在作品提交时打开“AI生成”的开关的作品不会被筛选出结果列表 print(json_result["illusts"][0]) diff --git a/download_illusts.py b/download_illusts.py index 3a75755..a06e3fa 100644 --- a/download_illusts.py +++ b/download_illusts.py @@ -3,7 +3,7 @@ import os import sys -from pixivpy3 import AppPixivAPI, ByPassSniApi +from pixivpy3 import AppPixivAPI, ByPassSniApi, enums sys.dont_write_bytecode = True @@ -23,7 +23,7 @@ def main(): api.auth(refresh_token=_REFRESH_TOKEN) # get rankings - json_result = api.illust_ranking("day", date="2019-01-01") + json_result = api.illust_ranking(enums.RankingMode.DAY, date="2019-01-01") directory = "illusts" if not os.path.exists(directory): diff --git a/example_bypass_sni.py b/example_bypass_sni.py index 3fd3b53..74eda30 100644 --- a/example_bypass_sni.py +++ b/example_bypass_sni.py @@ -1,7 +1,7 @@ import sys from datetime import datetime, timedelta -from pixivpy3 import ByPassSniApi +from pixivpy3 import ByPassSniApi, enums sys.dont_write_bytecode = True @@ -19,7 +19,7 @@ def main(): # api.login(_USERNAME, _PASSWORD) print(api.auth(refresh_token=_REFRESH_TOKEN)) - json_result = api.illust_ranking("day", date=(datetime.now() - timedelta(days=5)).strftime("%Y-%m-%d")) + json_result = api.illust_ranking(enums.RankingMode.DAY, date=(datetime.now() - timedelta(days=5)).strftime("%Y-%m-%d")) print("Printing image titles and tags with English tag translations present when available") diff --git a/example_tag_translations.py b/example_tag_translations.py index cea5bf7..d347068 100644 --- a/example_tag_translations.py +++ b/example_tag_translations.py @@ -3,7 +3,7 @@ import sys from datetime import datetime, timedelta -from pixivpy3 import AppPixivAPI +from pixivpy3 import AppPixivAPI, enums sys.dont_write_bytecode = True @@ -19,7 +19,7 @@ def main(): aapi.set_accept_language("en-us") # zh-cn aapi.auth(refresh_token=_REFRESH_TOKEN) - json_result = aapi.illust_ranking("day", date=(datetime.now() - timedelta(days=5)).strftime("%Y-%m-%d")) + json_result = aapi.illust_ranking(enums.RankingMode.DAY, date=(datetime.now() - timedelta(days=5)).strftime("%Y-%m-%d")) print("Printing image titles and tags with English tag translations present when available") diff --git a/pixivpy3/aapi.py b/pixivpy3/aapi.py index a2d4d6f..331a436 100644 --- a/pixivpy3/aapi.py +++ b/pixivpy3/aapi.py @@ -20,36 +20,18 @@ from requests.structures import CaseInsensitiveDict +from . import enums from .api import BasePixivAPI from .utils import ParamDict, ParsedJson, PixivError, Response # from typeguard import typechecked -_FILTER: TypeAlias = Literal["for_ios", ""] -_TYPE: TypeAlias = Literal["illust", "manga", ""] -_RESTRICT: TypeAlias = Literal["public", "private", ""] -_CONTENT_TYPE: TypeAlias = Literal["illust", "manga", ""] -_MODE: TypeAlias = Literal[ - "day", - "week", - "month", - "day_male", - "day_female", - "week_original", - "week_rookie", - "day_manga", - "day_r18", - "day_male_r18", - "day_female_r18", - "week_r18", - "week_r18g", - "", -] -_SEARCH_TARGET: TypeAlias = Literal["partial_match_for_tags", "exact_match_for_tags", "title_and_caption", "keyword", ""] -_SORT: TypeAlias = Literal["date_desc", "date_asc", "popular_desc", ""] -_DURATION: TypeAlias = Literal["within_last_day", "within_last_week", "within_last_month", "", None] -_BOOL: TypeAlias = Literal["true", "false"] +__all__ = ("AppPixivAPI",) + +BoolStr: TypeAlias = Literal["true", "false"] +Filter: TypeAlias = Literal["for_ios"] +FILTER = "for_ios" # App-API (6.x - app-api.pixiv.net) @@ -98,7 +80,7 @@ def parse_result(self, res: Response) -> ParsedJson: raise PixivError("parse_json() error: %s" % e, header=res.headers, body=res.text) @classmethod - def format_bool(cls, bool_value: bool | str | None) -> _BOOL: + def format_bool(cls, bool_value: bool | str | None) -> BoolStr: if isinstance(bool_value, bool): return "true" if bool_value else "false" if bool_value in {"true", "True"}: @@ -129,7 +111,7 @@ def parse_qs(cls, next_url: str | None) -> dict[str, Any] | None: def user_detail( self, user_id: int | str, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, req_auth: bool = True, ) -> ParsedJson: url = "%s/v1/user/detail" % self.hosts @@ -145,8 +127,8 @@ def user_detail( def user_illusts( self, user_id: int | str, - type: _TYPE = "illust", - filter: _FILTER = "for_ios", + type: enums.ContentType = enums.ContentType.ILLUSTRATION, + filter: Filter = FILTER, offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -167,8 +149,8 @@ def user_illusts( def user_bookmarks_illust( self, user_id: int | str, - restrict: _RESTRICT = "public", - filter: _FILTER = "for_ios", + restrict: enums.Visibility = enums.Visibility.PUBLIC, + filter: Filter = FILTER, max_bookmark_id: int | str | None = None, tag: str | None = None, req_auth: bool = True, @@ -190,8 +172,8 @@ def user_bookmarks_illust( def user_bookmarks_novel( self, user_id: int | str, - restrict: _RESTRICT = "public", - filter: _FILTER = "for_ios", + restrict: enums.Visibility = enums.Visibility.PUBLIC, + filter: Filter = FILTER, max_bookmark_id: int | str | None = None, tag: str | None = None, req_auth: bool = True, @@ -212,7 +194,7 @@ def user_bookmarks_novel( def user_related( self, seed_user_id: int | str, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -228,7 +210,7 @@ def user_related( def user_recommended( self, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -246,7 +228,7 @@ def user_recommended( # restrict: [public, private] def illust_follow( self, - restrict: _RESTRICT = "public", + restrict: enums.Visibility = enums.Visibility.PUBLIC, offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -291,7 +273,7 @@ def illust_comments( def illust_related( self, illust_id: int | str, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, seed_illust_ids: int | str | list[str] | None = None, offset: int | str | None = None, viewed: str | list[str] | None = None, @@ -318,9 +300,9 @@ def illust_related( # content_type: [illust, manga] def illust_recommended( self, - content_type: _CONTENT_TYPE = "illust", + content_type: enums.ContentType = enums.ContentType.ILLUSTRATION, include_ranking_label: bool | str = True, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, max_bookmark_id_for_recommend: int | str | None = None, min_bookmark_id_for_recent_illust: int | str | None = None, offset: int | str | None = None, @@ -388,7 +370,7 @@ def novel_comments( def novel_recommended( self, include_ranking_label: bool | str = True, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, offset: int | str | None = None, include_ranking_novels: str | bool | None = None, already_recommended: str | list[str] | None = None, @@ -425,8 +407,8 @@ def novel_recommended( # day_r18, day_male_r18, day_female_r18, week_r18, week_r18g] def illust_ranking( self, - mode: _MODE = "day", - filter: _FILTER = "for_ios", + mode: enums.RankingMode = enums.RankingMode.DAY, + filter: Filter = FILTER, date: str | None = None, offset: int | str | None = None, req_auth: bool = True, @@ -444,7 +426,7 @@ def illust_ranking( return self.parse_result(r) # 趋势标签 (Search - tags) - def trending_tags_illust(self, filter: _FILTER = "for_ios", req_auth: bool = True) -> ParsedJson: + def trending_tags_illust(self, filter: Filter = FILTER, req_auth: bool = True) -> ParsedJson: url = "%s/v1/trending-tags/illust" % self.hosts params = { "filter": filter, @@ -464,12 +446,12 @@ def trending_tags_illust(self, filter: _FILTER = "for_ios", req_auth: bool = Tru def search_illust( self, word: str, - search_target: _SEARCH_TARGET = "partial_match_for_tags", - sort: _SORT = "date_desc", - duration: _DURATION = None, + search_target: enums.SearchTarget = enums.SearchTarget.PARTIAL_MATCH_FOR_TAGS, + sort: enums.Sort = enums.Sort.DATE_DESC, + duration: enums.Duration | None = None, start_date: str | None = None, end_date: str | None = None, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, search_ai_type: Literal[0, 1] | None = None, offset: int | str | None = None, req_auth: bool = True, @@ -506,10 +488,10 @@ def search_illust( def search_novel( self, word: str, - search_target: _SEARCH_TARGET = "partial_match_for_tags", - sort: _SORT = "date_desc", - merge_plain_keyword_results: _BOOL = "true", - include_translated_tag_results: _BOOL = "true", + search_target: enums.SearchTarget = enums.SearchTarget.PARTIAL_MATCH_FOR_TAGS, + sort: enums.Sort = enums.Sort.DATE_DESC, + merge_plain_keyword_results: BoolStr = "true", + include_translated_tag_results: BoolStr = "true", start_date: str | None = None, end_date: str | None = None, filter: str | None = None, @@ -540,9 +522,9 @@ def search_novel( def search_user( self, word: str, - sort: _SORT = "date_desc", - duration: _DURATION = None, - filter: _FILTER = "for_ios", + sort: enums.Sort = enums.Sort.DATE_DESC, + duration: enums.Duration | None = None, + filter: Filter = FILTER, offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -572,7 +554,7 @@ def illust_bookmark_detail(self, illust_id: int | str, req_auth: bool = True) -> def illust_bookmark_add( self, illust_id: int | str, - restrict: _RESTRICT = "public", + restrict: enums.Visibility = enums.Visibility.PUBLIC, tags: str | list[str] | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -602,7 +584,7 @@ def illust_bookmark_delete(self, illust_id: int | str, req_auth: bool = True) -> def user_follow_add( self, user_id: int | str, - restrict: _RESTRICT = "public", + restrict: enums.Visibility = enums.Visibility.PUBLIC, req_auth: bool = True, ) -> ParsedJson: url = "%s/v1/user/follow/add" % self.hosts @@ -618,7 +600,7 @@ def user_follow_delete(self, user_id: int | str, req_auth: bool = True) -> Parse return self.parse_result(r) # 设置用户选项中是否展现AI生成作品 - def user_edit_ai_show_settings(self, setting: _BOOL, req_auth: bool = True) -> ParsedJson: + def user_edit_ai_show_settings(self, setting: BoolStr, req_auth: bool = True) -> ParsedJson: url = "%s/v1/user/ai-show-settings/edit" % self.hosts data = {"show_ai": setting} r = self.no_auth_requests_call("POST", url, data=data, req_auth=req_auth) @@ -628,7 +610,7 @@ def user_edit_ai_show_settings(self, setting: _BOOL, req_auth: bool = True) -> P def user_bookmark_tags_illust( self, user_id: int | str, - restrict: _RESTRICT = "public", + restrict: enums.Visibility = enums.Visibility.PUBLIC, offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -646,7 +628,7 @@ def user_bookmark_tags_illust( def user_following( self, user_id: int | str, - restrict: _RESTRICT = "public", + restrict: enums.Visibility = enums.Visibility.PUBLIC, offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -665,7 +647,7 @@ def user_following( def user_follower( self, user_id: int | str, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -701,7 +683,7 @@ def user_mypixiv( def user_list( self, user_id: int | str, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -730,7 +712,7 @@ def ugoira_metadata(self, illust_id: int | str, req_auth: bool = True) -> Parsed def user_novels( self, user_id: int | str, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -748,7 +730,7 @@ def user_novels( def novel_series( self, series_id: int | str, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, last_order: str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -774,7 +756,7 @@ def novel_detail(self, novel_id: int | str, req_auth: bool = True) -> ParsedJson def novel_new( self, - filter: _FILTER = "for_ios", + filter: Filter = FILTER, max_novel_id: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -791,7 +773,7 @@ def novel_new( # restrict: [public, private, all] def novel_follow( self, - restrict: _RESTRICT = "public", + restrict: enums.Visibility = enums.Visibility.PUBLIC, offset: int | None = None, req_auth: bool = True, ) -> ParsedJson: @@ -831,8 +813,8 @@ def novel_text(self, novel_id: int | str, req_auth: bool = True) -> ParsedJson: # content_type: [illust, manga] def illust_new( self, - content_type: _CONTENT_TYPE = "illust", - filter: _FILTER = "for_ios", + content_type: enums.ContentType = enums.ContentType.ILLUSTRATION, + filter: Filter = FILTER, max_illust_id: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: diff --git a/pixivpy3/enums.py b/pixivpy3/enums.py new file mode 100644 index 0000000..eccffeb --- /dev/null +++ b/pixivpy3/enums.py @@ -0,0 +1,55 @@ +from enum import Enum + +__all__ = ( + "ContentType", + "Visibility", + "RankingMode", + "SearchTarget", + "Sort", + "Duration", +) + + +class ContentType(str, Enum): + ILLUSTRATION = "illust" + MANGA = "manga" + + +class Visibility(str, Enum): + PUBLIC = "public" + PRIVATE = "private" + + +class RankingMode(str, Enum): + DAY = "day" + WEEK = "week" + MONTH = "month" + DAY_MALE = "day_male" + DAY_FEMALE = "day_female" + WEEK_ORIGINAL = "week_original" + WEEK_ROOKIE = "week_rookie" + DAY_MANGA = "day_manga" + DAY_R18 = "day_r18" + DAY_MALE_R18 = "day_male_r18" + DAY_FEMALE_R18 = "day_female_r18" + WEEK_R18 = "week_r18" + WEEK_R18G = "week_r18g" + + +class SearchTarget(str, Enum): + PARTIAL_MATCH_FOR_TAGS = "partial_match_for_tags" + EXACT_MATCH_FOR_TAGS = "exact_match_for_tags" + TITLE_AND_CAPTION = "title_and_caption" + KEYWORD = "keyword" + + +class Sort(str, Enum): + DATE_DESC = "date_desc" + DATE_ASC = "date_asc" + POPULAR_DESC = "popular_desc" + + +class Duration(str, Enum): + WITHIN_LAST_DAY = "within_last_day" + WITHIN_LAST_WEEK = "within_last_week" + WITHIN_LAST_MONTH = "within_last_month" From e963442632c91dfb4593a31f8153ad73a6524936 Mon Sep 17 00:00:00 2001 From: Danipulok Date: Tue, 14 Jan 2025 00:42:53 +0200 Subject: [PATCH 2/4] feat(enums): use enums in `README.md` --- README.md | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1b82d30..74825b8 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,7 @@ Requirements: [requests](https://pypi.python.org/pypi/requests) ```python from pixivpy3 import * +from pixivpy3 import enums api = AppPixivAPI() # api.login("username", "password") # Not required @@ -131,7 +132,7 @@ print(">>> origin url: %s" % illust.image_urls['large']) # get ranking: 1-30 # mode: [day, week, month, day_male, day_female, week_original, week_rookie, day_manga] -json_result = api.illust_ranking('day') +json_result = api.illust_ranking(mode=enums.RankingMode.DAY) for illust in json_result.illusts: print(" p1 [%s] %s" % (illust.title, illust.image_urls.medium)) @@ -199,6 +200,9 @@ Find Pixiv API in **Objective-C**? You might also like ### App-API (6.0 - app-api.pixiv.net) ```python +from pixivpy3 import enums + + class AppPixivAPI(BasePixivAPI): # 返回翻页用参数 @@ -209,20 +213,20 @@ class AppPixivAPI(BasePixivAPI): # 用户作品列表 ## type: [illust, manga] - def user_illusts(user_id, type="illust"): + def user_illusts(user_id, type=enums.ContentType.ILLUSTRATION): # 用户收藏作品列表 # tag: 从 user_bookmark_tags_illust 获取的收藏标签 - def user_bookmarks_illust(user_id, restrict="public"): + def user_bookmarks_illust(user_id, restrict=enums.Visibility.PUBLIC): # 用户收藏作品列表中的小说 - def user_bookmarks_novel(user_id, restrict="public"): + def user_bookmarks_novel(user_id, restrict=enums.Visibility.PUBLIC): def user_related(seed_user_id): # 关注用户的新作 # restrict: [public, private] - def illust_follow(restrict="public"): + def illust_follow(restrict=enums.Visibility.PUBLIC): # 作品详情 (类似PAPI.works(),iOS中未使用) def illust_detail(illust_id): @@ -235,7 +239,7 @@ class AppPixivAPI(BasePixivAPI): # 插画推荐 (Home - Main) # content_type: [illust, manga] - def illust_recommended(content_type="illust"): + def illust_recommended(content_type=enums.ContentType.ILLUSTRATION): # 小说推荐 def novel_recommended(): @@ -245,7 +249,7 @@ class AppPixivAPI(BasePixivAPI): # date: '2016-08-01' # mode (Past): [day, week, month, day_male, day_female, week_original, week_rookie, # day_r18, day_male_r18, day_female_r18, week_r18, week_r18g] - def illust_ranking(mode="day", date=None): + def illust_ranking(mode=enums.RankingMode.DAY, date=None): # 趋势标签 (Search - tags) def trending_tags_illust(): @@ -258,7 +262,14 @@ class AppPixivAPI(BasePixivAPI): # sort: [date_desc, date_asc, popular_desc] - popular_desc为会员的热门排序 # duration: [within_last_day, within_last_week, within_last_month] # start_date, end_date: '2020-07-01' - def search_illust(word, search_target="partial_match_for_tags", sort="date_desc", duration=None, start_date=None, end_date=None): + def search_illust( + word, + search_target=enums.SearchTarget.PARTIAL_MATCH_FOR_TAGS, + sort=enums.Sort.DATE_DESC, + duration=None, + start_date=None, + end_date=None, + ): # 搜索小说 (Search Novel) # search_target - 搜索类型 @@ -268,30 +279,36 @@ class AppPixivAPI(BasePixivAPI): # keyword - 关键词 # sort: [date_desc, date_asc] # start_date/end_date: 2020-06-01 - def search_novel(word, search_target="partial_match_for_tags", sort="date_desc", start_date=None, end_date=None): + def search_novel( + word, + search_target=enums.SearchTarget.PARTIAL_MATCH_FOR_TAGS, + sort=enums.Sort.DATE_DESC, + start_date=None, + end_date=None, + ): - def search_user(word, sort='date_desc', duration=None): + def search_user(word, sort=enums.Sort.DATE_DESC, duration=None): # 作品收藏详情 def illust_bookmark_detail(illust_id): # 新增收藏 - def illust_bookmark_add(illust_id, restrict="public", tags=None): + def illust_bookmark_add(illust_id, restrict=enums.Visibility.PUBLIC, tags=None): # 删除收藏 def illust_bookmark_delete(illust_id): # 关注用户 - def user_follow_add(user_id, restrict="public"): + def user_follow_add(user_id, restrict=enums.Visibility.PUBLIC): # 取消关注用户 def user_follow_delete(user_id): # 用户收藏标签列表 - def user_bookmark_tags_illust(restrict="public"): + def user_bookmark_tags_illust(restrict=enums.Visibility.PUBLIC): # Following用户列表 - def user_following(user_id, restrict="public"): + def user_following(user_id, restrict=enums.Visibility.PUBLIC): # Followers用户列表 def user_follower(user_id): @@ -322,7 +339,7 @@ class AppPixivAPI(BasePixivAPI): # 大家的新作 # content_type: [illust, manga] - def illust_new(content_type="illust", max_illust_id=None): + def illust_new(content_type=enums.ContentType.ILLUSTRATION, max_illust_id=None): def novel_new(max_novel_id=None): @@ -333,6 +350,7 @@ class AppPixivAPI(BasePixivAPI): [Usage](https://github.com/upbit/pixivpy/blob/aec177aa7a1979f7ec4c5bbbeed9085cc256bdbd/demo.py#L306): ```python +from pixivpy3 import enums aapi = AppPixivAPI() # 作品推荐 @@ -391,7 +409,7 @@ illust = json_result.illusts[0] print(">>> %s, origin url: %s" % (illust.title, illust.image_urls['large'])) # 标签 "水着" 搜索 -json_result = aapi.search_illust('水着', search_target='partial_match_for_tags') +json_result = aapi.search_illust('水着', search_target=enums.SearchTarget.PARTIAL_MATCH_FOR_TAGS) print(json_result) illust = json_result.illusts[0] print(">>> %s, origin url: %s" % (illust.title, illust.image_urls['large'])) From 46a51330b0b869da239335d9c3973f92fde0d564 Mon Sep 17 00:00:00 2001 From: Danipulok Date: Tue, 14 Jan 2025 20:19:11 +0200 Subject: [PATCH 3/4] fix formatting --- README.md | 26 ++++++++++++++++---------- example_bypass_sni.py | 4 +++- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 296caf8..0cc74ef 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,7 @@ while next_qs: ```python from pixivpy3 import AppPixivAPI + aapi = AppPixivAPI() json_result = aapi.illust_ranking() for illust in json_result.illusts[:3]: @@ -177,9 +178,10 @@ for illust in json_result.illusts[:3]: 2. Change deprecated SPAI call to Public-API call ```python -from pixivpy3 import AppPixivAPI +from pixivpy3 import AppPixivAPI, enums + api = AppPixivAPI() -rank_list = api.illust_ranking('day') +rank_list = api.illust_ranking(enums.RankingMode.DAY) print(rank_list) # more fields about response: https://github.com/upbit/pixivpy/wiki/sniffer @@ -210,6 +212,7 @@ from pixivpy3 import enums from pixivpy3.utils import ParsedJson from pixivpy3.api import BasePixivAPI + class AppPixivAPI(BasePixivAPI): # 返回翻页用参数 @@ -288,12 +291,12 @@ class AppPixivAPI(BasePixivAPI): # sort: [date_desc, date_asc] # start_date/end_date: 2020-06-01 def search_novel( - self, - word: str, - search_target=enums.SearchTarget.PARTIAL_MATCH_FOR_TAGS, - sort=enums.Sort.DATE_DESC, - start_date=None, - end_date=None, + self, + word: str, + search_target=enums.SearchTarget.PARTIAL_MATCH_FOR_TAGS, + sort=enums.Sort.DATE_DESC, + start_date=None, + end_date=None, ) -> ParsedJson: ... def search_user(self, word: str, sort=enums.Sort.DATE_DESC, duration=None) -> ParsedJson: ... @@ -360,6 +363,7 @@ class AppPixivAPI(BasePixivAPI): ```python from pixivpy3 import AppPixivAPI, enums + api = AppPixivAPI() # 作品推荐 @@ -434,9 +438,11 @@ json_result = api.novel_comments(16509454, include_total_comments=True) print(f"Total comments = {json_result.total_comments}") for comment in json_result.comments: if comment.parent_comment: - print(f"{comment.user.name} replied to {comment.parent_comment.user.name} at {comment.date} : {comment.comment}") + text = f"{comment.user.name} replied to {comment.parent_comment.user.name} at {comment.date} : {comment.comment}" + print(text) else: - print(f"{comment.user.name} at {comment.date} : {comment.comment}") + text = f"{comment.user.name} at {comment.date} : {comment.comment}" + print(text) ``` ## Package Publishing Instructions diff --git a/example_bypass_sni.py b/example_bypass_sni.py index 74eda30..2dfdda6 100644 --- a/example_bypass_sni.py +++ b/example_bypass_sni.py @@ -19,7 +19,9 @@ def main(): # api.login(_USERNAME, _PASSWORD) print(api.auth(refresh_token=_REFRESH_TOKEN)) - json_result = api.illust_ranking(enums.RankingMode.DAY, date=(datetime.now() - timedelta(days=5)).strftime("%Y-%m-%d")) + date = datetime.now() - timedelta(days=5) + date_str = date.strftime("%Y-%m-%d") + json_result = api.illust_ranking(enums.RankingMode.DAY, date=date_str) print("Printing image titles and tags with English tag translations present when available") From 1ecfcfe78e68fdf2cbdea0cf6b033f7028990c03 Mon Sep 17 00:00:00 2001 From: Danipulok Date: Tue, 14 Jan 2025 20:22:32 +0200 Subject: [PATCH 4/4] fix missing enums --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0cc74ef..1c41484 100644 --- a/README.md +++ b/README.md @@ -410,7 +410,7 @@ novel = json_result.novels[0] print(f">>> {novel.title}, text_length: {novel.text_length}, series: {novel.series}") # 2016-07-15 日的过去一周排行 -json_result = api.illust_ranking('week', date='2016-07-15') +json_result = api.illust_ranking(enums.RankingMode.WEEK, date='2016-07-15') print(json_result) illust = json_result.illusts[0] print(f">>> {illust.title}, origin url: {illust.image_urls.large}")