2019-07-26 15:09:20 2174次浏览 1条回答 0 悬赏 20 金钱

已知 磁力链接(格式:magnet:?xt=urn:btih:-----),怎么转换为 种子文件(.torrent
网上找了一段代码,不过是解析种子文件信息:

<?php

class Lightbenc
{

    public static function bdecode($s, &$pos = 0)
    {
        if ($pos >= strlen($s)) {
            return null;
        }
        switch ($s[$pos]) {
            case 'd':
                $pos++;
                $retval = array();
                while ($s[$pos] !== 'e') {
                    $key = Lightbenc::bdecode($s, $pos);
                    $val = Lightbenc::bdecode($s, $pos);
                    if ($key === null || $val === null) {
                        break;
                    }
                    $retval[$key] = $val;
                }
                $retval['isDct'] = true;
                $pos++;
                return $retval;
            case 'l':
                $pos++;
                $retval = array();
                while ($s[$pos] !== 'e') {
                    $val = Lightbenc::bdecode($s, $pos);
                    if ($val === null) {
                        break;
                    }
                    $retval[] = $val;
                }
                $pos++;
                return $retval;
            case 'i':
                $pos++;
                $digits = strpos($s, 'e', $pos) - $pos;
                $val    = (int)substr($s, $pos, $digits);
                $pos    += $digits + 1;
                return $val;
            default:
                $digits = strpos($s, ':', $pos) - $pos;
                if ($digits < 0 || $digits > 20) {
                    return null;
                }
                $len = (int)substr($s, $pos, $digits);
                $pos += $digits + 1;
                $str = substr($s, $pos, $len);
                $pos += $len;
                return (string)$str;
        }
    }

    public static function bencode(&$d)
    {
        if (is_array($d)) {
            $ret = 'l';
            if (isset($d['isDct']) && $d['isDct']) {
                $isDict = 1;
                $ret    = 'd';
                ksort($d, SORT_STRING);
            }
            foreach ($d as $key => $value) {
                if (isset($isDict) && $isDict) {
                    if ($key === 'isDct' and is_bool($value)) continue;
                    $ret .= strlen($key) . ':' . $key;
                }
                if (is_string($value)) {
                    $ret .= strlen($value) . ':' . $value;
                } elseif (is_int($value)) {
                    $ret .= 'i${value}e';
                } else {
                    $ret .= Lightbenc::bencode($value);
                }
            }
            return $ret . 'e';
        } elseif (is_string($d)) return strlen($d) . ':' . $d;
        elseif (is_int($d)) return 'i${d}e';
        else return null;
    }

    public static function bdecode_getinfo($filename)
    {
        $t              = Lightbenc::bdecode(file_get_contents($filename, FILE_BINARY));
        $t['info_hash'] = sha1(Lightbenc::bencode($t['info']));
        if (is_array($t['info']['files'])) { //multifile
            $t['info']['size']      = 0;
            $t['info']['filecount'] = 0;
            foreach ($t['info']['files'] as $file) {
                $t['info']['filecount']++;
                $t['info']['size'] += $file['length'];
            }
        } else {
            $t['info']['size']               = $t['info']['length'];
            $t['info']['filecount']          = 1;
            $t['info']['files'][0]['path']   = $t['info']['name'];
            $t['info']['files'][0]['length'] = $t['info']['length'];
        }
        return $t;
    }
}

<?php
require './Lightbenc.php';

$Lightbenc=new Lightbenc();

$btfile = $Lightbenc->bdecode_getinfo('r1.torrent');

//获取种子文件信息
echo '<strong>种子文件名</strong>:'.$btfile['info']['name'].'<br />';

echo '<strong>种子文件数</strong>:'.$btfile['info']['filecount'].'<br />';

echo '<strong>种子大小</strong>:'.$btfile['info']['size'].'<br />';

echo '<strong>磁力链接</strong>:'.'magnet:?xt=urn:btih:'.$btfile['info_hash'].'<br />';

echo '<strong>下载种子</strong>:'.'http://bt.box.n0808.com/'.substr($btfile['info_hash'],0,2).'/'.substr($btfile['info_hash'],38).'/'
    .$btfile['info_hash'].'.torrent'.'<br />';

echo '<strong>备用下载</strong>:'.'http://torcache.net/torrent/'.$btfile['info_hash'].'.torrent'.'<br />';

echo '<strong>备用下载</strong>:'.'http://btcache.sobt.org/'.$btfile['info_hash'].'.torrent'.'<br />';

echo '<strong>备用下载</strong>:'.'https://zoink.it/torrent/'.$btfile['info_hash'].'.torrent'.'<br />';

echo '<br /><strong>文件信息</strong>:<br />';

foreach ($btfile['info']['files'] as $f) {
    echo '—文件总大小:' . $f['length'] . ' byte<br />';
    echo '—文件列表:<br />';

    foreach ($f['path'] as $path) {
        echo '——' . $path . '<br />';
    }
    echo '<br />';
}
您需要登录后才可以回答。登录 | 立即注册
小叮当的肚兜
副董事长

小叮当的肚兜

注册时间:2016-10-31
最后登录:12小时前
在线时长:97小时45分
  • 粉丝13
  • 金钱44030
  • 威望270
  • 积分47700

热门问题