package tk.yourchanges.clicker.proxy;
import tk.yourchanges.clicker.proxy.Proxy;
import tk.yourchanges.clicker.proxy.ProxyGetter;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class CNProxyGetter implements ProxyGetter {
private static final Map<String, String> REPLACEMENTS = new HashMap<String, String>();
static {
final String text = "v=3;m=4;a=2;l=9;q=0;b=5;i=7;w=6;r=8;c=1";
final String[] kvs = text.split(";");
for (int i = 0; i < kvs.length; i++) {
final String kv = kvs[i];
final String k = kv.split("=")[0];
final String v = kv.split("=")[1];
REPLACEMENTS.put(k, v);
}
}
private Properties properties;
@Override
public void setProperties(Properties prop) {
this.properties = prop;
}
public CNProxyGetter(Properties properties){
this.properties = properties;
}
@Override
public String getName() {
return CNProxyGetter.class.getSimpleName();
}
@Override
public Set<Proxy> find() {
final Set<Proxy> ret = new HashSet<Proxy>();
for (int i = 1; i < 11; i++) {
try {
final Document doc = Jsoup.parse(new URL("http://www.cnproxy.com/proxy" + i + ".html"), TIMEOUT);
final Elements tables = doc.getElementsByTag("table");
final Element table = tables.get(2);
final Elements trs = table.getElementsByTag("tr");
for (int j = 1; j < trs.size(); j++) {
final Element tr = trs.get(j);
try {
final Element td = tr.getElementsByTag("td").get(0);
final String host = td.text();
String port = td.getElementsByTag("script").get(0).data();
port = port.replace("document.write(", "").replace("\"", "").replace(")", "").replace("+", "").replace(":", "");
for (final Map.Entry<String, String> replacement : REPLACEMENTS.entrySet()) {
port = port.replaceAll(replacement.getKey(), replacement.getValue());
}
final Proxy proxy = new Proxy(host, Integer.valueOf(port),this.properties);
ret.add(proxy);
} catch (final Exception e) {
}
}
} catch (final Exception e) {
}
}
return ret;
}
}