<commit_before>import time

from mopidy import backend
from mopidy.internal import encoding

import requests

from mopidy_pandora import listener

from mopidy_pandora.uri import logger


class PandoraPlaybackProvider(backend.PlaybackProvider):
    SKIP_LIMIT = 3

    def __init__(self, audio, backend):
        super(PandoraPlaybackProvider, self).__init__(audio, backend)

        # TODO: It shouldn't be necessary to keep track of the number of tracks that have been skipped in the
        # player anymore once https://github.com/mopidy/mopidy/issues/1221 has been fixed.
        self._consecutive_track_skips = 0

        # TODO: add gapless playback when it is supported in Mopidy > 1.1
        # self.audio.set_about_to_finish_callback(self.callback).get()

        # def callback(self):
        # See: https://discuss.mopidy.com/t/has-the-gapless-playback-implementation-been-completed-yet/784/2
        # self.audio.set_uri(self.translate_uri(self.get_next_track())).get()

    @property
    def consecutive_track_skips(self):
        return self._consecutive_track_skips

    @consecutive_track_skips.setter
    def consecutive_track_skips(self, value=1):
        if value > 0:
            self._consecutive_track_skips += value

            if self.consecutive_track_skips >= self.SKIP_LIMIT-1:
                logger.error('Maximum track skip limit (%s) exceeded, stopping...', self.SKIP_LIMIT)
                self._trigger_stop()
        else:
            self._consecutive_track_skips = 0

    def prepare_change(self):
        self._trigger_prepare_change()
        super(PandoraPlaybackProvider, self).prepare_change()

    def change_track(self, track):
        if track.uri is None:
            logger.warning("No URI for track '%s'. Track cannot be played.", track.name)
            self.consecutive_track_skips += 1
            return False

        if self.is_playable(track.uri):
            self.consecutive_track_skips = 0
            return super(PandoraPlaybackProvider, self).change_track(track)
        else:
            self.consecutive_track_skips += 1
            return False

    def translate_uri(self, uri):
        return self.backend.library.lookup_pandora_track(uri).audio_url

    def is_playable(self, track_uri):
        """ A track is playable if it can be retrieved, has a URL, and the Pandora URL can be accessed.<commit_msg>Add warning message to help determnie playback state.<commit_after>import time

from mopidy import backend
from mopidy.internal import encoding

import requests

from mopidy_pandora import listener

from mopidy_pandora.uri import logger


class PandoraPlaybackProvider(backend.PlaybackProvider):
    SKIP_LIMIT = 3

    def __init__(self, audio, backend):
        super(PandoraPlaybackProvider, self).__init__(audio, backend)

        # TODO: It shouldn't be necessary to keep track of the number of tracks that have been skipped in the
        # player anymore once https://github.com/mopidy/mopidy/issues/1221 has been fixed.
        self._consecutive_track_skips = 0

        # TODO: add gapless playback when it is supported in Mopidy > 1.1
        # self.audio.set_about_to_finish_callback(self.callback).get()

        # def callback(self):
        # See: https://discuss.mopidy.com/t/has-the-gapless-playback-implementation-been-completed-yet/784/2
        # self.audio.set_uri(self.translate_uri(self.get_next_track())).get()

    @property
    def consecutive_track_skips(self):
        return self._consecutive_track_skips

    @consecutive_track_skips.setter
    def consecutive_track_skips(self, value=1):
        if value > 0:
            self._consecutive_track_skips += value

            if self.consecutive_track_skips >= self.SKIP_LIMIT-1:
                logger.error('Maximum track skip limit (%s) exceeded, stopping...', self.SKIP_LIMIT)
                self._trigger_stop()
        else:
            self._consecutive_track_skips = 0

    def prepare_change(self):
        self._trigger_prepare_change()
        super(PandoraPlaybackProvider, self).prepare_change()

    def change_track(self, track):
        if track.uri is None:
            logger.warning("No URI for track '%s'. Track cannot be played.", track.name)
            self.consecutive_track_skips += 1
            return False

        if self.is_playable(track.uri):
            self.consecutive_track_skips = 0
            return super(PandoraPlaybackProvider, self).change_track(track)
        else:
            logger.warning("Skipping unplayable track '%s'.", track.name)
            self.consecutive_track_skips += 1
            return False

    def translate_uri(self, uri):
        return self.backend.library.lookup_pandora_track(uri).audio_url

    def is_playable(self, track_uri):
        """ A track is playable if it can be retrieved, has a URL, and the Pandora URL can be accessed.
{'fraction_non_alphanumeric': 0.08446641944787804, 'fraction_numerical': 0.007828594973217964, 'mean_word_length': 2.8379446640316206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4522974', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1416, 'n_words': 353}