FIx channels

This commit is contained in:
Kevo 2022-02-27 18:20:42 +01:00
parent 167f7327c7
commit 4fe89e95fa
3 changed files with 66 additions and 6 deletions

View File

@ -11,10 +11,15 @@ from .models.channels import Channel
_MDL = t.TypeVar('_MDL', bound=t.Type[BasePipedModel])
class APIError(Exception): """Raised when an API call fails"""
class PipedClient:
"""
An API client for [Piped](https://piped.kavin.rocks).
See also [Piped API docs](https://piped-docs.kavin.rocks/docs)
"""
def __init__(self, base_api_url: str='https://pipedapi.kavin.rocks', session: t.Type[Session]=Session()) -> None:
@ -40,7 +45,10 @@ class PipedClient:
- `**kwargs` - Additional keyword arguments to pass to `requests.Session.get`
"""
json = self.session.get(f"{self.base_api_url}{uri}", **kwargs).json()
json: dict = self.session.get(f"{self.base_api_url}{uri}", **kwargs).json()
if json.get('error', None) is not None:
raise APIError(f"Error: {json['error']}")
if as_model is not None:
return as_model(json)
@ -63,7 +71,7 @@ class PipedClient:
kw = kwargs.copy()
if nextpage is not None:
kw.update({'params': nextpage})
kw.update({'params': {'nextpage': nextpage}})
return self._get_json(f"/nextpage/comments/{video_id}", Comments, **kw)
else:
@ -100,13 +108,26 @@ class PipedClient:
return [Video.RelatedStream(trending_video) for trending_video in self._get_json(f"/trending", **kw)]
def get_channel(self, channel_id: str, **kwargs) -> Channel:
def get_channel_by_id(self, channel_id: str, **kwargs) -> Channel:
"""
Gets information about a specific channel.
Gets information about a specific channel by its ID.
### Parameters:
- `channel_id` - The ID of the channel to get information for
- `**kwargs` - Additional keyword arguments to pass to `requests.Session.get`
"""
return self._get_json(f"/channels/{channel_id}", Channel, **kwargs)
return self._get_json(f"/channel/{channel_id}", Channel, **kwargs)
def get_channel_by_name(self, channel_name: str, **kwargs) -> Channel:
"""
Gets information about a specific channel by its name.
### Parameters:
- `channel_name` - The name of the channel to get information for
- `**kwargs` - Additional keyword arguments to pass to `requests.Session.get`
"""
return self._get_json(f"/c/{channel_name}", Channel, **kwargs)

39
tests/test_channel.py Normal file
View File

@ -0,0 +1,39 @@
from tests import CLIENT
def test_channel_by_id(channel_id: str='UCuAXFkgsw1L7xaCfnd5JJOw') -> None:
"""
Prints out information about a channel by its ID.
"""
channel = CLIENT.get_channel_by_id(channel_id)
assert channel.id == channel_id
print(f"""
Channel ID: {channel_id}
Name: {channel.name}
Description: {channel.description}
Subscriber count: {channel.subscriber_count}
""")
def test_channel_by_name(channel_name: str='SusanWojcicki') -> None:
"""
Prints out information about a channel by its ID.
"""
channel = CLIENT.get_channel_by_name(channel_name)
print(f"""
Channel ID: {channel.id}
Name: {channel.name}
Description: {channel.description}
Subscriber count: {channel.subscriber_count}
""")
if __name__ == '__main__':
test_channel_by_id()
test_channel_by_name()

View File

@ -12,7 +12,7 @@ def test_comments(video_id: str='dQw4w9WgXcQ') -> None:
np = None
while at_page < max_pages:
comments = CLIENT.get_comments(video_id, nextpage=np)
comments = CLIENT.get_comments(video_id, nextpage=np, params={'hl': 'us'})
at_page += 1
print('=' * 35, f'Page: {at_page}', '=' * 35)