-
Notifications
You must be signed in to change notification settings - Fork 0
/
TxtSubscribe.java
135 lines (126 loc) · 5.26 KB
/
TxtSubscribe.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.github.catvod.live;
import com.github.catvod.utils.okhttp.OkHttpUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
public class TxtSubscribe {
public static void subscribe(LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>> allLives, String url, HashMap<String, String> headers) {
String content = OkHttpUtil.string(url, headers);
parse(allLives, content);
}
public static void parse(LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>> allLives, String txt) {
try {
BufferedReader br = new BufferedReader(new StringReader(txt));
String line = br.readLine();
LinkedHashMap<String, ArrayList<String>> noGroup = new LinkedHashMap<>();
LinkedHashMap<String, ArrayList<String>> groupLives = noGroup;
while (line != null) {
if (line.trim().isEmpty()) {
line = br.readLine();
continue;
}
String[] lineInfo = line.split(",");
if (lineInfo.length < 2) {
line = br.readLine();
continue;
}
if (line.contains("#genre#")) {
String group = lineInfo[0].trim();
if (!allLives.containsKey(group)) {
groupLives = new LinkedHashMap<>();
allLives.put(group, groupLives);
} else {
groupLives = allLives.get(group);
}
} else {
String title = lineInfo[0].trim();
String[] urlMix = lineInfo[1].trim().split("#");
for (int j = 0; j < urlMix.length; j++) {
String url = urlMix[j].trim();
if (url.isEmpty())
continue;
if (url.startsWith("http") || url.startsWith("rtsp") || url.startsWith("rtmp")) {
ArrayList<String> urls = null;
if (!groupLives.containsKey(title)) {
urls = new ArrayList<>();
groupLives.put(title, urls);
} else {
urls = groupLives.get(title);
}
if (!urls.contains(url))
urls.add(url);
} else {
// SpiderDebug.log("Skip " + url);
}
}
}
line = br.readLine();
}
br.close();
if (!noGroup.isEmpty()) {
allLives.put("未分组", noGroup);
}
} catch (Throwable th) {
}
}
public static String live2Json(LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>> lives) {
JSONArray groups = new JSONArray();
Iterator<String> groupKeys = lives.keySet().iterator();
while (groupKeys.hasNext()) {
String group = groupKeys.next();
JSONArray channels = new JSONArray();
LinkedHashMap<String, ArrayList<String>> allChannel = lives.get(group);
if (allChannel.isEmpty())
continue;
Iterator<String> channelKeys = allChannel.keySet().iterator();
while (channelKeys.hasNext()) {
String channel = channelKeys.next();
ArrayList<String> allUrls = allChannel.get(channel);
if (allUrls.isEmpty())
continue;
JSONArray urls = new JSONArray();
for (int i = 0; i < allUrls.size(); i++) {
urls.put(allUrls.get(i));
}
JSONObject newChannel = new JSONObject();
try {
newChannel.put("name", channel);
newChannel.put("urls", urls);
} catch (JSONException e) {
}
channels.put(newChannel);
}
JSONObject newGroup = new JSONObject();
try {
newGroup.put("group", group);
newGroup.put("channels", channels);
} catch (JSONException e) {
}
groups.put(newGroup);
}
return groups.toString();
}
public static Object[] load(String ext) {
try {
LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>> allLives = new LinkedHashMap<>();
TxtSubscribe.subscribe(allLives, ext, null);
String json = TxtSubscribe.live2Json(allLives);
Object[] result = new Object[3];
result[0] = 200;
result[1] = "text/plain; charset=utf-8";
ByteArrayInputStream baos = new ByteArrayInputStream(json.getBytes("UTF-8"));
result[2] = baos;
return result;
} catch (Throwable th) {
th.printStackTrace();
}
return null;
}
}