actor.py 1.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from __future__ import unicode_literals

import logging

from mopidy import backend

import pykka

from . import client
from . import library


logger = logging.getLogger(__name__)


class FunkwhaleBackend(pykka.ThreadingActor, backend.Backend):
    def __init__(self, config, audio):
        super(FunkwhaleBackend, self).__init__()
        self.config = config
Agate's avatar
Agate committed
20
        self.client = client.APIClient(config)
21
22
23
        self.library = library.FunkwhaleLibraryProvider(backend=self)
        self.playback = FunkwhalePlaybackProvider(audio=audio, backend=self)

Agate's avatar
Agate committed
24
        self.uri_schemes = ["funkwhale", "fw"]
25
26

    def on_start(self):
Agate's avatar
Agate committed
27
28
29
        if self.config["funkwhale"]["client_id"]:
            logger.info('Using OAuth2 connection"')
        elif self.client.username is not None:
Agate's avatar
Agate committed
30
31
32
33
34
35
            self.client.login()
            logger.info(
                'Logged in to Funkwhale as "%s" on "%s"',
                self.client.username,
                self.config["funkwhale"]["url"],
            )
36
        else:
Agate's avatar
Agate committed
37
            logger.info('Using "%s" anonymously', self.config["funkwhale"]["url"])
38
39
40
41
42


class FunkwhalePlaybackProvider(backend.PlaybackProvider):
    def translate_uri(self, uri):
        _, id = library.parse_uri(uri)
Agate's avatar
Agate committed
43
        track = self.backend.client.get_track(id)
44
45
46

        if track is None:
            return None
Agate's avatar
Agate committed
47
        url = track["listen_url"]
Agate's avatar
Agate committed
48

Agate's avatar
Agate committed
49
50
        if url.startswith("/"):
            url = self.backend.config["funkwhale"]["url"] + url
Agate's avatar
Agate committed
51
52
53
54
55
        if self.backend.client.use_oauth:
            url += "?token=" + self.backend.client.oauth_token["access_token"]

        elif self.backend.client.jwt_token:
            url += "?jwt=" + self.backend.client.jwt_token
56
        return url