From ab35ac8b769b2d9816dffb33a64f2c6f7bd5dd6e Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 05 九月 2024 17:05:55 +0800
Subject: [PATCH] 风行网页版爬虫

---
 src/main/java/com/yeshi/buwan/util/IPUtil.java |  303 +++++++++++++-------------------------------------
 1 files changed, 79 insertions(+), 224 deletions(-)

diff --git a/src/main/java/com/yeshi/buwan/util/IPUtil.java b/src/main/java/com/yeshi/buwan/util/IPUtil.java
index cd8c277..c24b684 100644
--- a/src/main/java/com/yeshi/buwan/util/IPUtil.java
+++ b/src/main/java/com/yeshi/buwan/util/IPUtil.java
@@ -1,24 +1,36 @@
 package com.yeshi.buwan.util;
 
+import com.google.gson.Gson;
+import com.show.api.ShowApiRequest;
+import net.sf.json.JSONObject;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.lionsoul.ip2region.xdb.Searcher;
+
+import javax.servlet.http.HttpServletRequest;
 import java.io.BufferedInputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLEncoder;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.servlet.http.HttpServletRequest;
-
-import com.show.api.ShowApiRequest;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpException;
-import org.apache.commons.httpclient.methods.GetMethod;
-
-import net.sf.json.JSONObject;
 
 public class IPUtil {
+    private static Searcher searcher;
+
+    static {
+        try {
+            URL url = IPUtil.class.getClassLoader().getResource("ip2region.xdb");
+            String path = new File(url.toURI()).getAbsolutePath();
+            byte[] vIndex = Searcher.loadVectorIndexFromFile(path);
+            searcher = Searcher.newWithVectorIndex(path, vIndex);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
     // http://verx.daili666.com/ip/?tid=558287898012218&num=1&operator=2
     public static void changeIp() {
         System.getProperties().setProperty("proxySet", "true"); // 濡傛灉涓嶈缃紝鍙浠g悊IP鍜屼唬鐞嗙鍙f纭�,姝ら」涓嶈缃篃鍙互
@@ -61,177 +73,6 @@
         return result;
     }
 
-    // 娌℃湁鎵惧埌浠g悊IP
-    // 閲嶅簡
-    public static String getDaiLiIP(String area) {
-        HttpClient client = new HttpClient();
-        GetMethod get = null;
-        try {
-            get = new GetMethod(
-
-                    "http://verx.daili666.com/ip/?tid=559511877530791&num=1&operator=2&filter=on&area="
-                            + URLEncoder.encode(area, "UTF-8") + "");
-        } catch (UnsupportedEncodingException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-
-        try {
-            client.executeMethod(get);
-            String result = get.getResponseBodyAsString();
-            sendNoticeEmail(result);
-            LogUtil.i(result);
-            if (!result.contains("娌℃湁鎵惧埌"))
-                return result;
-            else
-                return null;
-        } catch (HttpException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return null;
-    }
-
-    public static String getDaiLiIP1(String area) {
-        HttpClient client = new HttpClient();
-        GetMethod get = null;
-        try {
-            get = new GetMethod(
-
-                    "http://verx.daili666.com/ip/?tid=559186334744019&num=1&area=" + URLEncoder.encode(area, "UTF-8")
-                            + "");
-        } catch (UnsupportedEncodingException e1) {
-            e1.printStackTrace();
-        }
-        try {
-            client.executeMethod(get);
-            String result = get.getResponseBodyAsString();
-            sendNoticeEmail(result);
-            LogUtil.i(result);
-            if (!result.contains("娌℃湁鎵惧埌"))
-                return result;
-            else
-                return null;
-        } catch (HttpException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return null;
-    }
-
-    // 鎴愰兘
-
-    public static String getDaiLiIP2(String area) {
-        HttpClient client = new HttpClient();
-        GetMethod get = null;
-        try {
-            get = new GetMethod("http://verx.daili666.com/ip/?tid=558287898012218&num=1&filter=on&area="
-                    + URLEncoder.encode(area, "UTF-8"));// &area="+
-            // URLEncoder.encode(area,
-            // "UTF-8") + "
-        } catch (Exception e1) {
-            e1.printStackTrace();
-        }
-        try {
-            client.executeMethod(get);
-            String result = get.getResponseBodyAsString();
-            sendNoticeEmail(result);
-            LogUtil.i(result);
-            if (!result.contains("娌℃湁鎵惧埌"))
-                return result;
-            else
-                return null;
-        } catch (HttpException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return null;
-    }
-
-    public static String getDaiLiIP3(String area) {
-        HttpClient client = new HttpClient();
-        GetMethod get = null;
-        try {
-            get = new GetMethod("http://verx.daili666.com/ip/?tid=557489926231876&num=1&filter=on&area="
-                    + URLEncoder.encode(area, "UTF-8") + "");
-        } catch (UnsupportedEncodingException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        try {
-            client.executeMethod(get);
-            String result = get.getResponseBodyAsString();
-            sendNoticeEmail(result);
-            LogUtil.i(result);
-            if (!result.contains("娌℃湁鎵惧埌"))
-                return result;
-            else
-                return null;
-        } catch (HttpException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return null;
-    }
-
-    public static String getDaiLiIP4(String area) {
-        HttpClient client = new HttpClient();
-        GetMethod get = null;
-        try {
-            get = new GetMethod("http://verx.daili666.com/ip/?tid=556436495975069&num=1&area="
-                    + URLEncoder.encode(area, "UTF-8") + "");
-        } catch (UnsupportedEncodingException e1) {
-            // TODO Auto-generated catch block
-            e1.printStackTrace();
-        }
-        try {
-            client.executeMethod(get);
-            String result = get.getResponseBodyAsString();
-            sendNoticeEmail(result);
-            LogUtil.i(result);
-            if (!result.contains("娌℃湁鎵惧埌"))
-                return result;
-            else
-                return null;
-        } catch (HttpException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return null;
-    }
-
-    public static String getDaiLiIP5(String area) {
-        HttpClient client = new HttpClient();
-        GetMethod get = null;
-        try {
-            get = new GetMethod("http://verx.daili666.com/ip/?tid=559418280288916&num=1&filter=on&area="
-                    + URLEncoder.encode(area, "UTF-8") + "");
-
-        } catch (UnsupportedEncodingException e1) {
-            e1.printStackTrace();
-        }
-        try {
-            client.executeMethod(get);
-            String result = get.getResponseBodyAsString();
-            LogUtil.i(result);
-            sendNoticeEmail(result);
-            if (!result.contains("娌℃湁鎵惧埌"))
-                return result;
-            else
-                return null;
-        } catch (HttpException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return null;
-    }
-
     private static void sendNoticeEmail(String get) {
         if (get != null && get.contains("璁㈠崟鍓╀綑鏁伴噺涓嶈冻")) {
             /*
@@ -261,56 +102,70 @@
         return ip;
     }
 
-    public static Map<String, String> getIPInfo(String ip) {
-        Map<String, String> map = new HashMap<String, String>();
-        HttpClient client = new HttpClient();
-        GetMethod method = new GetMethod("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
-        try {
-            client.executeMethod(method);
-            String result = method.getResponseBodyAsString();
-            JSONObject object = JSONObject.fromObject(result);
-            if (object.optInt("code") == 0) {
-                map.put("city", object.optJSONObject("data").optString("city"));
-                map.put("country", object.optJSONObject("data").optString("country"));
-                return map;
-            }
 
-        } catch (HttpException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return map;
+    /**
+     * 鑾峰彇杩滅▼绔彛
+     *
+     * @param request
+     * @return
+     */
+    public static int getRemotePort(HttpServletRequest request) {
+        return request.getRemotePort();
     }
 
-    public static String getIPContry(String ip) {
-        String res = new ShowApiRequest("http://route.showapi.com/2152-1", "49053", "983e020ce60042cd95f39b3ebd4b9563")
-                .addTextPara("ip", ip)
-                .post();
-        JSONObject json = JSONObject.fromObject(res);
-        if (json.optInt("showapi_res_code") == 0) {
-            JSONObject body = json.optJSONObject("showapi_res_body");
-            if (body != null)
-                return body.optString("country");
-        }
-        return "";
+    public static IPInfo getLocalIPInfo(String ip) throws Exception {
+        String info = searcher.search(ip);
+        String[] infos = info.split("\\|");
+        IPInfo ipInfo = new IPInfo(infos[0], infos[2], infos[3]);
+        return ipInfo;
     }
 
-    public static String getIPProvince(String ip) {
+    public static class IPInfo {
+        private String province;
+        private String city;
+        private String country;
+
+        public IPInfo(String country, String province, String city) {
+            this.province = province;
+            this.city = city;
+            this.country = country;
+        }
+
+        public String getProvince() {
+            return province;
+        }
+
+        public void setProvince(String province) {
+            this.province = province;
+        }
+
+        public String getCity() {
+            return city;
+        }
+
+        public void setCity(String city) {
+            this.city = city;
+        }
+
+        public String getCountry() {
+            return country;
+        }
+
+        public void setCountry(String country) {
+            this.country = country;
+        }
+    }
+
+    public static void main(String[] args) {
         try {
-            String res = new ShowApiRequest("http://route.showapi.com/2152-1", "49053", "983e020ce60042cd95f39b3ebd4b9563")
-                    .addTextPara("ip", ip)
-                    .post();
-            JSONObject json = JSONObject.fromObject(res);
-            if (json.optInt("showapi_res_code") == 0) {
-                JSONObject body = json.optJSONObject("showapi_res_body");
-                if (body != null)
-                    return body.optString("state");
-            }
+            IPInfo info =     getLocalIPInfo("113.250.254.8");
+            System.out.println(new Gson().toJson(info));
+            info = getLocalIPInfo("193.112.35.168");
+            System.out.println(new Gson().toJson(info));
         } catch (Exception e) {
-
+            e.printStackTrace();
         }
-        return "";
+
     }
 
 }

--
Gitblit v1.8.0