diff --git a/src/phub/objects/query.py b/src/phub/objects/query.py index 1c657cb..15a4dbc 100644 --- a/src/phub/objects/query.py +++ b/src/phub/objects/query.py @@ -18,6 +18,39 @@ QueryItem = Video | FeedItem | User +class Pages: + ''' + An iterator for query pages. + ''' + + def __init__(self, query: Query) -> None: + ''' + Initialise a new Pages object. + ''' + + self.query = query + + def __repr__(self) -> str: + + return f'phub.Pages(query={self.query})' + + def __getitem__(self, index: int | slice) -> Iterator[QueryItem] | Iterator[Iterator[QueryItem]]: + ''' + Get a single, or slice of pages. + ''' + + if isinstance(index, int): + items = self.query._get_page(index) + return self.query._iter_page(items) + + def wrap(): + for i in range(index.start or 0, + index.stop or 0, + index.step or 1): + + yield self[i] + + return wrap() class Query: ''' @@ -74,22 +107,12 @@ def __len__(self) -> int: return int(counter) @cached_property - def pages(self) -> Iterator[Iterator[QueryItem]]: + def pages(self) -> Pages: ''' Iterate through the query pages. ''' - i = 0 - while 1: - - try: - page = self._get_page(i) - i += 1 - - yield self._iter_page(page) - - except errors.NoResult: - return + return Pages(self) def __iter__(self) -> Iterator[QueryItem]: '''