This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
/
svt-recorder
executable file
·106 lines (93 loc) · 2.97 KB
/
svt-recorder
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
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
begin
require 'svt/recorder'
rescue LoadError
$:.unshift File.join(File.dirname(File.expand_path(__FILE__)), '..', 'lib')
require 'rubygems'
require 'svt/recorder'
end
require 'tempfile'
require 'readline'
require 'ostruct'
require 'progress_bar'
if ARGV.empty? or ARGV[0].match(/\A(-h)|((--)?help)\Z/i)
puts "Usage: #{$0.split(/\//)[-1]} <SVT Play URL> [name of output file]"
puts "\tAll parts of the video at SVT Play URL will be recorded."
puts "\tIf you've supplied an output name that will be used, otherwise you'll be asked."
puts ''
puts "Library version: #{SVT::Recorder::VERSION}"
exit 0
elsif not ARGV[0].match(/(svtplay|svt\.se)/)
puts "No valid SVT Play or Play Rapport URL supplied"
exit 1
end
# Wohoo, don't need wget anymore!
class HTTPDownload
def initialize(downloader, output)
@base_url = URI.parse(downloader.base_url)
@downloader = downloader
@headers = {'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10'}
@file = output
@server = Net::HTTP.start(@base_url.host, @base_url.port)
if block_given?
begin
yield self
ensure
close()
end
end
end
# Will yield the number of megabytes downloaded every megabyte
def fetch_all
downloaded = 0
megabyte = 1024 * 1024
mb_down = 0
File.open(@file, 'wb+') do |file|
@downloader.parts do |part|
begin
@server.request_get(part, @headers) do |res|
res.read_body do |body|
file.write body
end
end # /@server
rescue Timeout::Error, EOFError, Errno::ECONNRESET => exception
yield -1
@server = Net::HTTP.start(@base_url.host, @base_url.port)
STDERR.puts "Connection error..."
retry
end
yield part
end # /parts
end # /File
end
def close ; @server.finish ; end
alias :disconnect :close
end
# Ensure that all output is written to console when it is asked to,
# no buffer for you!
STDOUT.sync = true
downloader = SVT::Recorder.record(ARGV[0])
output_file = if ARGV[1]
ARGV[1].strip
else
puts 'To edit the filename press up arrow'
Readline::HISTORY.push downloader.title
name = Readline::readline("Filename: [#{downloader.title}] ")
if not name.strip.empty?
name.strip
else
downloader.title
end
end + '.mp4'
if File.exists? output_file
STDERR.puts "\nThe file '#{output_file}' already exists!"
overwrite = Readline::readline('Overwrite? Y/N: ')
exit 1 unless overwrite.match(/Y/i)
end
progress = ProgressBar.new(downloader.parts?, :bar, :elapsed, :eta, :counter)
puts "Starting recording..."
HTTPDownload.new(downloader, output_file) do |down|
down.fetch_all {|part| progress.increment! }
puts "\nFinished recording: #{output_file}"
end