class MyFile < File
  def peek(len)
    str = self.read(len)
    self.pos -= len
    str
  end  
end

#字段信息
class TVal
  attr_accessor :Type
  attr_accessor :Value
  
  def to_s()
    @Value
  end
  
end

#文件信息
class TFile
  attr_reader :Length
  attr_reader :Path
  attr_reader :Name
  attr_reader :FirstPiece
  attr_reader :PieceLength
  
  def initialize(size, fullPath, pStart, pLen)
    @Length = Size
    @FirstPiece = pStart
    @PieceLength = pLen
    fileNamePos = fullPath.rindex('\\')
    if fileNamePos.nil? then
      @Path = ""
      @Name = fullPath
    else
      @Path = fullPath[0..fileNamePos]
      @Name = fullPath[fileNamePos..fullPath.length-1]
    end
  end
end
  

class TorrentParser
  attr_accessor :Root
  def initialize(tPath)
    
    @Path = tPath
    @Size = nil
    
    @Root = nil
    @Files = nil
    
    @Torrent = nil
    @TotalValues = 0
    
    @InfoStart = nil
    @InfoEnd = nil
    
  end
  #主函数
  def Process()
    raise "File doesn't exist"  if !MyFile.exist? @Path #检测文件路径
    @TotalValues = 0
    
    @Torrent = MyFile.new @Path
    @Torrent.binmode

    raise "Torrent File Error" if @Torrent.read(1) != 'd'
    
    @Root = ProcessDict()
    @Files = GetFiles()
    
  end
  #读取一个字典
  def ProcessDict()
    tempDict = Hash.new

    while @Torrent.peek(1) != 'e' #向后读一个字符 看看是不是e 如果是e的话 说明dictionary结束了
      key = ProcessString() #读取一个Key Name

      if key == 'info' then #如果是名字为info的key
        @InfoStart = @Torrent.pos #设置info start的index
      end
      if key == 'pieces' then #如果是pieces的key 则按照特殊的byte方式读取
        val = TVal.new
        val.Type = "BYTE"
        val.Value = ProcessByte()
        tempDict[key] = val
      else
        val = ProcessVal() #否则读该key的Val
        tempDict[key] = val
      end
      if key == "info" then
        @InfoEnd = @Torrent.pos - @InfoStart
      end
    end
    @Torrent.read 1 #读掉结尾的"e"
    
    return tempDict
  end
  
  

  #读取一个字串
  def ProcessString()
    tempLen = ''
    tempLen += @Torrent.read(1) while @Torrent.peek(1) != ':' #获取字串长度
    
    @Torrent.read(1) # 将后面的冒号读掉    

    content = @Torrent.read(tempLen.to_i) #然后读指定长度的字符串 并返回 

    return content
  end
    

  #读取字节串
  def ProcessByte()
    tempLen = ''
    tempLen += @Torrent.read(1) while @Torrent.peek(1) != ':' #获取字串长度
    
    @Torrent.read(1) # 将后面的冒号读掉    
    
    chrs = Array.new
    (1..tempLen.to_i).each{|idx|
      byte = @Torrent.readchar
      chrs << "%02X" % byte
    }
    chrs
  end
  

  #读一个数字
  def ProcessInt()
    tempInt = ''
    tempInt += @Torrent.read(1) while @Torrent.peek(1) != 'e'

    @Torrent.read(1) #把最后的e读掉
    tempInt.to_i
  end
  #读一个列表
  def ProcessList()
    tempList = Array.new
    while @Torrent.peek(1) != 'e'
      tempList << ProcessVal
    end
    tempList
  end
    
  #读取一个Val
  def ProcessVal()
    @TotalValues += 1
    
    val = TVal.new
    
    
    case @Torrent.peek(1)
      when 'd'
        @Torrent.read(1)  #把d读掉
        val.Type = "DICT"
        val.Value = ProcessDict()
      when 'l'
        @Torrent.read(1) #把l读掉
        val.Type = "LIST" 
        val.Value = ProcessList()
      when 'i'
        @Torrent.read(1) #把i读掉
        val.Type = "INT"
        val.Value = ProcessInt()
      else
        val.Type = "STRING"
        val.Value = ProcessString()
    end
    val
  end
  
end

begin
  _torrent = TorrentParser.new ARGV.shift
  _torrent.Process
  
  _torrent.Root.map{|key,value|
    case value.Type
      when 'LIST'
        puts "#{key}(#{value.Type})\t\t#{value.Value}"
      when 'BYTE'
        puts "#{key}(#{value.Type})\t\t%0X", value.Value
      when 'DICT'
        value.Value.map{|skey,svalue|
          puts "--#{skey}(#{svalue.Type})\t\t#{svalue.Value}"
        }
      else
        puts "#{key}(#{value.Type})\t\t#{value.Value}"
    end
  }
  
rescue
  puts $!
  puts $@
end

转载于:https://www.cnblogs.com/coderlee/archive/2008/04/08/1142788.html

