Class: VideoTorrentInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/video_torrent_info.rb,
lib/video_torrent_info/version.rb

Constant Summary collapse

DEFAULTS =
{
  port1: 8661,
  port2: 8662,
  temp_path: '/tmp',
  supported_extensions: %w{ .avi .mkv .mpg .mpeg .3gp .wmv .mov .flv .mts .mp4 },
  download_limit: 100000,
  timeout: 30
}
VERSION =
'0.1.26'

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ VideoTorrentInfo

Returns a new instance of VideoTorrentInfo.



13
14
15
16
# File 'lib/video_torrent_info.rb', line 13

def initialize(params = {})
  @params = DEFAULTS.merge(params)
  @torrent_client = VideoTorrentInfo::TorrentClient.new
end

Instance Method Details

#download_video(torrent_path) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/video_torrent_info.rb', line 23

def download_video(torrent_path)
  string = File.open(torrent_path){ |file| file.read }
  torrent = string.bdecode
  files = get_video_files(torrent)
  @torrent_client.load(torrent_path, files.keys.first, @params[:download_limit], @params[:temp_path], @params[:port1], @params[:port2], @params[:timeout])
  @params[:temp_path] + '/' + files.values.first
end

#get_video_files(torrent) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/video_torrent_info.rb', line 30

def get_video_files(torrent)
  info = torrent['info']
  res = {}
  files = []
  if info['files'].nil?
    files = [info['name']]
  else
    files = info['files'].map { |e| ([info['name']] + e['path']).join('/') }
  end
  files.each do |f|
    if @params[:supported_extensions].include?(File.extname(f))
      res[files.index(f)] = f
    end
  end
  raise "No files supported" if res.empty?
  res
end

#load(torrent_path) ⇒ Object



17
18
19
20
21
22
# File 'lib/video_torrent_info.rb', line 17

def load(torrent_path)
  dest = download_video(torrent_path)
  res = ::FFmpeg::Video.info(dest)
  File.unlink(dest)
  res
end