Exemplo n.º 1
0
    def parse_status(self):
        if ('filepath' not in self.status and 'media_info'
                not in self.status) or not self.status.get('duration'):
            return {}

        if 'filepath' in self.status:
            media_info = get_media_info(self.status['filepath'])
        else:
            media_info = self.status['media_info']

        if media_info is None:
            return {}

        ep = media_info.get('episode')
        if isinstance(ep, list):
            media_info = media_info.copy()
            num_eps = len(media_info['episode'])
            self.status['duration'] = self.status['duration'] // num_eps
            ep_num = int(self.status['position'] // self.status['duration'])
            media_info['episode'] = media_info['episode'][ep_num]
            self.status['position'] %= self.status['duration']
        elif isinstance(ep, str):
            media_info['episode'] = int(ep)

        progress = min(
            round(self.status['position'] * 100 / self.status['duration'], 2),
            100)
        return {
            'state': self.status['state'],
            'progress': progress,
            'media_info': media_info,
            'updated_at': time.time(),
        }
Exemplo n.º 2
0
    def parse_status(status):
        if ('filepath' not in status
                and 'media_info' not in status) or not status.get('duration'):
            return {}

        if 'filepath' in status:
            media_info = get_media_info(status['filepath'])
        else:
            media_info = status['media_info']

        if media_info is None:
            return {}

        ep = media_info.get('episode')
        if isinstance(ep, list):
            media_info = media_info.copy()
            num_eps = len(media_info['episode'])
            status['duration'] //= num_eps
            ep_num, status['position'] = divmod(status['position'],
                                                status['duration'])
            ep_num = int(ep_num)
            # handle case when pos >= duration, causing ep_num == num_eps
            if ep_num == num_eps:
                ep_num -= 1
                status['position'] = status['duration']
            media_info['episode'] = media_info['episode'][ep_num]
        elif isinstance(ep, str):
            media_info['episode'] = int(ep)

        progress = min(round(status['position'] * 100 / status['duration'], 2),
                       100)
        return {
            'state': status['state'],
            'progress': progress,
            'media_info': media_info,
            'updated_at': time.time(),
        }