From 10e39cb20e41627f0300f29286f30dfce5c54047 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 12 Apr 2019 15:19:28 +0200 Subject: [PATCH] Create Playlist and Pool PlayModes --- royalnet/utils/playmodes.py | 63 ++++++++++++++++++++++++++++++++++++ royalnet/utils/videoqueue.py | 2 -- 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 royalnet/utils/playmodes.py delete mode 100644 royalnet/utils/videoqueue.py diff --git a/royalnet/utils/playmodes.py b/royalnet/utils/playmodes.py new file mode 100644 index 00000000..70c61d85 --- /dev/null +++ b/royalnet/utils/playmodes.py @@ -0,0 +1,63 @@ +import math +import random + + +class PlayMode: + def videos_left(self): + raise NotImplementedError() + + async def next(self): + """Get the next video from the list and advance it.""" + raise NotImplementedError() + + def add(self, item): + """Add a new video to the PlayMode.""" + raise NotImplementedError() + + +class Playlist(PlayMode): + """A video list. Videos played are removed from the list.""" + def __init__(self, starting_list=None): + if starting_list is None: + starting_list = [] + self.list = starting_list + + def videos_left(self): + return len(self.list) + + async def next(self): + while True: + try: + next_video = self.list.pop(0) + except IndexError: + yield None + else: + yield next_video + + def add(self, item): + self.list.append(item) + + +class Pool(PlayMode): + """A video pool. Videos played are played back in random order, and they are kept in the pool.""" + def __init__(self, starting_pool=None): + if starting_pool is None: + starting_pool = [] + self.pool = starting_pool + self._pool_copy = [] + + def videos_left(self): + return math.inf + + async def next(self): + while True: + self._pool_copy = self.pool.copy() + random.shuffle(self._pool_copy) + while self._pool_copy: + next_video = self._pool_copy.pop(0) + yield next_video + + def add(self, item): + self.pool.append(item) + self._pool_copy.append(self._pool_copy) + random.shuffle(self._pool_copy) diff --git a/royalnet/utils/videoqueue.py b/royalnet/utils/videoqueue.py deleted file mode 100644 index d83c8245..00000000 --- a/royalnet/utils/videoqueue.py +++ /dev/null @@ -1,2 +0,0 @@ -class RoyalQueue: - raise NotImplementedError()