package hello;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@Controller
public class Application {
	@Autowired
	WebClient webClient;

	@RequestMapping("/")
	@ResponseBody
	public String home() {
		return "Hello Openshift World, enjoy yourself with Openshift.";
	}




	@GetMapping("/vip")
	public String vip(@RequestParam("url") String url, Model model) throws Exception{
		String parseURL=parseVideo(url);
		model.addAttribute("url",parseURL);

//		URL Url=new URL(parseURL);
//		HttpURLConnection connection=(HttpURLConnection) Url.openConnection();
//		connection.setDoInput(true);
//		connection.setDoOutput(true);
//		connection.setInstanceFollowRedirects(true);
//		connection.connect();
//		String contentType=connection.getContentType();
//
//		if(contentType.toLowerCase().contains("application/x-mpegurl")){
//			ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
//			IOUtils.copy(connection.getInputStream(),outputStream);
//			String base64=Base64.getEncoder().encodeToString(outputStream.toByteArray());
//			model.addAttribute("url",String.format("data:%s;base64,%s", contentType,base64));
//			model.addAttribute("type","application/x-mpegurl");
//
//		}
//		model.addAttribute("type",contentType);

//		connection.disconnect();

		return "vip";

	}
	@GetMapping("/vip/api")
    @ResponseBody
    public Map<String,String> vipapi(@RequestParam("url") String url) throws Exception{
        String parseURL=parseVideo(url);
        HashMap<String,String> result=new HashMap();
        result.put("url",parseURL);
        return result;

    }

	private String parseVideo(String url) throws Exception{
		WebRequest webRequest=new WebRequest(new URL("http://api.baiyug.cn/vip_vip/index.php?url="+url));
		webRequest.setAdditionalHeader("Referer","http://api.baiyug.cn/vip/index.php?url="+url);
		HtmlPage p=webClient.getPage(webRequest);
		ScriptResult result=p.executeJavaScript("basea17kdv(ykyun)");
		String text=(String)result.getJavaScriptResult();
		return text;
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}


	public @Bean  WebClient getWebClient(){
		 WebClient webClient=new WebClient(BrowserVersion.CHROME);
         webClient.getOptions().setCssEnabled(false);
		 return webClient;
	}

}

@@ -262,16 +262,54 @@
 mlPage;%0A
+import org.apache.commons.io.IOUtils;%0A
 import o
@@ -883,15 +883,113 @@
 ava.
-net.URL
+io.ByteArrayOutputStream;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.URL;%0Aimport java.util.Base64
 ;%0Aim
@@ -1444,18 +1444,16 @@
 eURL);%0A%0A
-//
 %09%09URL Ur
@@ -1473,18 +1473,16 @@
 seURL);%0A
-//
 %09%09HttpUR
@@ -1546,18 +1546,16 @@
 tion();%0A
-//
 %09%09connec
@@ -1569,34 +1569,32 @@
 tDoInput(true);%0A
-//
 %09%09connection.set
@@ -1601,34 +1601,32 @@
 DoOutput(true);%0A
-//
 %09%09connection.set
@@ -1656,18 +1656,16 @@
 (true);%0A
-//
 %09%09connec
@@ -1680,18 +1680,16 @@
 nect();%0A
-//
 %09%09String
@@ -1730,21 +1730,17 @@
 Type();%0A
-//%0A//
+%0A
 %09%09if(con
@@ -1798,18 +1798,16 @@
 url%22))%7B%0A
-//
 %09%09%09ByteA
@@ -1865,18 +1865,16 @@
 ream();%0A
-//
 %09%09%09IOUti
@@ -1924,18 +1924,16 @@
 tream);%0A
-//
 %09%09%09Strin
@@ -1941,16 +1941,17 @@
  base64=
+ 
 Base64.g
@@ -2089,26 +2089,24 @@
 e,base64));%0A
-//
 %09%09%09model.add
@@ -2127,44 +2127,27 @@
 e%22,%22
-application/x-mpegurl%22);%0A//%0A//%09%09%7D%0A//
+m3u8%22);%0A%0A%09%09%7Delse%7B%0A%09
 %09%09mo
@@ -2174,25 +2174,22 @@
 pe%22,
-contentType);%0A%0A//
+%22mp4%22);%0A%0A%09%09%7D%0A%0A
 %09%09co

update for video player