diff --git a/pixivpy3/aapi.py b/pixivpy3/aapi.py index f6ffd24..639e9e7 100644 --- a/pixivpy3/aapi.py +++ b/pixivpy3/aapi.py @@ -69,24 +69,30 @@ def set_api_proxy(self, proxy_hosts: str = "http://app-api.pixivlite.com") -> No """Set proxy hosts: eg pixivlite.com""" self.hosts = proxy_hosts + def _url(self, endpoint: str) -> str: + return f"{self.hosts}{endpoint}" + # Check auth and set BearerToken to headers def no_auth_requests_call( self, - method: str, + method: Literal["GET", "POST"], url: str, headers: ParamDict = None, params: ParamDict = None, data: ParamDict = None, req_auth: bool = True, ) -> Response: + # `mypy` doesn't allow type overwriting, must add `_` suffix headers_ = CaseInsensitiveDict(headers or {}) + if self.hosts != "https://app-api.pixiv.net": - headers_["host"] = "app-api.pixiv.net" - if "user-agent" not in headers_: + headers_["Host"] = "app-api.pixiv.net" + + if "User-Agent" not in headers_: # Set User-Agent if not provided - headers_["app-os"] = "ios" - headers_["app-os-version"] = "14.6" - headers_["user-agent"] = "PixivIOSApp/7.13.3 (iOS 14.6; iPhone13,2)" + headers_["App-OS"] = "ios" + headers_["App-OS-Version"] = "14.6" + headers_["User-Agent"] = "PixivIOSApp/7.13.3 (iOS 14.6; iPhone13,2)" if not req_auth: return self.requests_call(method, url, headers_, params, data) @@ -160,7 +166,7 @@ def user_detail( filter: _FILTER = "for_ios", req_auth: bool = True, ) -> models.UserInfoDetailed: - url = f"{self.hosts}/v1/user/detail" + url = self._url("/v1/user/detail") params = { "user_id": user_id, "filter": filter, @@ -178,7 +184,7 @@ def user_illusts( offset: int | str | None = None, req_auth: bool = True, ) -> models.UserIllustrations: - url = f"{self.hosts}/v1/user/illusts" + url = self._url("/v1/user/illusts") params = { "user_id": user_id, "filter": filter, @@ -201,7 +207,7 @@ def user_bookmarks_illust( tag: str | None = None, req_auth: bool = True, ) -> models.UserBookmarksIllustrations: - url = f"{self.hosts}/v1/user/bookmarks/illust" + url = self._url("/v1/user/bookmarks/illust") params = { "user_id": user_id, "restrict": restrict, @@ -224,7 +230,7 @@ def user_bookmarks_novel( tag: str | None = None, req_auth: bool = True, ) -> models.UserBookmarksNovel: - url = f"{self.hosts}/v1/user/bookmarks/novel" + url = self._url("/v1/user/bookmarks/novel") params = { "user_id": user_id, "restrict": restrict, @@ -244,7 +250,7 @@ def user_related( offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/user/related" + url = self._url("/v1/user/related") params = { "filter": filter, # Pixiv warns to put seed_user_id at the end -> put offset here @@ -260,7 +266,7 @@ def user_recommended( offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/user/recommended" + url = self._url("/v1/user/recommended") params = { "filter": filter, } @@ -278,7 +284,7 @@ def illust_follow( offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v2/illust/follow" + url = self._url("/v2/illust/follow") params: dict[str, str | int] = { "restrict": restrict, } @@ -289,7 +295,7 @@ def illust_follow( # 作品详情 (类似PAPI.works(),iOS中未使用) def illust_detail(self, illust_id: int | str, req_auth: bool = True) -> ParsedJson: - url = f"{self.hosts}/v1/illust/detail" + url = self._url("/v1/illust/detail") params = { "illust_id": illust_id, } @@ -304,7 +310,7 @@ def illust_comments( include_total_comments: str | bool | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/illust/comments" + url = self._url("/v1/illust/comments") params = { "illust_id": illust_id, } @@ -325,7 +331,7 @@ def illust_related( viewed: str | list[str] | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v2/illust/related" + url = self._url("/v2/illust/related") params: dict[str, Any] = { "illust_id": illust_id, "filter": filter, @@ -359,9 +365,9 @@ def illust_recommended( req_auth: bool = True, ) -> ParsedJson: if req_auth: - url = f"{self.hosts}/v1/illust/recommended" + url = self._url("/v1/illust/recommended") else: - url = f"{self.hosts}/v1/illust/recommended-nologin" + url = self._url("/v1/illust/recommended-nologin") params: dict[str, Any] = { "content_type": content_type, "include_ranking_label": self.format_bool(include_ranking_label), @@ -405,7 +411,7 @@ def novel_comments( include_total_comments: str | bool | None = None, req_auth: bool = True, ) -> models.NovelComments: - url = f"{self.hosts}/v1/novel/comments" + url = self._url("/v1/novel/comments") params = { "novel_id": novel_id, } @@ -428,7 +434,7 @@ def novel_recommended( include_privacy_policy: str | list[int | str] | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/novel/recommended" + url = self._url("/v1/novel/recommended") params: dict[str, Any] = { "include_ranking_label": self.format_bool(include_ranking_label), "filter": filter, @@ -465,7 +471,7 @@ def illust_ranking( offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/illust/ranking" + url = self._url("/v1/illust/ranking") params: dict[str, Any] = { "mode": mode, "filter": filter, @@ -481,7 +487,7 @@ def illust_ranking( def trending_tags_illust( self, filter: _FILTER = "for_ios", req_auth: bool = True ) -> ParsedJson: - url = f"{self.hosts}/v1/trending-tags/illust" + url = self._url("/v1/trending-tags/illust") params = { "filter": filter, } @@ -510,7 +516,7 @@ def search_illust( offset: int | str | None = None, req_auth: bool = True, ) -> models.SearchIllustrations: - url = f"{self.hosts}/v1/search/illust" + url = self._url("/v1/search/illust") params: dict[str, Any] = { "word": word, "search_target": search_target, @@ -553,7 +559,7 @@ def search_novel( offset: int | str | None = None, req_auth: bool = True, ) -> models.SearchNovel: - url = f"{self.hosts}/v1/search/novel" + url = self._url("/v1/search/novel") params: dict[str, Any] = { "word": word, "search_target": search_target, @@ -582,7 +588,7 @@ def search_user( offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/search/user" + url = self._url("/v1/search/user") params: dict[str, Any] = { "word": word, "sort": sort, @@ -599,7 +605,7 @@ def search_user( def illust_bookmark_detail( self, illust_id: int | str, req_auth: bool = True ) -> ParsedJson: - url = f"{self.hosts}/v2/illust/bookmark/detail" + url = self._url("/v2/illust/bookmark/detail") params = { "illust_id": illust_id, } @@ -614,7 +620,7 @@ def illust_bookmark_add( tags: str | list[str] | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v2/illust/bookmark/add" + url = self._url("/v2/illust/bookmark/add") data = { "illust_id": illust_id, "restrict": restrict, @@ -631,7 +637,7 @@ def illust_bookmark_add( def illust_bookmark_delete( self, illust_id: int | str, req_auth: bool = True ) -> ParsedJson: - url = f"{self.hosts}/v1/illust/bookmark/delete" + url = self._url("/v1/illust/bookmark/delete") data = { "illust_id": illust_id, } @@ -645,7 +651,7 @@ def user_follow_add( restrict: _RESTRICT = "public", req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/user/follow/add" + url = self._url("/v1/user/follow/add") data = {"user_id": user_id, "restrict": restrict} r = self.no_auth_requests_call("POST", url, data=data, req_auth=req_auth) return self.parse_result(r) @@ -654,7 +660,7 @@ def user_follow_add( def user_follow_delete( self, user_id: int | str, req_auth: bool = True ) -> ParsedJson: - url = f"{self.hosts}/v1/user/follow/delete" + url = self._url("/v1/user/follow/delete") data = {"user_id": user_id} r = self.no_auth_requests_call("POST", url, data=data, req_auth=req_auth) return self.parse_result(r) @@ -663,7 +669,7 @@ def user_follow_delete( def user_edit_ai_show_settings( self, setting: _BOOL, req_auth: bool = True ) -> ParsedJson: - url = f"{self.hosts}/v1/user/ai-show-settings/edit" + url = self._url("/v1/user/ai-show-settings/edit") data = {"show_ai": setting} r = self.no_auth_requests_call("POST", url, data=data, req_auth=req_auth) return self.parse_result(r) @@ -676,7 +682,7 @@ def user_bookmark_tags_illust( offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/user/bookmark-tags/illust" + url = self._url("/v1/user/bookmark-tags/illust") params: dict[str, Any] = { "user_id": user_id, "restrict": restrict, @@ -694,7 +700,7 @@ def user_following( offset: int | str | None = None, req_auth: bool = True, ) -> models.UserFollowing: - url = f"{self.hosts}/v1/user/following" + url = self._url("/v1/user/following") params = { "user_id": user_id, "restrict": restrict, @@ -713,7 +719,7 @@ def user_follower( offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/user/follower" + url = self._url("/v1/user/follower") params = { "user_id": user_id, "filter": filter, @@ -731,7 +737,7 @@ def user_mypixiv( offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/user/mypixiv" + url = self._url("/v1/user/mypixiv") params = { "user_id": user_id, } @@ -749,7 +755,7 @@ def user_list( offset: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v2/user/list" + url = self._url("/v2/user/list") params = { "user_id": user_id, "filter": filter, @@ -764,7 +770,7 @@ def user_list( def ugoira_metadata( self, illust_id: int | str, req_auth: bool = True ) -> ParsedJson: - url = f"{self.hosts}/v1/ugoira/metadata" + url = self._url("/v1/ugoira/metadata") params = { "illust_id": illust_id, } @@ -780,7 +786,7 @@ def user_novels( offset: int | str | None = None, req_auth: bool = True, ) -> models.UserNovels: - url = f"{self.hosts}/v1/user/novels" + url = self._url("/v1/user/novels") params = { "user_id": user_id, "filter": filter, @@ -798,7 +804,7 @@ def novel_series( last_order: str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v2/novel/series" + url = self._url("/v2/novel/series") params = { "series_id": series_id, "filter": filter, @@ -814,7 +820,7 @@ def novel_detail( novel_id: int | str, req_auth: bool = True, ) -> models.NovelInfo: - url = f"{self.hosts}/v2/novel/detail" + url = self._url("/v2/novel/detail") params = { "novel_id": novel_id, } @@ -829,7 +835,7 @@ def novel_new( max_novel_id: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/novel/new" + url = self._url("/v1/novel/new") params: dict[str, Any] = { "filter": filter, } @@ -846,7 +852,7 @@ def novel_follow( offset: int | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/novel/follow" + url = self._url("/v1/novel/follow") params: dict[str, Any] = {"restrict": restrict, "offset": offset} r = self.no_auth_requests_call("GET", url, params=params, req_auth=req_auth) return self.parse_result(r) @@ -860,7 +866,7 @@ def webview_novel( req_auth: bool = True, ) -> models.WebviewNovel | str: # change new endpoint due to #337 - url = f"{self.hosts}/webview/v2/novel" + url = self._url("/webview/v2/novel") params = { "id": novel_id, "viewer_version": "20221031_ai", @@ -899,7 +905,7 @@ def illust_new( max_illust_id: int | str | None = None, req_auth: bool = True, ) -> ParsedJson: - url = f"{self.hosts}/v1/illust/new" + url = self._url("/v1/illust/new") params: dict[str, Any] = { "content_type": content_type, "filter": filter,