浏览代码

Now fallsback and tries to use the filename to guess artist/title if otherwise unknown


git-svn-id: http://svn.dmdirc.com/trunk@2867 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Shane Mc Cormack 16 年前
父节点
当前提交
a7adaac782
共有 1 个文件被更改,包括 78 次插入3 次删除
  1. 78
    3
      src/com/dmdirc/addons/mediasource_vlc/VlcMediaSourcePlugin.java

+ 78
- 3
src/com/dmdirc/addons/mediasource_vlc/VlcMediaSourcePlugin.java 查看文件

31
 import com.dmdirc.util.Downloader;
31
 import com.dmdirc.util.Downloader;
32
 
32
 
33
 import java.io.IOException;
33
 import java.io.IOException;
34
+import java.io.File;
34
 import java.net.MalformedURLException;
35
 import java.net.MalformedURLException;
35
 import java.util.HashMap;
36
 import java.util.HashMap;
36
 import java.util.List;
37
 import java.util.List;
70
     /** {@inheritDoc} */
71
     /** {@inheritDoc} */
71
     @Override    
72
     @Override    
72
     public String getArtist() {
73
     public String getArtist() {
73
-        return information.containsKey("artist") ? information.get("artist")
74
+        String result = information.containsKey("artist") ? information.get("artist")
74
                 : "unknown";
75
                 : "unknown";
76
+        
77
+        // Artist is unknown, lets guess using the filename
78
+        if (result.equals("unknown") && information.containsKey("playlist_current")) {
79
+            try {
80
+                final int item = Integer.parseInt(information.get("playlist_current"));
81
+                String[] bits = information.get("playlist_item_"+item).split(File.separator);
82
+                result = bits[bits.length-1];
83
+                bits = result.split("-");
84
+                if (bits.length > 1) {
85
+                    result = bits[0];
86
+                } else {
87
+                    // Whole filename is the title, so no artist is known.
88
+                    result = "";
89
+                }
90
+            } catch (NumberFormatException nfe) {
91
+            }
92
+        }
93
+        return result;
75
     }
94
     }
76
 
95
 
77
     /** {@inheritDoc} */
96
     /** {@inheritDoc} */
78
     @Override    
97
     @Override    
79
     public String getTitle() {
98
     public String getTitle() {
80
-        return information.containsKey("title") ? information.get("title")
99
+        String result = information.containsKey("title") ? information.get("title")
81
                 : "unknown";
100
                 : "unknown";
101
+        
102
+        // Title is unknown, lets guess using the filename
103
+        if (result.equals("unknown") && information.containsKey("playlist_current")) {
104
+            try {
105
+                final int item = Integer.parseInt(information.get("playlist_current"));
106
+                String[] bits = information.get("playlist_item_"+item).split(File.separator);
107
+                result = bits[bits.length-1];
108
+                bits = result.split("-");
109
+                if (bits.length > 1) {
110
+                    result = bits[1];
111
+                } else {
112
+                    // Whole filename is the title, so result is already correct
113
+                }
114
+                // Now to remove the file extension
115
+                bits = result.split("\\.");
116
+                StringBuilder res = new StringBuilder();
117
+                for (int i = 0; i < bits.length-1; i++) {
118
+                    if (res.length() > 0) {
119
+                        res.append(".");
120
+                    }
121
+                    res.append(bits[i]);
122
+                }
123
+                result = res.toString();
124
+            } catch (NumberFormatException nfe) {
125
+            }
126
+        }
127
+        return result;
82
     }
128
     }
83
 
129
 
84
     /** {@inheritDoc} */
130
     /** {@inheritDoc} */
197
             }
243
             }
198
         }
244
         }
199
         
245
         
246
+        boolean isPlaylist = false;
247
+        boolean isCurrent = false;
248
+        boolean isItem = false;
249
+        int playlistItem = 0;
200
         for (String line : res2) {
250
         for (String line : res2) {
201
             final String tline = line.trim();
251
             final String tline = line.trim();
202
             
252
             
203
-            if (tline.startsWith("State:")) {
253
+            if (isPlaylist) {
254
+                if (tline.startsWith("</ul>")) {
255
+                    isPlaylist = false;
256
+                    information.put("playlist_items", Integer.toString(playlistItem));
257
+                } else if (tline.equalsIgnoreCase("<strong>")) {
258
+                    isCurrent = true;
259
+                } else if (tline.equalsIgnoreCase("</strong>")) {
260
+                    isCurrent = false;
261
+                } else if (tline.startsWith("<a href=\"?control=play&amp")) {
262
+                    isItem = true;
263
+                } else if (isItem) {
264
+                    String itemname = tline;
265
+                    if (itemname.endsWith("</a>")) {
266
+                        itemname = itemname.substring(0, itemname.length()-4);
267
+                    }
268
+                    if (!itemname.equals("")) {
269
+                        if (isCurrent) {
270
+                            information.put("playlist_current", Integer.toString(playlistItem));
271
+                        }
272
+                        information.put("playlist_item_"+Integer.toString(playlistItem++), itemname);
273
+                    }
274
+                    isItem = false;
275
+                }
276
+            } else if (tline.equalsIgnoreCase("<!-- Playlist -->")) {
277
+                isPlaylist = true;
278
+            } else if (tline.startsWith("State:")) {
204
                 information.put("state", tline.substring(6, tline.indexOf('<')).trim());
279
                 information.put("state", tline.substring(6, tline.indexOf('<')).trim());
205
             } else if (tline.startsWith("got_")) {
280
             } else if (tline.startsWith("got_")) {
206
                 final int equals = tline.indexOf('=');
281
                 final int equals = tline.indexOf('=');

正在加载...
取消
保存