<commit_before>from __future__ import unicode_literals

import hashlib
import logging
import time

from urllib import urlencode
from urlparse import parse_qs, urljoin, urlsplit, urlunsplit

from mopidy import backend, httpclient, models

import pykka

import requests

import mopidy_emby


logger = logging.getLogger(__name__)


class EmbyBackend(pykka.ThreadingActor, backend.Backend):
    uri_schemes = ['emby']

    def __init__(self, config, audio):
        super(EmbyBackend, self).__init__()

        self.library = EmbyLibraryProvider(backend=self)
        self.playback = EmbyPlaybackProvider(audio=audio, backend=self)
        self.playlist = None
        self.remote = EmbyHandler(config)


class EmbyPlaybackProvider(backend.PlaybackProvider):

    def translate_uri(self, uri):
        id = uri.split(':')[-1]

        track_url = self.backend.remote.api_url(
            '/Audio/{}/stream.mp3'.format(id)
        )

        logger.debug('Emby track streaming url: {}'.format(track_url))

        return track_url


class EmbyLibraryProvider(backend.LibraryProvider):

    root_directory = models.Ref.directory(uri='emby:',
                                          name='Emby')

    def browse(self, uri):
        if uri == self.root_directory.uri:
            music_root = self.backend.remote.get_music_root()
            artists = sorted(
                self.backend.remote.get_directory(music_root)['Items'],
                key=lambda k: k['Name']
            )
            return [
                models.Ref.artist(
                    uri='emby:{}'.format(i['Id']),
                    name=i['Name']
                )
                for i in artists
            ]

        # split uri
        parts = uri.split(':')

        # albums
        if len(parts) == 2:
            id = parts[1]
            albums = sorted(
                self.backend.remote.get_directory(id)['Items'],
                key=lambda k: k['Name']
            )
            return [
                models.Ref.album(
                    uri='emby:{}:{}'.format(id, i['Id']),
                    name=i['Name']
                )
                for i in albums
            ]

        # tracklist
        if len(parts) == 3:
            id = parts[2]
            tracks = sorted(
                self.backend.remote.get_directory(id)['Items'],
                key=lambda k: k['IndexNumber']
            )
            return [
                models.Ref.track(
                    uri='emby:{}:{}:{}'.format(id, parts[1], i['Id']),
                    name=i['Name']
                )
                for i in tracks
            ]

        return []

    def lookup(self, uri=None, uris=None):
        logger.debug('Emby lookup: {}'.format(uri or uris))
        if uri:
            parts = uri.split(':')
            logger.debug('Emby lookup: {}'.format(uri))

            if len(parts) == 3:
                tracks = self.backend.remote.get_album_tracks(uri)

            elif len(parts) == 4 or len(parts) == 5:
                tracks = [self.backend.remote.get_track(uri)]

            else:
                logger.info('Unknown Emby lookup URI: '.format(uri))
                tracks = []

            return [track for track in tracks if track]

        else:
            return {uri: self.lookup(uri=uri) for uri in uris}


class cache(object):
<commit_msg>lookup only checks for 4 parts<commit_after>from __future__ import unicode_literals

import hashlib
import logging
import time

from urllib import urlencode
from urlparse import parse_qs, urljoin, urlsplit, urlunsplit

from mopidy import backend, httpclient, models

import pykka

import requests

import mopidy_emby


logger = logging.getLogger(__name__)


class EmbyBackend(pykka.ThreadingActor, backend.Backend):
    uri_schemes = ['emby']

    def __init__(self, config, audio):
        super(EmbyBackend, self).__init__()

        self.library = EmbyLibraryProvider(backend=self)
        self.playback = EmbyPlaybackProvider(audio=audio, backend=self)
        self.playlist = None
        self.remote = EmbyHandler(config)


class EmbyPlaybackProvider(backend.PlaybackProvider):

    def translate_uri(self, uri):
        id = uri.split(':')[-1]

        track_url = self.backend.remote.api_url(
            '/Audio/{}/stream.mp3'.format(id)
        )

        logger.debug('Emby track streaming url: {}'.format(track_url))

        return track_url


class EmbyLibraryProvider(backend.LibraryProvider):

    root_directory = models.Ref.directory(uri='emby:',
                                          name='Emby')

    def browse(self, uri):
        if uri == self.root_directory.uri:
            music_root = self.backend.remote.get_music_root()
            artists = sorted(
                self.backend.remote.get_directory(music_root)['Items'],
                key=lambda k: k['Name']
            )
            return [
                models.Ref.artist(
                    uri='emby:{}'.format(i['Id']),
                    name=i['Name']
                )
                for i in artists
            ]

        # split uri
        parts = uri.split(':')

        # albums
        if len(parts) == 2:
            id = parts[1]
            albums = sorted(
                self.backend.remote.get_directory(id)['Items'],
                key=lambda k: k['Name']
            )
            return [
                models.Ref.album(
                    uri='emby:{}:{}'.format(id, i['Id']),
                    name=i['Name']
                )
                for i in albums
            ]

        # tracklist
        if len(parts) == 3:
            id = parts[2]
            tracks = sorted(
                self.backend.remote.get_directory(id)['Items'],
                key=lambda k: k['IndexNumber']
            )
            return [
                models.Ref.track(
                    uri='emby:{}:{}:{}'.format(id, parts[1], i['Id']),
                    name=i['Name']
                )
                for i in tracks
            ]

        return []

    def lookup(self, uri=None, uris=None):
        logger.debug('Emby lookup: {}'.format(uri or uris))
        if uri:
            parts = uri.split(':')
            logger.debug('Emby lookup: {}'.format(uri))

            if len(parts) == 4:
                tracks = [self.backend.remote.get_track(uri)]

            else:
                logger.info('Unknown Emby lookup URI: '.format(uri))
                tracks = []

            return [track for track in tracks if track]

        else:
            return {uri: self.lookup(uri=uri) for uri in uris}


class cache(object):

{'fraction_non_alphanumeric': 0.11728208232445521, 'fraction_numerical': 0.0028753026634382567, 'mean_word_length': 1.629924393155591, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4474853', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1824, 'n_words': 399}