海运的博客

pt torrent种子自动辅种相似度计算

发布时间:March 5, 2021 // 分类:PT // No Comments

torrent解析类使用https://github.com/Rhilip/Bencode,打补丁支持获取种子hash信息。

--- Bencode.php.1       2021-02-24 07:30:41.677067125 +0800
+++ Bencode.php 2021-03-05 17:52:23.803081067 +0800
@@ -26,6 +26,9 @@
  */
 class Bencode
 {
+    public function info_hash($data, $info_hash) {
+      return sha1(substr($data, $info_hash['pos'], $info_hash['length']));
+    }
     /**
      * Decode bencoded data from string
      *
@@ -46,7 +49,15 @@
             $return = [];
             while ($data[$pos] !== 'e') {
                 $key = self::decode($data, $pos);
+                if (!strcmp($key, "info")) {
+                //if ($key === "info") {
+                  $return['info_hash']['pos'] = $pos;
+                }
                 $value = self::decode($data, $pos);
+                if (!strcmp($key, "info")) {
+                //if ($key === "info") {
+                  $return['info_hash']['length'] = ($pos - $return['info_hash']['pos']);
+                }
                 if ($key === null || $value === null) {
                     break;
                 }
@@ -125,6 +136,9 @@
             $return = '';
             $check = -1;
             $list = true;
+            if (isset($data['info_hash'])) {
+              unset($data['info_hash']);
+            }
             foreach ($data as $key => $value) {
                 if ($key !== ++$check) {
                     $list = false;

解析种子torrent所包含的文件名称信息,将名称和文件大小组合排序并计算sha,sha相同则2个种子内容一样可自动辅种。

<?php
require_once './Bencode.php';
require_once './ParseErrorException.php';
$bencode = new Bencode(); 
$str = file_get_contents($filename);
$s = $bencode->decode($str);

if (isset($s['info']["files"])) {
  foreach ($s['info']["files"] as $v) {
    $length = $v["length"];
    $file = end($v["path"]);
    $save[] = $length.$file;
  }
} else {
  $length = $s['info']["length"];
  $file = $s['info']["name"];
  $save[] = $length.$file;
}
if (isset($s["encoding"])) {
  $coding = $s["encoding"];
} else {
  $coding = mb_detect_encoding(implode('', $save), ['UTF-8', 'GB18030', 'GBK', 'GB2312', 'ISO-8859-1']);
  echo 'detect encoding: '.$coding.PHP_EOL;
}
if (!$coding) {
  $coding = 'UTF-8';
}
if ($coding != "UTF-8") {
  foreach ($save as $k => $v) {
    $save[$k] = mb_convert_encoding($v, "UTF-8", $coding);
  }
}
sort($save);
$sha1 = sha1(implode('', $save));

标签:pt, torrent

评论已关闭

分类
最新文章
最近回复
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...
  • jiangker: good, very helpful to me
  • fengfeng: N1 armbian 能有编译下内核吗。。我要开启can 不懂怎么操作