-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSources.java
More file actions
126 lines (110 loc) · 4.1 KB
/
Copy pathSources.java
File metadata and controls
126 lines (110 loc) · 4.1 KB
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
package proxies;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Sources {
Proxies p;
public Sources(Proxies p){
this.p = p;
}
public interface Parse {
public List<ProxyClass> process(String t, Object obj);
}
public class Source{
String name;
int interval;
String url;
boolean go = false;
Parse parse;
boolean json = false;
public Source(String name, String url, int interval, boolean json, Parse parse){
this.name = name;
this.interval = interval;
this.url = url;
this.parse = parse;
this.json = json;
}
private String getDoc(){
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(this.url);
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(connect.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException ex) {
//Logger.getLogger(Proxies.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
return sb.toString();
}
private Object getJSON(String doc){
Object obj = null;
try {
obj = JsonParser.any().from(doc);
} catch (JsonParserException ex) {
//Logger.getLogger(Sources.class.getName()).log(Level.SEVERE, null, ex);
}
return obj;
}
public void scan(){
new Thread(){
@Override
public void run(){
while(go){
System.out.println("refreshing " + name);
String doc = getDoc();
if(doc == null)continue;
Object obj = null;
if(json) obj = getJSON(doc);
List<ProxyClass> proxies = parse.process(doc, obj);
addProxies(proxies);
// System.out.println("Thread-" + Thread.currentThread().getId());
// System.out.println(proxies);
// System.out.println(p.proxies.get("all").size());
try {
Thread.sleep(interval);
} catch (InterruptedException ex) {
Logger.getLogger(Sources.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}.start();
}
public void setGo(boolean go){
this.go = go;
}
}
protected final LinkedHashMap<String, Source> sources = new LinkedHashMap<>();
public void addSource(String name, String url, int interval, boolean json, Parse p){
addSource(name, new Source(name, url, interval, json, p));
}
public void addSource(String name, Source s){
sources.put(name, s);
}
public void addProxies(List<ProxyClass> list){
//synchronized(p.proxies){
for(ProxyClass e : list){
String key = e.proxy;
p.proxies.put(key, e); //?updating information if different from duplicate proxies
}
//}
}
public void startAll(){
for(Entry<String, Source> e : sources.entrySet()){
e.getValue().setGo(true);
e.getValue().scan();
}
}
}