ruby写的BT种子解析器相关推荐

  1. B编码与BT种子文件分析,以及模仿json-cpp写一个B编码解析器

    B编码与BT种子文件分析,以及模仿json-cpp写一个B编码解析器 1.什么是B编码 2.B编码格式 3.种子文件结构 3.1.主文件结构 3.2.info结构 4.简单的例子了解一下种子文件和B编 ...

  2. bt种子播放器WebTorrent for Mac使用方法

    WebTorrent for Mac这是一款Mac平台上非常好用的bt种子在线播放器,支持种子和边播放边下载.WebTorrent Mac版不仅可以用来BT下载,还可以边下边播,使用简单,拖动种子文件 ...

  3. 自己写的简单xml解析器

    已经自我放逐好几年了.打算去上班得了.在最后的自由日子里,做点有意义的事吧... 先来下载地址 http://www.kuaipan.cn/file/id_12470514853353274.htm ...

  4. python 正则表达式语法大全_Python 之父撰文回忆:为什么要创造 pgen 解析器?

    花下猫语: 近日,Python 之父在 Medium 上开通了博客,并发布了一篇关于 PEG 解析器的文章(参见我翻的 全文译文).据我所知,他有自己的博客,为什么还会跑去 Medium 上写文呢?好 ...

  5. 【LLM】Langchain使用[一](模型、提示和解析器、存储)

    note 文章目录 note 零.LangChain介绍 一.模型.提示和解析器 1. 提示 2. 使用langchain写提示 3. 输出解析器 4. 链式思考推理(ReAct) 二.存储 1. 对 ...

  6. Go语言写的解析器(支持json,linq,sql,net,http等)

    Monkey程序语言 Monkey v2.0版本已发布. monkey v2.0 增加了如下内容: 新增 short arrow(->)支持(类似C#的lambda表达式) 增加 列表推导和哈希 ...

  7. # 解析bt文件_磁力链接和BT种子使用方法

    目前用的最多的是磁力链接和BT种子,不过好多人并不太会使用,因此写个教程给大家说明一下. 何为磁力链接:简单地说,磁力链接是一种特殊链接,但是它与传统基于文件的位置或名称的普通链接(如http://x ...

  8. python 接口测试 如何写配置文件_python接口自动化测试 - configparser配置文件解析器详细使用...

    configparser简介 ConfigParser模块已在Python 3中重命名为configparser 该模块定义了ConfigParser类. ConfigParser类实现一种基本的配置 ...

  9. string 转 json_手写Json解析器学习心得

    哦?从"{"开始,看来是个对象了! 一. 介绍 一周前,老同学阿立给我转了一篇知乎回答,答主说检验一门语言是否掌握的标准是实现一个Json解析器,网易游戏过去的Python入门培训 ...

  10. 手写token解析器、语法解析器、LLVM IR生成器(GO语言)

    最近开始尝试用go写点东西,正好在看LLVM的资料,就写了点相关的内容 - 前端解析器+中间代码生成(本地代码的汇编.执行则靠LLVM工具链完成) https://github.com/daibinh ...

最新文章

  1. 股市币市:数据分析与交易所最新公告(20190302)
  2. zabbix系列~ 监控模式
  3. 宏基因组报名倒计时!报名线上课还可免费参加线下课
  4. ubuntu安装使用不同版本的gcc
  5. FW : 一只小青蛙的一生(图片连载)
  6. Maven---学习心得---maven的Dependency Mechanism(依赖关系机制)
  7. 图像局部显著性—点特征(Fast)
  8. mysql 全局不重复_php uniqid() 通过MYSQL实现全局不重复的唯一ID
  9. C语言课后习题(60)
  10. 微信电脑版重大更新,可以上班刷朋友圈摸鱼了
  11. SpringBoot实现抽奖大转盘
  12. 如何做好一场技术分享,100%纯实用技巧输出
  13. java碰撞检测代码_java 实现精确碰撞检测。
  14. 保镖机器人作文_暴力“保镖”作文800字
  15. 大中型 UGC 平台的反垃圾(anti-spam)工作
  16. 零基础微信小程序开发学习笔记(2.构建静态界面)
  17. 不怕加班狗有情绪,就怕加班狗有“武器”
  18. 深度学习模型轻量化(上)
  19. 2023年长沙Java培训机构排名前十新鲜出炉!有你心仪的机构吗?
  20. 昨夜西风凋碧树,独上高楼,望尽天涯路

热门文章

  1. Linux for Matlab中文注释乱码(亲测有效)
  2. sklearn 自定义函数转化器FunctionTransformer使用
  3. python基于scipy拟合构建所需统计分析模型,可视化分析展示
  4. 用PBD制作餐饮店KPI分析仪-入门篇
  5. 数据库事务 写偏斜write-skew
  6. 数据库学习笔记5-隔离级别 Repeatable Read
  7. Mac如何删除python Python cannot be opened because of a problem
  8. 基于SSM的教学质量系统
  9. 常见linux服务器存储空间,全面了解linux服务器的常用命令总结
  10. 华为 台积电 高通申请_华为表态愿意合作,台积电送来“神助攻”,高通:我太难了...