index.php 8.6 KB
Newer Older
Z
Zain 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
<?php
error_reporting(0);
class API{

    // General
    protected $_MINI_MODE=false;
    protected $_MODULUS='00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7';
    protected $_NONCE='0CoJUm6Qyw8W8jud';
    protected $_PUBKEY='010001';
    protected $_VI='0102030405060708';
    protected $_USERAGENT='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.157 Safari/537.36';
    protected $_COOKIE='os=pc; osver=Microsoft-Windows-10-Professional-build-10586-64bit; appver=2.0.3.131777; channel=netease; __remember_me=true;';
    protected $_REFERER='http://music.163.com/';
    // use static secretKey, without RSA algorithm
    protected $_secretKey='TA3YiYCfY2dDJQgg';
    protected $_encSecKey='84ca47bca10bad09a6b04c5c927ef077d9b9f1e37098aa3eac6ea70eb59df0aa28b691b7e75e4f1f9831754919ea784c8f74fbfadf2898b0be17849fd656060162857830e241aba44991601f137624094c114ea8d17bce815b0cd4e5b8e2fbaba978c6d1d14dc3d1faf852bdd28818031ccdaaa13a6018e1024e2aae98844210';

    // encrypt mod
    protected function prepare($raw){
        $data['params']=$this->aes_encode(json_encode($raw),$this->_NONCE);
        $data['params']=$this->aes_encode($data['params'],$this->_secretKey);
        $data['encSecKey']=$this->_encSecKey;
        return $data;
    }
    protected function aes_encode($secretData,$secret){
        return openssl_encrypt($secretData,'aes-128-cbc',$secret,false,$this->_VI);
    }

    // CURL
    protected function curl($url,$data=null,$cookie=false){
        $curl=curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        if($data){
            if(is_array($data))$data=http_build_query($data);
            curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
            curl_setopt($curl,CURLOPT_POST,1);
        }
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl,CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($curl,CURLOPT_REFERER,$this->_REFERER);
        curl_setopt($curl,CURLOPT_COOKIE,$this->_COOKIE."__csrf=".$_COOKIE["__csrf"]."; MUSIC_U=".$_COOKIE["MUSIC_U"]);
        curl_setopt($curl,CURLOPT_USERAGENT,$this->_USERAGENT);
        if($cookie==true){
        curl_setopt($curl,CURLOPT_HEADER,1);
        $result=curl_exec($curl);
        preg_match_all('/\{(.*)\}/', $result, $json);
        if(json_decode($json[0][0],1)["code"]==200){
            preg_match_all('/Set-Cookie: MUSIC_U=(.*?)\;/', $result, $musicu);
            preg_match_all('/Set-Cookie: __csrf=(.*?)\;/', $result, $csrf);
            setcookie("MUSIC_U",$musicu[1][0]);
            setcookie("__csrf",$csrf[1][0]);
        }
        $result = $json[0][0];
        }else{
        $result=curl_exec($curl);}
        curl_close($curl);
        return $result;
    }

