• 技术文章 >php教程 >PHP源码

    磁力链接打包下载

    PHP中文网PHP中文网2016-05-23 16:38:42原创11508
    keyWord=$keyWord;
    		$this->saveFile=$saveFile;
    		$this->minSize=$minSize;
    		$this->page=$pageRange[0];
    		$this->maxPage=$pageRange[1];
    	}
    	
    	public function run(){
    	    
    		while($this->flag && $this->maxPage>$this->page){
    		    $this->timestamp=$this->microtimeFloat();
    			$this->_execute();
    			$this->page++;
    			$this->_notice();
    			
    		}
    	}
    	/**
    	 * 执行
    	 */
    	protected function _execute(){
    	    
    		$this->_getUrl();
    		$this->_getHtmlPage();
    		$this->_parseHtml();
    		if($this->_currentMagnetCount){//当有磁力链接时保存
    			$this->_save();
    		}
    		
    	}
    	
    	public function microtimeFloat(){
            list($usec, $sec) = explode(" ", microtime());
            return sprintf('%01.2f',(float)$usec + (float)$sec);
        }
        /**
         * 通知
         */
    	protected function _notice(){
    	    
    	    $currentTime = $this->microtimeFloat();
    	    $useTime =sprintf('%01.2f', $currentTime-$this->timestamp);
    	    $this->timestamp = $currentTime;
    	    echo '第'.$this->page.'页存储了'.$this->_currentMagnetCount.'个磁力链接,耗时'.$useTime."秒\r\n";
    		if(!($this->flag && $this->maxPage>$this->page)){
    			echo '总共存储了'.$this->_magnetCount.'个磁力链接'."\r\n";
    		}
    	}
        /**
         * 设置url
         */
    	protected function _getUrl(){
    	    
    		$this->_url = $this->host.'search?q='.rawurlencode($this->keyWord).'&p='.$this->page;	
    	}
    	/**
    	 * 获得html文件内容
    	 */
    	protected function _getHtmlPage(){
    	    
    		$ch = curl_init();
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_TIMEOUT,$this->timeout);
            curl_setopt($ch, CURLOPT_URL, $this->_url);
            curl_setopt($ch, CURLOPT_SSLVERSION,3); 
            $result = curl_exec($ch);
            if($result === false){
                //echo 'Curl error: '. curl_errno($ch) .' : '. curl_error($ch);
                $opts = array(
                    'https'=>array(
                        'method'=>"GET",
                        'timeout'=>$this->timeout,//单位秒
                    )
                );
                $context = stream_context_create($opts);
                $this->_html =  @file_get_contents($this->_url,false,$context);
            }else{
                $this->_html = $result;
            }
            curl_close($ch);
    	}
        /**
         * 抓取磁力链接
         */
    	protected function _parseHtml(){
    	    
    		preg_match_all('/href="(magnet\:\?xt=urn\:btih\:.+?)"/',$this->_html,$magnet);
    		preg_match_all('/attr_val">(\d+?\.?\d*? [G|M|K]B)/',$this->_html,$size);
    		if(empty($size[1])){
    			$this->flag=false;//当获取磁力链接时不到时,停止翻页
    		}
    		foreach($size[1] as $key=>$val){
    			if($this->_compareSize($val)){
    				$this->_magnet[]=$magnet[1][$key];
    			}		
    		}
    		$this->_currentMagnetCount = count($this->_magnet);
    		$this->_magnetCount += count($this->_magnet);
    	}
        /**
         * 比较磁力链接文件的大小
         */
    	protected function _compareSize($size){
    	    
    		$number = substr($size,0,strpos($size,' '));
    		$unit = substr($size,strpos($size,' ')+6);
    
    		$n='';
    		switch($unit){
    			case 'GB':
    					$n=1;
    					break;
    			case 'MB':
    					$n=1024;
    					break;
    			case 'KB':
    					$n=1048576;
    					break;
    			default :
    					$n=false;
    		}
    		if($n===false){
    		    try {
    		        throw new Exception("文件大小单位识别失败\n");
    		    } catch (Exception $e) {
    		        echo $e->getMessage();
    		        return false;
    		    }
    			
    		}
    		return ( $this->minSize < ($number/$n) );
    	}
        /**
         * 把磁力链接保存到文件中
         */
    	protected function _save(){
    	    
    	    $data = '';
    		foreach($this->_magnet as $val){
    		    $data .= $val."\n";
    		    $this->magnetNum++;
    		    if(0==$this->magnetNum%15)
    		      $data.="\n\n";
    		}
    		file_put_contents($this->saveFile,$data,FILE_APPEND);
    		$this->_magnet=array();
    	}
    	
    }
    //示例
    $s = new Magnet('LOL','magnet.log',2,array(0,10));
    $s->run();

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:php多维数组转一维数组 下一篇:微信公众平台开发接口PHP SDK完整版
    Web大前端开发直播班

    相关文章推荐

    • 检测移动设备的php代码(手机访问)• php 增加数据保存到mysql数据库实例教程• php入门教程-留言板程序• php文件上传代码[带缩略图与水印]• 剖析PHP中的输出缓冲 flush之类

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网