File: actor.py

package info (click to toggle)
mopidy-somafm 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 168 kB
  • sloc: python: 282; makefile: 5
file content (86 lines) | stat: -rw-r--r-- 2,495 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import unicode_literals

import logging
from mopidy import backend
from mopidy.models import Album, Artist, Ref, Track
import pykka
import mopidy_somafm
from .somafm import SomaFMClient


logger = logging.getLogger(__name__)


class SomaFMBackend(pykka.ThreadingActor, backend.Backend):

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

        self.somafm = SomaFMClient(
            config['proxy'],
            "%s/%s" % (
                mopidy_somafm.Extension.dist_name,
                mopidy_somafm.__version__))
        self.library = SomaFMLibraryProvider(backend=self)

        self.uri_schemes = ['somafm']
        self.quality = config['somafm']['quality']
        self.encoding = config['somafm']['encoding']
        self.dj_as_artist = config['somafm']['dj_as_artist']

    def on_start(self):
        self.somafm.refresh(self.encoding, self.quality)


class SomaFMLibraryProvider(backend.LibraryProvider):

    root_directory = Ref.directory(uri='somafm:root', name='SomaFM')

    def lookup(self, uri):
        # Whatever the uri, it will always contains one track
        # which is a url to a pls

        if not uri.startswith('somafm:'):
            return None

        channel_name = uri[uri.index('/') + 1:]
        channel_data = self.backend.somafm.channels[channel_name]

        # Artists
        if self.backend.dj_as_artist:
            artist = Artist(name=channel_data['dj'])
        else:
            artist = Artist()

        # Build album (idem as playlist, but with more metada)
        album = Album(
            artists=[artist],
            images=[channel_data['image']],
            name=channel_data['title'],
            uri='somafm:channel:/%s' % (channel_name))

        track = Track(
            artists=[artist],
            album=album,
            last_modified=channel_data['updated'],
            comment=channel_data['description'],
            genre=channel_data['genre'],
            name=channel_data['title'],
            uri=channel_data['pls'])

        return [track]

    def browse(self, uri):

        if uri != 'somafm:root':
            return []

        result = []
        for channel in self.backend.somafm.channels:
            result.append(Ref.track(
                uri='somafm:channel:/%s' % (channel),
                name=self.backend.somafm.channels[channel]['title']
                ))

        result.sort(key=lambda ref: ref.name.lower())
        return result