磁力链接打包下载

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
<?php
/**
 * 磁力链接下载,根据 关键词 文件大小 保存到指定文件中
 */
class Magnet{
     
    public $host='https://btdigg.org/';//域名
    public $keyWord='';//关键词
    public $minSize=0;//文件大小最小值
    public $saveFile='';//存储文件
     
    public $flag=true;//是否继续翻页
    public $page=0;//当前页面
    protected $_url='';//当前的url
    protected $_html='';//当前的url的html文件内容
    protected $_magnet=array();//保存磁力链接的数组
    protected $_currentMagnetCount=0;//当前磁力链接的数量
    protected $_magnetCount=0;//磁力链接的总数
    public $timeout=10;//页面超时设置
    public $maxPage=1000;
    public $magnetNum=0;
    public $timestamp=0;
     
    public function __construct($keyWord,$saveFile='./magnet.log',$minSize=0,$pageRange=array(0,1000)){
        $this->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*?&nbsp;[G|M|K]B)<\/span>/',$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,'&nbsp;'));
        $unit = substr($size,strpos($size,'&nbsp;')+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();

编程技巧