Skip to content

Commit e6467da

Browse files
committed
解决字幕库抓取限制问题
1 parent eed8647 commit e6467da

File tree

5 files changed

+85
-9
lines changed

5 files changed

+85
-9
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>SubTitleSearcher</groupId>
77
<artifactId>SubTitleSearcher</artifactId>
8-
<version>2.0.3.0</version>
8+
<version>2.0.4.0</version>
99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1111
<copyright>Copyright 2019 pslib.com</copyright>

src/main/java/zimu/AppConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class AppConfig {
77
public static String appName = "SubTitleSearcher";
88
public static String appTitle = "字幕下载";
99
//public static String appTitle = "SubTitleSearcher";
10-
public static String appVer = "2.0.3";
10+
public static String appVer = "2.0.4";
1111

1212
public static String appPath;
1313
public static boolean isExe;

src/main/java/zimu/common/sites/ZIMuKuCommon.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
import cn.hutool.core.codec.Base64;
1111
import cn.hutool.core.util.CharsetUtil;
12+
import cn.hutool.core.util.StrUtil;
1213
import cn.hutool.core.util.URLUtil;
1314
import cn.hutool.http.HttpResponse;
1415
import cn.hutool.json.JSONArray;
1516
import cn.hutool.json.JSONObject;
1617
import cn.hutool.log.Log;
1718
import cn.hutool.log.LogFactory;
19+
import zimu.util.ExeJsUtil;
1820
import zimu.util.HtHttpUtil;
1921
import zimu.util.StringUtil;
2022
import zimu.util.regex.RegexUtil;
@@ -33,9 +35,9 @@ public class ZIMuKuCommon {
3335

3436
public static void main(String[] args) throws Exception {
3537
//System.out.println(DownList("憨豆特工.mkv"));
36-
System.out.println(DownList("downsizing.2017.720p.bluray.x264-geckos.mkv"));
37-
38-
38+
//System.out.println(DownList("downsizing.2017.720p.bluray.x264-geckos.mkv"));
39+
System.out.println(DownList("From.Beijing.with.Love.1994.720p.BluRay.x264-WiKi.mkv"));
40+
//System.out.println(getPageList("From.Beijing.with.Love"));
3941

4042
//System.out.println(downContent("/detail/101779.html"));;
4143
//detail/100250.html
@@ -67,13 +69,13 @@ public static JSONArray DownList(String fileName) throws Exception {
6769
* @return
6870
*/
6971
public static JSONObject downContent(String url) {
70-
String result = HtHttpUtil.http.get(baseUrl+url);
72+
String result = httpGet(baseUrl+url);
7173
String downUrl = RegexUtil.getMatchStr(result,
7274
"<a\\s+id=\"down1\"\\s+href=\"(/dld/[\\w]+\\.html)\""
7375
, Pattern.DOTALL);
7476
if(downUrl == null)return null;
7577
downUrl = baseUrl + downUrl;
76-
result = HtHttpUtil.http.get(downUrl);
78+
result = httpGet(downUrl);
7779
if(result == null)return null;
7880
//System.out.println(result);
7981
JSONArray resList = RegexUtil.getMatchList(result,
@@ -102,7 +104,7 @@ public static JSONObject downContent(String url) {
102104
* @return
103105
*/
104106
public static JSONArray getDetailList(String url) {
105-
String result = HtHttpUtil.http.get(baseUrl+url);
107+
String result = httpGet(baseUrl+url);
106108
//System.out.println(result);
107109
Document doc = Jsoup.parse(result);
108110
Elements matchList = doc.select("#subtb tbody tr");
@@ -189,7 +191,7 @@ public static JSONArray getFuzzyPageList(String title) {
189191
* @return
190192
*/
191193
public static JSONArray getPageList(String title) {
192-
String result = HtHttpUtil.http.get(baseUrl+"/search?q="+URLUtil.encodeAll(title, CharsetUtil.CHARSET_UTF_8));
194+
String result = httpGet(baseUrl+"/search?q="+URLUtil.encodeAll(title, CharsetUtil.CHARSET_UTF_8));
193195
//System.out.println(result);
194196
JSONArray resList = RegexUtil.getMatchList(result, "<p\\s+class=\"tt\\s+clearfix\"><a\\s+href=\"(/subs/[\\w]+\\.html)\"\\s+"
195197
+ "target=\"_blank\"><b>(.*?)</b></a></p>", Pattern.DOTALL);
@@ -200,4 +202,26 @@ public static JSONArray getPageList(String title) {
200202

201203
return resList;
202204
}
205+
206+
public static String httpGet(String url) {
207+
String result = HtHttpUtil.http.get(url);
208+
if(result!=null && StrUtil.count(result, "url")>10 && result.contains("<script")) {
209+
String jsStr = RegexUtil.getMatchStr(result, "<script[^>]*>(.*?)</script>");
210+
jsStr = jsStr.replaceAll("window.location[\\s]*=[\\s]*url", "");
211+
jsStr = jsStr.replaceAll("location[\\s]*=[\\s]*url", "");
212+
if(jsStr==null) {
213+
jsStr = "";
214+
}
215+
String jsVal = null;
216+
try {
217+
jsVal = ExeJsUtil.getJsVal("function getUrl(){"+jsStr+";return url;} getUrl()");
218+
}catch(Exception e) {
219+
logger.error(e);
220+
}
221+
if(jsVal!=null&&jsVal.length()>0) {
222+
return httpGet(jsVal.contains("://") ? jsVal : baseUrl+jsVal);
223+
}
224+
}
225+
return result;
226+
}
203227
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package zimu.tests;
2+
3+
import javax.script.Compilable;
4+
import javax.script.CompiledScript;
5+
import javax.script.ScriptEngine;
6+
import javax.script.ScriptEngineManager;
7+
8+
public class Test2 {
9+
10+
public static void main(String[] args) {
11+
ScriptEngineManager manager = new ScriptEngineManager();
12+
ScriptEngine engine = manager.getEngineByName("JavaScript");
13+
try {
14+
String script = "function getUrl(){var url='adsf';return url;} getUrl()";
15+
Compilable compilable = (Compilable) engine;
16+
CompiledScript JSFunction = compilable.compile(script);
17+
Object result = JSFunction.eval();
18+
System.out.println(result);
19+
} catch (Exception e) {
20+
e.printStackTrace();
21+
}
22+
}
23+
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package zimu.util;
2+
3+
import javax.script.Compilable;
4+
import javax.script.CompiledScript;
5+
import javax.script.ScriptEngine;
6+
import javax.script.ScriptEngineManager;
7+
8+
public class ExeJsUtil {
9+
10+
/**
11+
* 执行js并返回结果
12+
* @param jsStr
13+
* @return
14+
*/
15+
public static String getJsVal(String jsStr) {
16+
ScriptEngineManager manager = new ScriptEngineManager();
17+
ScriptEngine engine = manager.getEngineByName("JavaScript");
18+
try {
19+
Compilable compilable = (Compilable) engine;
20+
CompiledScript JSFunction = compilable.compile(jsStr);
21+
Object result = JSFunction.eval();
22+
return result != null ? result.toString() : null;
23+
} catch (Exception e) {
24+
e.printStackTrace();
25+
}
26+
return null;
27+
}
28+
}

0 commit comments

Comments
 (0)