summaryrefslogtreecommitdiffstats
path: root/py3status/modules/clementine.py
blob: 2c5c66f6b32f34881b86442382b6652c15f8277c (plain)
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
# -*- coding: utf-8 -*-
"""
Display the current "artist - title" playing in Clementine.

@author Francois LASSERRE <choiz@me.com>
@license GNU GPL http://www.gnu.org/licenses/gpl.html
"""

from time import time
from subprocess import check_output


class Py3status:

    # available configuration parameters
    cache_timeout = 0

    def _getMetadatas(self):
        """
        Get the current song metadatas (artist - title)
        """
        track_id = check_output('qdbus org.mpris.clementine /TrackList org.freedesktop.MediaPlayer.GetCurrentTrack', shell=True)
        metadatas = check_output('qdbus org.mpris.clementine /TrackList org.freedesktop.MediaPlayer.GetMetadata {}'.format(track_id.decode()), shell=True)
        lines = metadatas.decode('utf-8').split('\n')
        lines = filter(None, lines)

        now_playing = ''

        if lines:
            artist = ''
            title = ''
            internet_radio = False

            for item in lines:
                if item.find('artist:') != -1:
                    artist = item[8:]
                if item.find('title:') != -1:
                    title = item[7:]

            if title.find('.wav') != -1 or title.find('.mp3') != -1:
                title = title[:-4]
            if title.find('http') != -1:
                title = ''
                internet_radio = True

            if artist and title:
                now_playing = '♫ {} - {}'.format(artist, title)
            elif artist:
                now_playing = '♫ {}'.format(artist)
            elif title:
                now_playing = '♫ {}'.format(title)
            elif internet_radio:
                now_playing = '♫ Internet Radio'

        return now_playing

    def clementine(self, i3s_output_list, i3s_config):
        """
        Get the current "artist - title" and return it.
        """
        response = {'full_text': ''}

        response['cached_until'] = time() + self.cache_timeout
        response['full_text'] = self._getMetadatas()

        return response

if __name__ == "__main__":
    """
    Test this module by calling it directly.
    """
    from time import sleep
    x = Py3status()
    config = {
        'color_good': '#00FF00',
        'color_bad': '#FF0000',
    }
    while True:
        print(x.clementine([], config))
        sleep(1)