Procházet zdrojové kódy

Add linux title media source plugin

Change-Id: I8392f2426bfd68d74033560cc8e6822653e3b88c
Reviewed-on: http://gerrit.dmdirc.com/278
Reviewed-by: Gregory Holmes <greboid@dmdirc.com>
Tested-by: Gregory Holmes <greboid@dmdirc.com>
tags/0.6.3
Chris Smith před 14 roky
rodič
revize
32038a72ee

+ 154
- 0
src/com/dmdirc/addons/mediasource_linux_title/TitleMediaSource.java Zobrazit soubor

@@ -0,0 +1,154 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.mediasource_linux_title;
24
+
25
+import com.dmdirc.addons.nowplaying.MediaSource;
26
+import com.dmdirc.addons.nowplaying.MediaSourceState;
27
+import com.dmdirc.logger.ErrorLevel;
28
+import com.dmdirc.logger.Logger;
29
+import com.dmdirc.util.StreamUtil;
30
+
31
+import java.io.BufferedReader;
32
+import java.io.IOException;
33
+import java.io.InputStreamReader;
34
+
35
+/**
36
+ * Provides a media source for Linux players using the `xwininfo` command.
37
+ */
38
+public class TitleMediaSource implements MediaSource {
39
+
40
+    /** The command to use to get the title. */
41
+    private final String command;
42
+
43
+    /** The name of the player we're retrieving. */
44
+    private final String name;
45
+
46
+    /**
47
+     * Creates a new title media source.
48
+     *
49
+     * @param command The command to be executed
50
+     * @param name The name of the media source
51
+     */
52
+    public TitleMediaSource(final String command, final String name) {
53
+        this.command = command;
54
+        this.name = name;
55
+    }
56
+
57
+    /** {@inheritDoc} */
58
+    @Override
59
+    public MediaSourceState getState() {
60
+        if (getInfo().isEmpty()) {
61
+            return MediaSourceState.CLOSED;
62
+        } else if (getInfo().indexOf('-') == -1) {
63
+            return MediaSourceState.STOPPED;
64
+        } else {
65
+            return MediaSourceState.PLAYING;
66
+        }
67
+    }
68
+
69
+    /** {@inheritDoc} */
70
+    @Override
71
+    public String getAppName() {
72
+        return name;
73
+    }
74
+
75
+    /** {@inheritDoc} */
76
+    @Override
77
+    public String getArtist() {
78
+        return getInfo().split("–", 2)[0].trim();
79
+    }
80
+
81
+    /** {@inheritDoc} */
82
+    @Override
83
+    public String getTitle() {
84
+        final String[] info = getInfo().split("–", 2);
85
+        
86
+        if (info.length >= 2) {
87
+            return info[1].trim();
88
+        } else {
89
+            return "";
90
+        }
91
+    }
92
+
93
+    /** {@inheritDoc} */
94
+    @Override
95
+    public String getAlbum() {
96
+        return "";
97
+    }
98
+
99
+    /** {@inheritDoc} */
100
+    @Override
101
+    public String getLength() {
102
+        return "";
103
+    }
104
+
105
+    /** {@inheritDoc} */
106
+    @Override
107
+    public String getTime() {
108
+        return "";
109
+    }
110
+
111
+    /** {@inheritDoc} */
112
+    @Override
113
+    public String getFormat() {
114
+        return "";
115
+    }
116
+
117
+    /** {@inheritDoc} */
118
+    @Override
119
+    public String getBitrate() {
120
+        return "";
121
+    }
122
+    
123
+    private String getInfo() {
124
+        InputStreamReader reader = null;
125
+        BufferedReader input = null;
126
+        Process process;
127
+        
128
+        try {
129
+            final String[] args = new String[]{
130
+                "/bin/bash", "-c", "xwininfo -root -tree | " + command
131
+            };
132
+
133
+            process = Runtime.getRuntime().exec(args);
134
+            
135
+            reader = new InputStreamReader(process.getInputStream());
136
+            input = new BufferedReader(reader);
137
+            
138
+            String line = "";
139
+            
140
+            while ((line = input.readLine()) != null) {
141
+                return line;
142
+            }
143
+        } catch (IOException ex) {
144
+            Logger.userError(ErrorLevel.LOW, "Unable to retrieve media source info",
145
+                    ex);
146
+        } finally {
147
+            StreamUtil.close(reader);
148
+            StreamUtil.close(input);
149
+        }
150
+        
151
+        return "";
152
+    }
153
+
154
+}

