package edu.neusoft.project.util.GetMusic;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetSongFromNetease {
    public static AtomicInteger count=new AtomicInteger(0);
    public static String getMusician(int id){
        Document doc=null;
        try {
            doc = Jsoup.connect("https://music.163.com/song?id="+id)
                    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
                    .header("Accept-Encoding", "gzip, deflate, br")
                    .header("Accept-Language", "zh-CN,zh;q=0.8")
                    .header("Referer", "https://music.163.com/")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
                    .timeout(5000)
                    .get();
        } catch (IOException e) {
        }
        if(doc==null){
            System.out.println("musician");
            return "aaaaa";
        }
        return doc.select("a[href*=/artist]").html();
    }
//    #auto-id-XUeE9g9eBF6UQtLc > div.g-bd4.f-cb > div.g-mn4 > div > div > div.m-lycifo > div.f-cb > div.cnt > p:nth-child(2) > span > a

    public static String getMusicName(int id){
        Document doc=null;
        try {
            doc = Jsoup.connect("https://music.163.com/song?id="+id)
                    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
                    .header("Accept-Encoding", "gzip, deflate, br")
                    .header("Accept-Language", "zh-CN,zh;q=0.8")
                    .header("Referer", "https://music.163.com/")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
                    .timeout(5000)
                    .get();
        } catch (IOException e) {
        }
        if(doc==null){
            System.out.println("musician");
            return "aaaaa";
        }
        return doc.select("em[class*=f-ff2]").html();
    }
    public static String getMusicCommnetCount(int id){
        Document doc=null;
        try {
            doc = Jsoup.connect("https://music.163.com/song?id="+id)
                    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
                    .header("Accept-Encoding", "gzip, deflate, br")
                    .header("Accept-Language", "zh-CN,zh;q=0.8")
                    .header("Referer", "https://music.163.com/")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
                    .timeout(5000)
                    .get();
        } catch (IOException e) {
        }
        if(doc==null){
            System.out.println("musician");
            return "aaaaa";
        }
        System.out.println(doc);
        System.out.println("------------------------------------");
        System.out.println("------------------------------------");
        System.out.println("------------------------------------");
//        #auto-id-k4oobOfXGrmC2px0 > div.u-title.u-title-1 > span > span
//        *[@id="auto-id-k4oobOfXGrmC2px0"]/div[1]/span/span
//        document.querySelector('#auto-id-k4oobOfXGrmC2px0 > div.u-title.u-title-1 > span > span')

        return doc.select("#auto-id-k4oobOfXGrmC2px0 > div.u-title.u-title-1 > span > span").text();
    }
    public static int getCommentCount(int id){
        Document doc=null;
        try {
            doc = Jsoup.connect("https://music.163.com/weapi/v1/resource/comments/R_SO_4_"+id+"?csrf_token=")
                    .header("Accept", "*/*")
                    .header("Accept-Encoding", "gzip, deflate")
                    .header("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                    .header("Referer", "http://music.163.com/")
                    //.header("X-Forwarded-For", "108.61.199.82,103.50.168.37,172.21.58.18")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
                    .timeout(5000)
                    .data("params", "zMkmTNwNyxd4lLPnUS5J0hN9TES07xhkSTO//SRGNp7IiNlOllaTFmbDRA5cFriarOTgR2VcArRAdEOXO911eUknpOmqAYiQE/YKPMQvenKllTOoZP1x7qM4C14ZNcOL56ZD2BWhiAS5oI5pKsfb30robX6R5czEvCL3F2T7E2XqSwXvJ5nFUGe+0HwKaPzi")
                    .data("encSecKey","3dc1351d7cd37509e61d2a6bd5452282966743ca34d4039627b78e3a13060133cd9418417e4755e9b8f303406e08d07e3cdb231843849396b0a55eaaeb84e4b3b44cf1366bd796b6192996dbb13795f1327f1fb7f7c4c4a48ec22d41e2f38b6417c87f66ccc6ee9d5121dc966c514ef6a3e58ea73f0054aa3242aee84f65ebc0")
                    .post();
        } catch (IOException e) {
        }
        if(doc==null){
            System.out.println("评论数目获取失败");
            count.incrementAndGet();
            return -1;
        }
        String string=doc.toString();
        Pattern pa=Pattern.compile("total\":\\d+");
        Matcher ma=pa.matcher(string);
        ma.find();
        try{
            string=ma.group();
        }catch(Exception e){
            return 0;
        }
        return Integer.parseInt(string.split(":")[1]);
    }
}
