Skip to content

Commit 1431c45

Browse files
committed
Fixed video pagination. New exceptions for error handling. Improved model relation links. Resource pagination (wip).
1 parent 24785d7 commit 1431c45

File tree

14 files changed

+159
-67
lines changed

14 files changed

+159
-67
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@
3939
test_suite='tests',
4040
tests_require=test_requirements,
4141
url='https://github.com/PetterKraabol/Twitch-Python',
42-
version='0.0.10',
42+
version='0.0.11',
4343
zip_safe=True,
4444
)

twitch/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from .helix import Helix
33
from .v5 import V5
44

5-
65
name = "twitch"
76

87
__all__ = [Helix, V5, Chat]

twitch/cache.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ def __init__(self, duration: timedelta = None):
1010
self._duration: timedelta = duration or timedelta(minutes=30)
1111

1212
def get(self, key: str, ignore_expiration: bool = False) -> Optional[dict]:
13-
if self.has(key):
14-
if ignore_expiration or self.expired(key):
15-
return self._store[key]['value']
13+
if self.has(key) and (ignore_expiration or self.expired(key)):
14+
return self._store[key]['value']
1615

1716
def set(self, key: str, value: dict, duration: timedelta = None) -> datetime:
1817
expiration: datetime = datetime.now() + (duration or self._duration)

twitch/helix/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from .models import Stream, User, Video, Game
66

77
# Resources
8-
from .streams import Streams
8+
from .streams import Streams, StreamNotFound
99
from .users import Users
1010
from .videos import Videos
1111
from .games import Games
1212

13-
__all__ = [Helix, Stream, User, Video, Streams, Users, Videos]
13+
__all__ = [Helix, Stream, User, Video, Streams, Users, Videos, StreamNotFound]

twitch/helix/games.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ def __init__(self, api: API, **kwargs: Optional):
1515
self._api.get(self._path, params=kwargs)['data']]
1616

1717
def top(self, **kwargs) -> List['helix.Game']:
18-
return [helix.Game(api=self._api, data=game) for game in self._api.get('games/top', params=kwargs)['data']]
18+
return [helix.Game(api=self._api, data=game) for game in
19+
self._api.get(f'{self._path}/top', params=kwargs)['data']]

twitch/helix/helix.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import timedelta
2-
from typing import List, Union
2+
from typing import List, Union, Optional
33

44
import twitch.helix as helix
55
from twitch.api import API
@@ -9,11 +9,21 @@ class Helix:
99
BASE_URL: str = 'https://api.twitch.tv/helix/'
1010

1111
def __init__(self, client_id: str, client_secret: str = None, use_cache: bool = False,
12-
cache_duration: timedelta = timedelta(minutes=30), rate_limit: int = 30):
12+
cache_duration: Optional[timedelta] = None, rate_limit: int = 30):
13+
"""
14+
Helix API (New Twitch API)
15+
https://dev.twitch.tv/docs/api/
16+
17+
:param client_id: Twitch client ID
18+
:param client_secret: Twitch client secret
19+
:param use_cache: Cache API requests (recommended)
20+
:param cache_duration: Cache duration
21+
:param rate_limit: API rate limit
22+
"""
1323
self.client_id: str = client_id
1424
self.client_secret: str = client_secret
1525
self.use_cache: bool = use_cache
16-
self.cache_duration: timedelta = cache_duration
26+
self.cache_duration: Optional[timedelta] = cache_duration
1727
self.rate_limit: int = rate_limit
1828

1929
def api(self) -> API:
@@ -25,9 +35,9 @@ def users(self, *args) -> 'helix.Users':
2535
def user(self, user: Union[str, int]) -> 'helix.User':
2636
return self.users(user)[0]
2737

28-
def videos(self, video_ids: Union[str, int, List[Union[str, int]]], **kwargs) -> 'helix.Videos':
29-
if type(video_ids) != list:
30-
video_ids = [video_ids]
38+
def videos(self, video_ids: Union[str, int, List[Union[str, int]]] = None, **kwargs) -> 'helix.Videos':
39+
if video_ids and type(video_ids) != list:
40+
video_ids = [int(video_ids)]
3141
return helix.Videos(self.api(), video_ids=video_ids, **kwargs)
3242

3343
def video(self, video_id: Union[str, int] = None, **kwargs) -> 'helix.Video':

twitch/helix/models/game.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import twitch.helix as helix
12
from twitch.api import API
23

34

@@ -19,3 +20,6 @@ def __init__(self, api: API, data: dict):
1920

2021
def __str__(self):
2122
return self.name
23+
24+
def videos(self, **kwargs) -> 'helix.Videos':
25+
return helix.Videos(self._api, game_id=self.id, **kwargs)

twitch/helix/models/stream.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, api: API, data: dict):
2828
self.__dict__[key] = value
2929

3030
def user(self) -> 'helix.User':
31-
return helix.Users(self._api, self.user_id)[0]
31+
return helix.Users(self._api, int(self.user_id))[0]
3232

3333
def __str__(self):
3434
return self.title

twitch/helix/models/user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __str__(self):
2929
return self.login
3030

3131
def videos(self, **kwargs) -> 'helix.Videos':
32-
return helix.Videos(api=self._api, user_id=self.id, **kwargs)
32+
return helix.Videos(api=self._api, user_id=int(self.id), **kwargs)
3333

3434
def stream(self) -> 'helix.Stream':
35-
return helix.Streams(api=self._api, user_id=self.id)[0]
35+
return helix.Streams(api=self._api, user_id=int(self.id))[0]

twitch/helix/models/video.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def comments(self) -> 'v5.Comments':
3434
use_cache=self._api.use_cache,
3535
cache_duration=self._api.cache_duration).comments(self.id)
3636

37-
def user(self) -> 'helix.Users':
38-
return helix.Users(self._api, self.user_id)
37+
def user(self) -> 'helix.User':
38+
return helix.Users(self._api, int(self.user_id))[0]
3939

4040
def __str__(self):
4141
return self.title

0 commit comments

Comments
 (0)