forked from cqr/PlayerHater
/
PlaylistParser.java
112 lines (99 loc) · 3.09 KB
/
PlaylistParser.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package org.prx.playerhater.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import android.net.Uri;
public final class PlaylistParser {
private static String[] PLS_MIME_TYPES = new String[] { "audio/scpls",
"audio/x-scpls" };
private static String[] M3U_MIME_TYPES = new String[] { "audio/x-mpegurl" };
public static Uri[] parsePlaylist(Uri uri) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpHead(uri
.toString()));
Header contentType = response.getEntity().getContentType();
if (contentType != null) {
String mimeType = contentType.getValue().split(";")[0].trim();
for (String plsMimeType : PLS_MIME_TYPES) {
if (plsMimeType.equalsIgnoreCase(mimeType)) {
return parsePls(uri);
}
}
for (String m3uMimeType : M3U_MIME_TYPES) {
if (m3uMimeType.equalsIgnoreCase(mimeType)) {
return parseM3u(uri);
}
}
}
} catch (Exception e) {}
return new Uri[] { uri };
}
private static Uri[] parsePls(Uri uri) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(uri.toString()));
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
String header = reader.readLine();
if (header.trim().equalsIgnoreCase("[playlist]")) {
String line;
ArrayList<Uri> uriList = new ArrayList<Uri>();
do {
line = reader.readLine();
if (line != null) {
if (line.startsWith("File")) {
String fileName = line.substring(
line.indexOf("=") + 1).trim();
uriList.add(Uri.parse(fileName));
}
}
} while (line != null);
if (uriList.size() > 0) {
Uri[] res = new Uri[uriList.size()];
return uriList.toArray(res);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return new Uri[] { uri };
}
private static Uri[] parseM3u(Uri uri) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(uri
.toString()));
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
String line;
ArrayList<Uri> uriList = new ArrayList<Uri>();
do {
line = reader.readLine();
if (line != null) {
if (!line.startsWith("#")) {
uriList.add(Uri.parse(line.trim()));
}
}
} while (line != null);
if (uriList.size() > 0) {
Uri[] res = new Uri[uriList.size()];
return uriList.toArray(res);
}
} catch (Exception e) {
}
return new Uri[] { uri };
}
}