<commit_before>import datetime as dt
import logging

from spotify import Link, SpotifyError

from mopidy import settings
from mopidy.backends.spotify import ENCODING
from mopidy.models import Artist, Album, Track, Playlist

logger = logging.getLogger('mopidy.backends.spotify.translator')

class SpotifyTranslator(object):
    @classmethod
    def to_mopidy_artist(cls, spotify_artist):
        if not spotify_artist.is_loaded():
            return Artist(name=u'[loading...]')
        return Artist(
            uri=str(Link.from_artist(spotify_artist)),
            name=spotify_artist.name()
        )

    @classmethod
    def to_mopidy_album(cls, spotify_album):
        if spotify_album is None or not spotify_album.is_loaded():
            return Album(name=u'[loading...]')
        # TODO pyspotify got much more data on albums than this
        return Album(name=spotify_album.name())

    @classmethod
    def to_mopidy_track(cls, spotify_track):
        uri = str(Link.from_track(spotify_track, 0))
        if not spotify_track.is_loaded():
            return Track(uri=uri, name=u'[loading...]')
        if (spotify_track.album() is not None and
                dt.MINYEAR <= int(spotify_track.album().year()) <= dt.MAXYEAR):
            date = dt.date(spotify_track.album().year(), 1, 1)
        else:
            date = None
        return Track(
            uri=uri,
            name=spotify_track.name(),
            artists=[cls.to_mopidy_artist(a) for a in spotify_track.artists()],
            album=cls.to_mopidy_album(spotify_track.album()),
            track_no=spotify_track.index(),
            date=date,
            length=spotify_track.duration(),
            bitrate=settings.SPOTIFY_BITRATE,
        )

    @classmethod
    def to_mopidy_playlist(cls, spotify_playlist):
        if not spotify_playlist.is_loaded():
            return Playlist(name=u'[loading...]')
        # FIXME Replace this try-except with a check on the playlist type,
        # which is currently not supported by pyspotify, to avoid handling
        # playlist folder boundaries like normal playlists.
        try:
            return Playlist(
                uri=str(Link.from_playlist(spotify_playlist)),
                name=spotify_playlist.name(),
                # FIXME if check on link is a hackish workaround for is_local
                tracks=[cls.to_mopidy_track(t) for t in spotify_playlist
                    if str(Link.from_track(t, 0))],
            )
        except SpotifyError, e:
            logger.info(u'Failed translating Spotify playlist '
                '(probably a playlist folder boundary): %s', e)<commit_msg>Use new  method in playlists to check if they are playlists or folder boundaries<commit_after>import datetime as dt
import logging

from spotify import Link, SpotifyError

from mopidy import settings
from mopidy.backends.spotify import ENCODING
from mopidy.models import Artist, Album, Track, Playlist

logger = logging.getLogger('mopidy.backends.spotify.translator')

class SpotifyTranslator(object):
    @classmethod
    def to_mopidy_artist(cls, spotify_artist):
        if not spotify_artist.is_loaded():
            return Artist(name=u'[loading...]')
        return Artist(
            uri=str(Link.from_artist(spotify_artist)),
            name=spotify_artist.name()
        )

    @classmethod
    def to_mopidy_album(cls, spotify_album):
        if spotify_album is None or not spotify_album.is_loaded():
            return Album(name=u'[loading...]')
        # TODO pyspotify got much more data on albums than this
        return Album(name=spotify_album.name())

    @classmethod
    def to_mopidy_track(cls, spotify_track):
        uri = str(Link.from_track(spotify_track, 0))
        if not spotify_track.is_loaded():
            return Track(uri=uri, name=u'[loading...]')
        if (spotify_track.album() is not None and
                dt.MINYEAR <= int(spotify_track.album().year()) <= dt.MAXYEAR):
            date = dt.date(spotify_track.album().year(), 1, 1)
        else:
            date = None
        return Track(
            uri=uri,
            name=spotify_track.name(),
            artists=[cls.to_mopidy_artist(a) for a in spotify_track.artists()],
            album=cls.to_mopidy_album(spotify_track.album()),
            track_no=spotify_track.index(),
            date=date,
            length=spotify_track.duration(),
            bitrate=settings.SPOTIFY_BITRATE,
        )

    @classmethod
    def to_mopidy_playlist(cls, spotify_playlist):
        if not spotify_playlist.is_loaded():
            return Playlist(name=u'[loading...]')
        if spotify_playlist.type() != 'playlist':
            return
        try:
            return Playlist(
                uri=str(Link.from_playlist(spotify_playlist)),
                name=spotify_playlist.name(),
                # FIXME if check on link is a hackish workaround for is_local
                tracks=[cls.to_mopidy_track(t) for t in spotify_playlist
                    if str(Link.from_track(t, 0))],
            )
        except SpotifyError, e:
            logger.warning(u'Failed translating Spotify playlist: %s', e)
{'fraction_non_alphanumeric': 0.09748062015503876, 'fraction_numerical': 0.0015503875968992248, 'mean_word_length': 2.406600660066007, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6872051', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1536, 'n_words': 354}