Przeglądaj źródła

Add moc.py, plugin for Music On Console

As moc-titler ported to Python/gmon

  [1]: https://github.com/AloisMahdal/moc-titler
Alois Mahdal 10 lat temu
rodzic
commit
690a1716f4
1 zmienionych plików z 68 dodań i 0 usunięć
  1. 68
    0
      plugins/moc.py

+ 68
- 0
plugins/moc.py Wyświetl plik

@@ -0,0 +1,68 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+import gmon
4
+
5
+import subprocess
6
+
7
+
8
+class TrackData(object):
9
+
10
+    _field_names = ['state', 'artist', 'album', 'song', 'ct', 'tt']
11
+
12
+    def __init__(self):
13
+        self.state = ""
14
+        self.artist = ""
15
+        self.album = ""
16
+        self.song = ""
17
+        self.ct = ""    # current time
18
+        self.tt = ""    # total time
19
+
20
+    def load(self):
21
+        for fieldname in self.__class__._field_names:
22
+            value = subprocess.check_output(['mocp', '-Q', "%" + fieldname])
23
+            setattr(self, fieldname, value.strip())
24
+
25
+
26
+class MocTitler(object):
27
+
28
+    def __init__(self):
29
+        self.title = "(unknown)"
30
+        self.td = TrackData()
31
+
32
+    def read_data(self):
33
+        self.td.load()
34
+
35
+    def mocp_running(self):
36
+        cmd = 'ps -eo comm | grep -q mocp'
37
+        return subprocess.call(cmd, shell=True) == 0
38
+
39
+    def assemble_title(self):
40
+        """Assemble the data to human-readable format"""
41
+        d = self.td
42
+        title = ""
43
+        title += "(%s) " % d.state
44
+        title += d.artist if d.artist else ""
45
+        title += " " if d.artist and d.album else ""
46
+        title += "[%s]" % d.album if d.album else ""
47
+        if (d.artist or d.album) and (d.song or d.tt):
48
+            title += ": "
49
+        title += d.song if d.song else ""
50
+        title += " (%s/%s)" % (d.ct, d.tt) if d.tt else ""
51
+
52
+        return title
53
+
54
+    def get_title(self):
55
+        """Get title or explaining message"""
56
+        try:
57
+            self.td.load()
58
+            return self.assemble_title()
59
+        except subprocess.CalledProcessError:
60
+            return "(not running)"
61
+
62
+
63
+class Plugin(gmon.BasePlugin):
64
+
65
+    name = "moc"
66
+
67
+    def render_plain(self):
68
+        return MocTitler().get_title()