Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Avoid Block #9

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions chzzkpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
__author__ = "gunyu1019"
__license__ = "MIT"
__copyright__ = "Copyright 2024-present gunyu1019"
__version__ = "0.0.4-alpha1" # version_info.to_string()
__version__ = "0.0.5-alpha1" # version_info.to_string()


class VersionInfo(NamedTuple):
Expand All @@ -57,5 +57,5 @@ def to_string(self) -> str:


version_info: VersionInfo = VersionInfo(
major=0, minor=0, micro=4, release_level="alpha", serial=1
major=0, minor=0, micro=5, release_level="alpha", serial=1
)
42 changes: 40 additions & 2 deletions chzzkpy/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@
import asyncio
import functools
import logging
from typing import Annotated, Optional
from typing import Annotated, Final, Optional

from ahttp_client import Session, get, Path
from ahttp_client import Session, get, Path, Query
from ahttp_client.extension import get_pydantic_response_model
from ahttp_client.request import RequestCore

from .base_model import ChzzkModel, Content
from .channel import Channel
from .error import LoginRequired
from .live import LiveStatus, LiveDetail
from .search import TopSearchResult
from .user import User

_log = logging.getLogger(__name__)
_user_agent: Final[str] = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36"


class ChzzkSession(Session):
Expand Down Expand Up @@ -75,6 +78,11 @@ async def before_request(
request.headers["Cookie"] += self._token
elif getattr(request.func, "__login_required__", False):
raise LoginRequired()

# Add User-Agent to avoid blocking
if "User-Agent" not in request.headers:
request.headers['User-Agent'] = _user_agent

return request, path

@property
Expand All @@ -100,6 +108,36 @@ async def live_detail(
) -> Content[LiveDetail]:
pass

@get_pydantic_response_model()
@get("/service/v1/search/channels", directly_response=True)
async def search_channel(
self,
keyword: Annotated[str, Query],
offset: Annotated[int, Query] = 0,
size: Annotated[int, Query] = 13
) -> Content[TopSearchResult]:
pass

@get_pydantic_response_model()
@get("/service/v1/search/lives", directly_response=True)
async def search_live(
self,
keyword: Annotated[str, Query],
offset: Annotated[int, Query] = 0,
size: Annotated[int, Query] = 13
) -> Content[TopSearchResult]:
pass

@get_pydantic_response_model()
@get("/service/v1/search/videos", directly_response=True)
async def search_video(
self,
keyword: Annotated[str, Query],
offset: Annotated[int, Query] = 0,
size: Annotated[int, Query] = 13
) -> Content[TopSearchResult]:
pass


class NaverGameAPISession(ChzzkSession):
def __init__(self, loop: Optional[asyncio.AbstractEventLoop] = None):
Expand Down