Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
okaits committed Aug 31, 2024
2 parents 98f2d07 + 7fa50a6 commit 7770a9f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
3 changes: 2 additions & 1 deletion nicovideo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" nicovideo.py: nicovideo API wrapper for Python3. """
from . import video, errors, user, apirawdicts

__version__ = "4.1.1"
__version__ = "4.1.2"
__version_info__ = (4, 1, 2)
__all__ = ["video", "user", "errors", "apirawdicts"]
14 changes: 7 additions & 7 deletions nicovideo/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

NICOVIDEO_USERPAGE_URL = "https://www.nicovideo.jp/user/{}/video?responseType=json"

class _APIResponse():
class APIResponse():
"""
ユーザの詳細 (e.g. ニックネーム, 投稿動画, etc.) を格納するクラスです。
Expand Down Expand Up @@ -82,7 +82,7 @@ def _videolist_nextpage(self, page: int) -> list[apirawdicts.UserAPIRawDicts.NVA
return rawdict["nvapi"][0]["body"]["data"]["items"]

@property
def videolist(self) -> collections.abc.Generator[video.APIResponse, None, None]:
def videolist(self) -> collections.abc.Generator[video.APIResponseFromServer, None, None]:
"""
ユーザが投稿した動画を一つずつ、video.APIResponseにしてからyieldします。
nextごとにニコニコ動画でのAPIリクエストが発生するため、注意してください。
Expand All @@ -107,15 +107,15 @@ def __setattr__(self, _, name) -> typing.NoReturn:
def __delattr__(self, name) -> typing.NoReturn:
raise errors.FrozenInstanceError(f"cannot delete field {name}")
def __repr__(self) -> str:
return f"user.APIResponse(user_id={self.user_id})"
return f"<nicovideo.py user.APIResponse: {self.user_id}>"
def __str__(self) -> str:
return self.nickname
def __hash__(self) -> int:
return self.user_id

APIResponse = typing.NewType("APIResponse", _APIResponse)
APIResponseFromServer = typing.NewType("APIResponseFromServer", APIResponse)

def get_metadata(user_id: int) -> APIResponse:
def get_metadata(user_id: int) -> APIResponseFromServer:
"""
ニコニコのAPIサーバからユーザ情報を取得します。
Expand All @@ -129,7 +129,7 @@ def get_metadata(user_id: int) -> APIResponse:
Example:
>>> get_metadata(9003560)
"""
gotapiresponse = _APIResponse()
gotapiresponse = APIResponse()
try:
with urllib.request.urlopen(url=NICOVIDEO_USERPAGE_URL.format(user_id)) as res:
response_text = res.read()
Expand Down Expand Up @@ -177,4 +177,4 @@ def get_metadata(user_id: int) -> APIResponse:
rawdict_userdata["icons"]["small"],
rawdict_userdata["icons"]["large"]
))
return APIResponse(gotapiresponse)
return APIResponseFromServer(gotapiresponse)
16 changes: 8 additions & 8 deletions nicovideo/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

NICOVIDEO_VIDEOPAGE_URL = "https://www.nicovideo.jp/watch/{}?responseType=json"

class _APIResponse():
class APIResponse():
"""
動画の詳細(e.g. タイトル, 概要, etc.)を格納するクラスです。
Expand Down Expand Up @@ -47,13 +47,13 @@ class _APIResponse():
genre: typing.Optional[dict[typing.Literal["label", "key"], str]]

@property
def uploader(self) -> user._APIResponse:
def uploader(self) -> user.APIResponse:
"""動画の投稿者を取得する。"""
return user.get_metadata(user_id=int(self._rawdict["owner"]["id"]))

@property
@functools.cache
def cached_uploader(self) -> user._APIResponse:
def cached_uploader(self) -> user.APIResponse:
"""動画の投稿者を取得する。(初回にキャッシュするので最新ではない可能性がある。)"""
return self.uploader

Expand All @@ -62,17 +62,17 @@ def __setattr__(self, _, name) -> typing.NoReturn:
def __delattr__(self, name) -> typing.NoReturn:
raise errors.FrozenInstanceError(f"cannot delete field {name}")
def __repr__(self) -> str:
return f"video.APIResponse(video_id={self.nicovideo_id})"
return f"<nicovideo.py video.APIResponse: {self.nicovideo_id}>"
def __str__(self) -> str:
return self.title
def __hash__(self) -> int:
return int("".join(
[str(object=ord(character)) for character in self.nicovideo_id]
))

APIResponse = typing.NewType("APIResponse", _APIResponse)
APIResponseFromServer = typing.NewType("APIResponseFromServer", APIResponse)

def get_metadata(video_id: str) -> APIResponse:
def get_metadata(video_id: str) -> APIResponseFromServer:
"""
ニコニコのAPIサーバから動画情報を取得します。
Expand All @@ -86,7 +86,7 @@ def get_metadata(video_id: str) -> APIResponse:
Example:
>>> get_metadata("sm9")
"""
gotapiresponse = _APIResponse()
gotapiresponse = APIResponse()
object.__setattr__(gotapiresponse, "nicovideo_id", video_id)

try:
Expand Down Expand Up @@ -131,4 +131,4 @@ def get_metadata(video_id: str) -> APIResponse:
})
else:
object.__setattr__(gotapiresponse, "genre", None)
return APIResponse(gotapiresponse)
return APIResponseFromServer(gotapiresponse)
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nicovideo.py"
version = "4.1.1"
version = "4.1.2"
description = "ニコニコ動画のAPIを使用して、動画や投稿者などの情報を取得するライブラリです。"
license = "MIT"
authors = ["okaits7534 <[email protected]>"]
Expand All @@ -20,7 +20,7 @@ packages = [{include = "nicovideo"}]
include = ["LICENSE.md", "README.md"]

[tool.poetry.dependencies]
python = ">=3.11"
python = ">=3.9"
json5 = "0.9.25"
bs4 = "^0.0.2"

Expand Down

0 comments on commit 7770a9f

Please sign in to comment.