ali m3u8 url

package cn.sixjo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class AliEduDownloadUrl {
	public static void main(String[] args) throws Exception{
		String cookie = "";
		String courseUrl = "https://edu.aliyun.com/course/53?spm=5176.10731334.0.0.63c26580Q6fqyg";
		String spm = courseUrl.substring(courseUrl.indexOf("spm"));
		List<Map<String,String>> courseList = getClassList(courseUrl, cookie);
		for(Map<String,String> map : courseList) {
			String m3u8BaseUrl = "";
			try {
				m3u8BaseUrl = getM3u8BaseUrl(courseUrl, cookie, map.get("url"), spm);
				
			} catch (Exception e) {
				continue;
			}
			
			String m3u8DownloadUrl = getM3u8DownloadUrl(m3u8BaseUrl);
			System.out.println(m3u8DownloadUrl+","+map.get("name"));
			
		}
	}
	
	private static List<Map<String, String>> getClassList(String courseUrl,String cookie) throws Exception{
		HttpGet httpGet = new HttpGet(courseUrl);
		httpGet = initHttpGet(httpGet, cookie);
		 
		
		String html = getResponseText(httpGet);
		Document doc = Jsoup.parse(html,"utf-8");
		
		Element root = doc.getElementById("course-item-list");
		Elements classList = root.getElementsByClass("course-lesson");
		List<Map<String, String>> courseList = new ArrayList<Map<String,String>>();
		int i = 0;
		for(Element tagA:classList) {
			Map<String, String> resultMap = new HashMap<String, String>();
			String url = tagA.attr("href");
			String name = tagA.attr("title");
			name = name.replaceAll(" ", "").replaceAll("\\(", "\\\\(")
					.replaceAll("\\)", "\\\\)").replaceAll("&", "AND")
					.replaceAll("amp;", "").replaceAll(":", ".").replaceAll("/","");
			name = (++i) + "." + name;
			resultMap.put("url", url);
			resultMap.put("name", name);
			courseList.add(resultMap);
		}
		return courseList;
	}
	
	private static String getM3u8BaseUrl(String courseUrl,String cookie,String singleUrl,String spm) throws Exception{
		String[] courseInfo = singleUrl.split("_");
		
		// /lesson_1795_15045
//		HttpGet httpGet = new HttpGet("https://edu.aliyun.com/course/1795/lesson/15044/player");
		HttpGet httpGet = new HttpGet("https://edu.aliyun.com/course/"+courseInfo[1]+"/lesson/"+courseInfo[2]+"/player");
		
		httpGet = initHttpGet(httpGet, cookie);
		
		httpGet.setHeader("Referer", "https://edu.aliyun.com/"+singleUrl+"?"+spm);
		 
		String html = getResponseText(httpGet);
		Document doc = Jsoup.parse(html,"utf-8");
		
		Element root = doc.getElementById("lesson-video-content");
		return root.attr("data-url");
	}
	
	private static String getM3u8DownloadUrl(String m3u8BaseUrl) throws Exception{
		
		HttpGet httpGet = new HttpGet(m3u8BaseUrl);
		
		String responseText = getResponseText(httpGet);
		
		String[] downLoadUrls = responseText.split("\n");
		return downLoadUrls[downLoadUrls.length-1];
	}
	private static String getResponseText(HttpGet httpGet) throws Exception{
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		CloseableHttpResponse response = null;
		response = httpClient.execute(httpGet);
		HttpEntity responseEntity = response.getEntity();
		return EntityUtils.toString(responseEntity);
	}
	private static HttpGet initHttpGet(HttpGet httpGet,String cookie) {
		httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
		httpGet.setHeader("Accept-Encoding", "gzip, deflate, br");
		httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
		httpGet.setHeader("Connection", "keep-alive");
		httpGet.setHeader("DNT", "1");
		httpGet.setHeader("Host", "edu.aliyun.com");
		httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36");
		httpGet.setHeader("Upgrade-Insecure-Requests", "1");
		httpGet.setHeader("Cookie",cookie);
		return httpGet;
	}
}
相關文章
相關標籤/搜索