+ 68
- 0
src/com/dmdirc/addons/mediasource_linux_title/TitleMediaSourcePlugin.java Zobrazit soubor

@@ -0,0 +1,68 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.mediasource_linux_title;
24
+
25
+import com.dmdirc.addons.nowplaying.MediaSource;
26
+import com.dmdirc.addons.nowplaying.MediaSourceManager;
27
+import com.dmdirc.plugins.Plugin;
28
+
29
+import java.util.ArrayList;
30
+import java.util.List;
31
+
32
+/**
33
+ * A media source plugin which provides two {@link TitleMediaSource}s, one for
34
+ * the Last.fm linux client and one for Spotify (running under Wine).
35
+ *
36
+ * @author chris
37
+ */
38
+public class TitleMediaSourcePlugin extends Plugin implements MediaSourceManager {
39
+
40
+    /** The sources to be returned. */
41
+    private List<MediaSource> sources = null;
42
+
43
+    /** {@inheritDoc} */
44
+    @Override
45
+    public void onLoad() {
46
+        sources = new ArrayList<MediaSource>(2);
47
+        sources.add(new TitleMediaSource("grep -E '\\(\"last\\.?fm\" \"Last\\.?fm\"\\)'" +
48
+                    "| grep -vE '(\"Last.fm " +
49
+                    "Options\"|\"Diagnostics\"|\"last\\.?fm\"|\"Share\"|\\(has no " +
50
+                    "name\\)):' | sed -r 's/^[^\"]*?\"(.*)\": \\(\"last\\.?fm.*$/\\1/g'", "Last.fm"));
51
+        sources.add(new TitleMediaSource("grep '\": (\"spotify.exe' | cut -d '\"' -f 2 | "
52
+                    + "cut -d '-' -f 2- | sed -r 's/^\\s+|\\s+$//g' | sed -r 's/–/-/g'", "Spotify"));
53
+    }
54
+
55
+    /** {@inheritDoc} */
56
+    @Override
57
+    public void onUnload() {
58
+        sources.clear();
59
+        sources = null;
60
+    }
61
+
62
+    /** {@inheritDoc} */
63
+    @Override
64
+    public List<MediaSource> getSources() {
65
+        return sources;
66
+    }
67
+
68
+}

+ 34
- 0
src/com/dmdirc/addons/mediasource_linux_title/plugin.config Zobrazit soubor

@@ -0,0 +1,34 @@
1
+# This is a DMDirc configuration file.
2
+
3
+# This section indicates which sections below take key/value
4
+# pairs, rather than a simple list. It should be placed above
5
+# any sections that take key/values.
6
+keysections:
7
+  metadata
8
+  updates
9
+  version
10
+  requires
11
+
12
+metadata:
13
+  author=Chris <chris@dmdirc.com>
14
+  mainclass=com.dmdirc.addons.mediasource_linux_title.TitleMediaSourcePlugin
15
+  description=Provides a media source for the Linux Last.fm and Spotify clients
16
+  name=linuxtitlemediasource
17
+  nicename=Linux Title Media Source
18
+
19
+updates:
20
+  id=27
21
+
22
+version:
23
+  friendly=0.1
24
+
25
+provides:
26
+  spotify mediasource
27
+  lastfm mediasource
28
+
29
+requires:
30
+  os=linux
31
+  files=/bin/bash
32
+
33
+required-services:
34
+  mediasource manager

Načítá se…
Zrušit
Uložit