<commit_before>import datetime as dt
import logging
import sys

import spytify

from mopidy import config
from mopidy.backends import BaseBackend
from mopidy.models import Artist, Album, Track, Playlist

logger = logging.getLogger(u'backends.despotify')

ENCODING = 'utf-8'

class DespotifyBackend(BaseBackend):
    def __init__(self, *args, **kwargs):
        super(DespotifyBackend, self).__init__(*args, **kwargs)
        logger.info(u'Connecting to Spotify')
        self.spotify = spytify.Spytify(
            config.SPOTIFY_USERNAME, config.SPOTIFY_PASSWORD)
        self.cache_stored_playlists()

    def cache_stored_playlists(self):
        logger.info(u'Caching stored playlists')
        playlists = []
        for spotify_playlist in self.spotify.stored_playlists:
            playlists.append(self._to_mopidy_playlist(spotify_playlist))
        self._playlists = playlists
        logger.debug(u'Available playlists: %s',
            u', '.join([u'<%s>' % p.name for p in self._playlists]))

# Model translation

    def _to_mopidy_id(self, spotify_uri):
        return 0 # TODO

    def _to_mopidy_artist(self, spotify_artist):
        return Artist(
            uri=spotify_artist.get_uri(),
            name=spotify_artist.name.decode(ENCODING)
        )

    def _to_mopidy_album(self, spotify_album_name):
        return Album(name=spotify_album_name.decode(ENCODING))

    def _to_mopidy_track(self, spotify_track):
        if dt.MINYEAR <= int(spotify_track.year) <= dt.MAXYEAR:
            date = dt.date(spotify_track.year, 1, 1)
        else:
            date = None
        return Track(
            uri=spotify_track.get_uri(),
            title=spotify_track.title.decode(ENCODING),
            artists=[self._to_mopidy_artist(a) for a in spotify_track.artists],
            album=self._to_mopidy_album(spotify_track.album),
            track_no=spotify_track.tracknumber,
            date=date,
            length=spotify_track.length,
            id=self._to_mopidy_id(spotify_track.get_uri()),
        )

    def _to_mopidy_playlist(self, spotify_playlist):
        return Playlist(
            uri=spotify_playlist.get_uri(),
            name=spotify_playlist.name.decode(ENCODING),
            tracks=[self._to_mopidy_track(t) for t in spotify_playlist.tracks],
        )

# Play control

    def _next(self):
        self._current_song_pos += 1
        self.spotify.play(self.spotify.lookup(self._current_track.uri))
        return True

    def _pause(self):
        self.spotify.pause()
        return True

    def _play(self):
        if self._current_track is not None:
            self.spotify.play(self.spotify.lookup(self._current_track.uri))
            return True
        else:
            return False

    def _play_id(self, songid):
        self._current_song_pos = songid # XXX
        self.spotify.play(self.spotify.lookup(self._current_track.uri))
        return True

    def _play_pos(self, songpos):
        self._current_song_pos = songpos
        self.spotify.play(self.spotify.lookup(self._current_track.uri))
        return True

    def _previous(self):
        self._current_song_pos -= 1
        self.spotify.play(self.spotify.lookup(self._current_track.uri))
        return True

    def _resume(self):
        self.spotify.resume()
        return True

    def _stop(self):
        self.spotify.stop()
        return True

# Status querying

    def status_bitrate(self):
        return 320

    def url_handlers(self):
        return [u'spotify:', u'http://open.spotify.com/']

# Music database

    def search(self, type, what):
        query = u'%s:%s' % (type, what)
        result = self.spotify.search(query.encode(ENCODING))
        return self._to_mopidy_playlist(result.playlist).mpd_format()<commit_msg>Fix crash in despotify search<commit_after>import datetime as dt
import logging
import sys

import spytify

from mopidy import config
from mopidy.backends import BaseBackend
from mopidy.models import Artist, Album, Track, Playlist

logger = logging.getLogger(u'backends.despotify')

ENCODING = 'utf-8'

class DespotifyBackend(BaseBackend):
    def __init__(self, *args, **kwargs):
        super(DespotifyBackend, self).__init__(*args, **kwargs)
        logger.info(u'Connecting to Spotify')
        self.spotify = spytify.Spytify(
            config.SPOTIFY_USERNAME, config.SPOTIFY_PASSWORD)
        self.cache_stored_playlists()

    def cache_stored_playlists(self):
        logger.info(u'Caching stored playlists')
        playlists = []
        for spotify_playlist in self.spotify.stored_playlists:
            playlists.append(self._to_mopidy_playlist(spotify_playlist))
        self._playlists = playlists
        logger.debug(u'Available playlists: %s',
            u', '.join([u'<%s>' % p.name for p in self._playlists]))

# Model translation

    def _to_mopidy_id(self, spotify_uri):
        return 0 # TODO

    def _to_mopidy_artist(self, spotify_artist):
        return Artist(
            uri=spotify_artist.get_uri(),
            name=spotify_artist.name.decode(ENCODING)
        )

    def _to_mopidy_album(self, spotify_album_name):
        return Album(name=spotify_album_name.decode(ENCODING))

    def _to_mopidy_track(self, spotify_track):
        if dt.MINYEAR <= int(spotify_track.year) <= dt.MAXYEAR:
            date = dt.date(spotify_track.year, 1, 1)
        else:
            date = None
        return Track(
            uri=spotify_track.get_uri(),
            title=spotify_track.title.decode(ENCODING),
            artists=[self._to_mopidy_artist(a) for a in spotify_track.artists],
            album=self._to_mopidy_album(spotify_track.album),
            track_no=spotify_track.tracknumber,
            date=date,
            length=spotify_track.length,
            id=self._to_mopidy_id(spotify_track.get_uri()),
        )

    def _to_mopidy_playlist(self, spotify_playlist):
        return Playlist(
            uri=spotify_playlist.get_uri(),
            name=spotify_playlist.name.decode(ENCODING),
            tracks=[self._to_mopidy_track(t) for t in spotify_playlist.tracks],
        )

# Play control

    def _next(self):
        self._current_song_pos += 1
        self.spotify.play(self.spotify.lookup(self._current_track.uri))
        return True

    def _pause(self):
        self.spotify.pause()
        return True

    def _play(self):
        if self._current_track is not None:
            self.spotify.play(self.spotify.lookup(self._current_track.uri))
            return True
        else:
            return False

    def _play_id(self, songid):
        self._current_song_pos = songid # XXX
        self.spotify.play(self.spotify.lookup(self._current_track.uri))
        return True

    def _play_pos(self, songpos):
        self._current_song_pos = songpos
        self.spotify.play(self.spotify.lookup(self._current_track.uri))
        return True

    def _previous(self):
        self._current_song_pos -= 1
        self.spotify.play(self.spotify.lookup(self._current_track.uri))
        return True

    def _resume(self):
        self.spotify.resume()
        return True

    def _stop(self):
        self.spotify.stop()
        return True

# Status querying

    def status_bitrate(self):
        return 320

    def url_handlers(self):
        return [u'spotify:', u'http://open.spotify.com/']

# Music database

    def search(self, type, what):
        query = u'%s:%s' % (type, what)
        result = self.spotify.search(query.encode(ENCODING))
        if result is not None:
            return self._to_mopidy_playlist(result.playlist).mpd_format()
{'fraction_non_alphanumeric': 0.10014475588893275, 'fraction_numerical': 0.002368732727990525, 'mean_word_length': 2.6053130929791273, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '3193689', 'n_tokens_mistral': 2505, 'n_tokens_neox': 2430, 'n_words': 438}