    // main function
    // login by phone
    public function login($cell,$pwd){
        $url="https://music.163.com/weapi/login/cellphone";
        $data=array(
        "phone"=>$cell,
        "countrycode"=>"86",
        "password"=>$pwd,
        "rememberLogin"=>"true");
        return $this->curl($url,$this->prepare($data),true);
    }
    // login by email
    public function loginByEmail($cell,$pwd){
        $url="https://music.163.com/weapi/login";
        $data=array(
        "username"=>$cell,
        "password"=>$pwd,
        "rememberLogin"=>"true");
        return $this->curl($url,$this->prepare($data),true);
    }
    // get user detail
    public function detail($uid){
        $url="https://music.163.com/weapi/v1/user/detail/${uid}";
        return $this->curl($url,$this->prepare($data),true);
    }
    public function follow(){
        $url="https://music.163.com/weapi/user/follow/0";
        return '{"code":'.json_decode($this->curl($url,$this->prepare(array('csrf_token'=>$_COOKIE["__csrf"]))),1)["code"].'}';
    }
    public function recommend(){
        $url="https://music.163.com/weapi/v1/discovery/recommend/resource";
        $json = json_decode($this->curl($url,$this->prepare(array('csrf_token'=>$_COOKIE["__csrf"]))), 1);
		foreach ($json["recommend"] as $i => $k) {
			$id[$i] = $k["id"];
		}
		return $id;
        
    }
    public function daka_new(){
        $playlist = $this->recommend();
        $ids = array();
        $count=0;
        for ($i = 0; sizeof($ids) < 310; $i++) {
        	$songid = $this->getsongid($playlist[rand(0,sizeof($playlist)-1)]);
        	for ($k=0;sizeof($ids) < 310&&$k<sizeof($songid);$k++) {
        	
        	$ids[$count]["action"]="play";
        	$ids[$count]["json"]["download"] =0 ;
        	$ids[$count]["json"]["end"] ="playend"; 
     		$ids[$count]["json"]["id"] = $songid[$k]["id"];
     		$ids[$count]["json"]["sourceId"] ="";
     		$ids[$count]["json"]["time"] = 240;
     		$ids[$count]["json"]["type"] ="song";
     		$ids[$count]["json"]["wifi"] =0;
     		$count++;
    	    }
        }
        $data =json_encode($ids);
        $url = "http://music.163.com/weapi/feedback/weblog";
        $this->curl($url,$this->prepare(array("logs"=>$data)));
        return '{"code":200,"count":'.$count.'}';
    }
    public function getsongid($playlist_id){
        $url='https://music.163.com/weapi/v3/playlist/detail?csrf_token=';
        $data=array(
            'id'=>$playlist_id,
            'n'=>1000,
            'csrf_token'=>'',
        );
        $raw=$this->curl($url,$this->prepare($data));
        $json=json_decode($raw,1);
      	
		return $json["playlist"]["trackIds"];
        //return json_decode($raw,1)[];
    }
    
    
    
    
    public function daka(){
        $playlist = $this->recommend();
        $ids = array();
        for ($i = 0; sizeof($ids) < 300; $i++) {
        	$songid = $this->playlist($playlist[rand(0,sizeof($playlist))]);
        	foreach ($songid as $id) {
     		$ids[] = $id;
    	    }
        }
        return '{"code":200,"count":'.$this->scrobble($ids).'}';
    }
    public function sign(){
        $url="https://music.163.com/weapi/point/dailyTask";
        $data=array("type"=>0);
        return $this->curl($url,$this->prepare($data),true);
    }
    public function scrobble($songid){
        $url="http://music.163.com/weapi/feedback/weblog";
        $res = array();
        $count=0;
        $cookies=$this->_COOKIE."__csrf=".$_COOKIE["__csrf"]."; MUSIC_U=".$_COOKIE["MUSIC_U"];
		$mh = curl_multi_init();
		foreach ($songid as $k => $id) {
   	        $data[$k]=$this->prepare(array("logs"=>'[{"action":"play","json":{"download":0,"end":"playend","id":"'.$id.'","sourceId":"","time":240,"type":"song","wifi":0}}]'));
            $data[$k]=http_build_query($data[$k]);
			$conn[$k] = curl_init($k);
			curl_setopt($conn[$k], CURLOPT_URL, $url);
			curl_setopt($conn[$k],CURLOPT_POST,1);
			curl_setopt($conn[$k],CURLOPT_POSTFIELDS,$data[$k]);
			//curl_setopt($conn[$k], CURLOPT_TIMEOUT, $timeout);
			curl_setopt($conn[$k], CURLOPT_COOKIE, $cookies);
			curl_setopt($conn[$k], CURLOPT_RETURNTRANSFER, 1);
			curl_multi_add_handle($mh, $conn[$k]);
			$count++;
		}
		do {
			$mrc = curl_multi_exec($mh, $active);
		}
		while ($mrc == CURLM_CALL_MULTI_PERFORM);
		while ($active and $mrc == CURLM_OK) {
			if (curl_multi_select($mh) != -1) {
				do {
					$mrc = curl_multi_exec($mh, $active);
				}
				while ($mrc == CURLM_CALL_MULTI_PERFORM);
			}
		}
		foreach ($array as $k => $value) {
			curl_error($conn[$k]);
			$res[$k] = curl_multi_getcontent($conn[$k]);
			//$header[$k] = curl_getinfo($conn[$k]);
			curl_close($conn[$k]);
			curl_multi_remove_handle($mh, $conn[$k]);
		}
		curl_multi_close($mh);
		//return array('return' => $res, 'header' => $header);
        //return "233";
        return $count;
    }

    public function playlist($playlist_id){
        $url='https://music.163.com/weapi/v3/playlist/detail?csrf_token=';
        $data=array(
            'id'=>$playlist_id,
            'n'=>1000,
            'csrf_token'=>'',
        );
        $raw=$this->curl($url,$this->prepare($data));
        $json=json_decode($raw,1);
      	foreach ($json["playlist"]["trackIds"] as $i => $k) {
			$ids[$i] = $k["id"];
		}
		return $ids;
        //return json_decode($raw,1)[];
    }
}
$api= new API();
$api->follow();
//test();
if($_REQUEST["do"]=="login"){
    echo $api->login($_REQUEST["uin"],$_REQUEST["pwd"]);}
    elseif($_REQUEST["do"]=="email"){echo $api->loginByEmail($_REQUEST["uin"],$_REQUEST["pwd"]);}
    elseif($_REQUEST["do"]=="sign"){echo $api->sign();}
    elseif($_REQUEST["do"]=="daka"){echo $api->daka_new();}
    elseif($_REQUEST["do"]=="check"){echo $api->follow();}
    elseif($_REQUEST["do"]=="detail"){echo $api->detail($_REQUEST["uid"]);}

?>