<commit_before>import datetime as dt
import logging

import spytify

from mopidy import config
from mopidy.backends import (BaseBackend, BaseCurrentPlaylistController,
    BaseLibraryController, BasePlaybackController,
    BaseStoredPlaylistsController)
from mopidy.models import Artist, Album, Track, Playlist

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

ENCODING = 'utf-8'

class DespotifyBackend(BaseBackend):
    def __init__(self):
        self.current_playlist = DespotifyCurrentPlaylistController(backend=self)
        self.library = DespotifyLibraryController(backend=self)
        self.playback = DespotifyPlaybackController(backend=self)
        self.stored_playlists = DespotifyStoredPlaylistsController(backend=self)
        self.uri_handlers = [u'spotify:', u'http://open.spotify.com/']
        self.translate = DespotifyTranslator()
        self.spotify = self._connect()
        self.stored_playlists.refresh()

    def _connect(self):
        logger.info(u'Connecting to Spotify')
        return spytify.Spytify(
            config.SPOTIFY_USERNAME, config.SPOTIFY_PASSWORD)


class DespotifyCurrentPlaylistController(BaseCurrentPlaylistController):
    pass


class DespotifyLibraryController(BaseLibraryController):
    def search(self, type, what):
        query = u'%s:%s' % (type, what)
        result = self.backend.spotify.search(query.encode(ENCODING))
        if result is not None:
            return self.backend.translate.to_mopidy_playlist(result.playlist)

<commit_msg>despotify: search() should always return a Playlist<commit_after>import datetime as dt
import logging

import spytify

from mopidy import config
from mopidy.backends import (BaseBackend, BaseCurrentPlaylistController,
    BaseLibraryController, BasePlaybackController,
    BaseStoredPlaylistsController)
from mopidy.models import Artist, Album, Track, Playlist

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

ENCODING = 'utf-8'

class DespotifyBackend(BaseBackend):
    def __init__(self):
        self.current_playlist = DespotifyCurrentPlaylistController(backend=self)
        self.library = DespotifyLibraryController(backend=self)
        self.playback = DespotifyPlaybackController(backend=self)
        self.stored_playlists = DespotifyStoredPlaylistsController(backend=self)
        self.uri_handlers = [u'spotify:', u'http://open.spotify.com/']
        self.translate = DespotifyTranslator()
        self.spotify = self._connect()
        self.stored_playlists.refresh()

    def _connect(self):
        logger.info(u'Connecting to Spotify')
        return spytify.Spytify(
            config.SPOTIFY_USERNAME, config.SPOTIFY_PASSWORD)


class DespotifyCurrentPlaylistController(BaseCurrentPlaylistController):
    pass


class DespotifyLibraryController(BaseLibraryController):
    def search(self, type, what):
        query = u'%s:%s' % (type, what)
        result = self.backend.spotify.search(query.encode(ENCODING))
        if result is None:
            return Playlist()
        return self.backend.translate.to_mopidy_playlist(result.playlist)


{'fraction_non_alphanumeric': 0.0869140625, 'fraction_numerical': 0.0006510416666666666, 'mean_word_length': 4.173400673400673, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3839180', 'n_tokens_mistral': 913, 'n_tokens_neox': 901, 'n_words': 171}