最近本人在玩抖音睡,看到好看的视频想保存下载分享,所以无聊自己写了个脚本,仅供参考学习,请勿用于商业用途!
java版:
  1. public class DYDecode {
  2. public static String urlAnalysisMethod(String url) {
  3. try {
  4. if (isContainChinese(url)) {
  5. url = cuthttpschinese(url);
  6. }
  7. if (url.length() < 40) {
  8. url = getURI(url);
  9. }
  10. return url;
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. return null;
  14. }
  15. }
  16. public static String NewUrlDecode(String url) {
  17. Document doc = null;
  18. try {
  19. doc = Jsoup.connect(url).timeout(5000).get();
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. // System.out.println(doc.data());
  24. Elements elem = doc.getElementsByTag("script");
  25. String url1 = elem.toString();
  26. int start = url1.indexOf("playAddr");
  27. url1 = url1.substring(start);
  28. int end = url1.indexOf("\",");
  29. return url1.substring(11, end).replaceAll("playwm", "play");
  30. }
  31. // 获取封面,项目要用到
  32. public static String getCover(String url) {
  33. Document doc = null;
  34. try {
  35. doc = Jsoup.connect(url).timeout(5000).get();
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. Elements elem = doc.getElementsByTag("script");
  40. String url1 = elem.toString();
  41. int start = url1.indexOf("cover");
  42. String str = url1.substring(start);
  43. int end = str.indexOf("}");
  44. return str.substring(8, end).replaceAll("\"", "");
  45. }
  46. /**
  47. * @param url
  48. * @return
  49. */
  50. // 获取真实地址
  51. public static String getURI(String url) {
  52. HttpClient httpClient = new DefaultHttpClient();
  53. HttpContext httpContext = new BasicHttpContext();
  54. HttpGet httpGet = new HttpGet(url);
  55. try {
  56. // 将HttpContext对象作为参数传给execute()方法,则HttpClient会把请求响应交互过程中的状态信息存储在HttpContext
  57. HttpResponse response = httpClient.execute(httpGet, httpContext);
  58. // 获取重定向之后的主机地址信息
  59. HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
  60. // 获取实际的请求对象的URI,即重定向之后的地址
  61. HttpUriRequest realRequest = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);
  62. return (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST)
  63. + ((HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST)).getURI().toString();
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. return null;
  67. } finally {
  68. httpClient.getConnectionManager().shutdown();
  69. }
  70. }
  71. /**
  72. * @param str
  73. * @return
  74. */
  75. // 检查是否有中文
  76. public static boolean isContainChinese(String str) {
  77. Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
  78. Matcher m = p.matcher(str);
  79. if (m.find()) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. /**
  85. *
  86. * @param str
  87. * @return
  88. */
  89. // 截取到http开始的字段
  90. public static String cuthttpschinese(String str) {
  91. int start = str.indexOf("http");
  92. return str.substring(start);
  93. }
  94. }
想要成品源码的朋友请联系我,谢谢。