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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern;
public class GetData2Json { private static List<String> urlList ; private static List<Server> serverList = new ArrayList<Server>(); static{ urlList = new ArrayList<String>(); urlList.add("http://www.ishadowsocks.net"); urlList.add("https://freessr.xyz"); } public static String getHTML(String url){ StringBuffer sb = new StringBuffer(); BufferedReader br; String line = null; try{ URL url1 = new URL(url); URLConnection conn = url1.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"); conn.connect(); br = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = br.readLine()) != null) { sb.append(line); sb.append("\n"); } }catch (Exception e){ e.printStackTrace(); }finally { return sb.toString(); }
} private static void initData(){ String re = " <h4>[ABC]服务器地址:([^<]*)</h4>\n" + " <h4>端口:([^<]*)</h4>\n" + "<h4>[ABC]密码:([^<]*)</h4>\n" + " <h4>加密方式:([^<]*)</h4>\n"; String HTML; HTML = getHTML(urlList.get(0)); Pattern p = Pattern.compile(re); Matcher m = p.matcher(HTML); while(m.find()){ serverList.add(new Server(m.group(1),m.group(2),m.group(3),m.group(4))); } re = "\\s*<h4>[A-Z]*服务器地址:([^<]*)</h4>\n" + "\\s*<h4>端口:([^<]*)</h4>\n" + "\\s*<h4>密码:([^<]*)</h4>\n" + "\\s*<h4>加密方式:([^<]*)</h4>";
HTML = getHTML(urlList.get(1)); Pattern p2 = Pattern.compile(re); Matcher m2 = p2.matcher(HTML); while(m2.find()){ serverList.add(new Server(m2.group(1),m2.group(2),m2.group(3),m2.group(4))); } } public static void getJson() { String os = System.getProperty("os.name").toLowerCase(); initData(); StringBuffer sb = new StringBuffer(); for (int i = 0;i<serverList.size();i++){ sb.append(serverList.get(i)); if(i<serverList.size()-1){ sb.append(",\n"); }else{ sb.append("\n"); } } if(os.contains("windows")){ StringBuffer sb1 = new StringBuffer(); sb1.append("{\n" + "\"configs\" : [\n"); sb1.append(sb.toString()); sb1.append("],\n" + " \"strategy\": null,\n" + " \"index\": 0,\n" + " \"global\": true,\n" + " \"enabled\": true,\n" + " \"shareOverLan\": false,\n" + " \"isDefault\": false,\n" + " \"localPort\": 1080,\n" + " \"pacUrl\": null,\n" + " \"useOnlinePac\": false,\n" + " \"availabilityStatistics\": false,\n" + " \"autoCheckUpdate\": true,\n" + " \"isVerboseLogging\": false,\n" + " \"logViewer\": {\n" + " \"fontName\": \"Consolas\",\n" + " \"fontSize\": 8.0,\n" + " \"bgColor\": \"black\",\n" + " \"textColor\": \"white\",\n" + " \"topMost\": false,\n" + " \"wrapText\": false,\n" + " \"toolbarShown\": false,\n" + " \"width\": 600,\n" + " \"height\": 400,\n" + " \"top\": 328,\n" + " \"left\": 766,\n" + " \"maximized\": true\n" + " },\n" + " \"proxy\": {\n" + " \"useProxy\": false,\n" + " \"proxyType\": 0,\n" + " \"proxyServer\": \"\",\n" + " \"proxyPort\": 0,\n" + " \"proxyTimeout\": 3\n" + " },\n" + " \"hotkey\": {\n" + " \"SwitchSystemProxy\": \"\",\n" + " \"ChangeToPac\": \"\",\n" + " \"ChangeToGlobal\": \"\",\n" + " \"SwitchAllowLan\": \"\",\n" + " \"ShowLogs\": \"\",\n" + " \"ServerMoveUp\": \"\",\n" + " \"ServerMoveDown\": \"\"\n" + " }\n" + "}"); try { FileWriter fw = new FileWriter("gui-config.json"); fw.write(sb1.toString()); fw.close(); } catch (IOException e) { e.printStackTrace(); } }else if(os.contains("linux")){ try { FileWriter fw = new FileWriter(".config.json"); fw.write(serverList.get(4).toString()); fw.close(); } catch (IOException e) { e.printStackTrace(); } }else{ System.out.println("there is no solution yet!"); }
} }
|