<commit_before>"""
mopidy.py
~~~~~~~~~

Websocket JSONRPC client for the Mopidy media server
"""
import json

import requests
from flask_socketio import disconnect, emit

from home.core.models import get_device
from home.web.utils import ws_optional_auth
from home.web.web import socketio

UNAUTH_COMMANDS = (
    'search',
    'get_tracks',
    'add_track',
    'get_state',
    'get_current_track',
    'get_time_position',
)

SPOTIFY_API = 'https://api.spotify.com/v1/{}/{}'


def get_album_art(album_id, image=1):
    return requests.get(SPOTIFY_API.format('albums', album_id)).json().get('images')[image]


class Mopidy:
    def __init__(self, host):
        self.host = "http://" + host + ":6680/mopidy/rpc"
        self.id = 1

    def send(self, method, **kwargs):
        msg = {"jsonrpc": "2.0", "id": self.id, 'method': method, 'params': dict(kwargs)}
        return requests.post(self.host, data=json.dumps(msg)).json()

    def get_current_track(self):
        return self.send('core.playback.get_current_tl_track')['result']['track']

    def get_state(self):
        return self.send('core.playback.get_state')

    def get_time_position(self):
        return self.send('core.playback.get_time_position')

    def get_volume(self):
        return self.send('core.playback.get_volume')

    def next(self):
        return self.send('core.playback.next')

    def pause(self):
        return self.send('core.playback.pause')

    def play(self, track=None):
        return self.send('core.playback.play', tl_track=track)

    def previous(self):
        return self.send('core.playback.previous')

    def clear(self):
        return self.send('core.tracklist.clear')

    def resume(self):
        return self.send('core.playback.resume')

    def stop(self):
        return self.send('core.playback.stop')

    def get_playlists(self):
        return self.send('core.playlists.as_list')

    def add_track(self, uri):
        return self.send('core.tracklist.add', uri=uri)

    def get_tracks(self):
        return self.send('core.tracklist.get_tracks')

    def search(self, query):
        return self.send('core.library.search', any=[query])

    def custom(self, target, value):
        return self.send(target, value)

    def get_images(self, uris, index=0):
        return self.send('core.library.get_images', uris=uris)['result'].popitem()[1][index]


@socketio.on('mopidy', namespace='/mopidy')
@ws_optional_auth
def mopidy_ws(data, **kwargs):
    mopidy = get_device(data.pop('device')).dev
    auth = kwargs.pop('auth', False)
    action = data.pop('action')
    if not auth and action not in UNAUTH_COMMANDS:
        print("Disconnected client from Mopidy endpoint, not authorized/invalid command")
        disconnect()
    if action == 'search':
        results = mopidy.search(**data)
        try:
            results = results['result'][0]['tracks']
            emit('search results', json.dumps(results))
        except Exception as e:
            pass
    elif action == 'add_track':
        r = mopidy.add_track(**data)
    elif action == 'get_current_track':
        track = mopidy.get_current_track()
        emit('track', json.dumps({<commit_msg>Finally close #5<commit_after>"""
mopidy.py
~~~~~~~~~

Websocket JSONRPC client for the Mopidy media server
"""
import json

import requests
from flask_socketio import disconnect, emit

from home.core.models import get_device
from home.web.utils import ws_optional_auth
from home.web.web import socketio

UNAUTH_COMMANDS = (
    'search',
    'get_tracks',
    'add_track',
    'get_state',
    'get_current_track',
    'get_time_position',
)

SPOTIFY_API = 'https://api.spotify.com/v1/{}/{}'


def get_album_art(album_id, image=1):
    return requests.get(SPOTIFY_API.format('albums', album_id)).json().get('images')[image]


class Mopidy:
    def __init__(self, host):
        self.host = "http://" + host + ":6680/mopidy/rpc"
        self.id = 1

    def send(self, method, **kwargs):
        msg = {"jsonrpc": "2.0", "id": self.id, 'method': method, 'params': dict(kwargs)}
        return requests.post(self.host, data=json.dumps(msg)).json()

    def get_current_track(self):
        return self.send('core.playback.get_current_tl_track')['result']['track']

    def get_state(self):
        return self.send('core.playback.get_state')

    def get_time_position(self):
        return self.send('core.playback.get_time_position')

    def get_volume(self):
        return self.send('core.playback.get_volume')

    def next(self):
        return self.send('core.playback.next')

    def pause(self):
        return self.send('core.playback.pause')

    def play(self, track=None):
        return self.send('core.playback.play', tl_track=track)

    def previous(self):
        return self.send('core.playback.previous')

    def clear(self):
        return self.send('core.tracklist.clear')

    def resume(self):
        return self.send('core.playback.resume')

    def stop(self):
        return self.send('core.playback.stop')

    def get_playlists(self):
        return self.send('core.playlists.as_list')

    def add_track(self, uri):
        return self.send('core.tracklist.add', uri=uri)

    def get_tracks(self):
        return self.send('core.tracklist.get_tracks')

    def search(self, query):
        return self.send('core.library.search', any=[query])

    def custom(self, target, key, value):
        return self.send(target, key=value)

    def get_images(self, uris, index=0):
        return self.send('core.library.get_images', uris=uris)['result'].popitem()[1][index]


@socketio.on('mopidy', namespace='/mopidy')
@ws_optional_auth
def mopidy_ws(data, **kwargs):
    mopidy = get_device(data.pop('device')).dev
    auth = kwargs.pop('auth', False)
    action = data.pop('action')
    if not auth and action not in UNAUTH_COMMANDS:
        print("Disconnected client from Mopidy endpoint, not authorized/invalid command")
        disconnect()
    if action == 'search':
        results = mopidy.search(**data)
        try:
            results = results['result'][0]['tracks']
            emit('search results', json.dumps(results))
        except Exception as e:
            pass
    elif action == 'add_track':
        r = mopidy.add_track(**data)
    elif action == 'get_current_track':
        track = mopidy.get_current_track()
        emit('track', json.dumps({
{'fraction_non_alphanumeric': 0.14565694575101912, 'fraction_numerical': 0.00391972405142678, 'mean_word_length': 3.230106100795756, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1632793', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2051, 'n_words': 424}