JOCHLSDownloader 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.md +24 -0
  2. data/lib/JOCHLSDownloader.rb +138 -0
  3. metadata +48 -0
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ JOCHLSDownloader
2
+ ================
3
+
4
+ The JOCHLSDownloader is a very simple native ruby code that downloads all files linked by a m3u8 manifest
5
+
6
+ Usage examples:
7
+
8
+ require 'JOCHLSDownloader.rb'
9
+
10
+ #URL from HLS apple example
11
+ url = "https://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8"
12
+ downloadpath = "./donloadtestfiles"
13
+ logfilename = "jochlsdownloader.log"
14
+
15
+ begin
16
+ hlsdownloader = CJOCHLSDownloader.new(url, downloadpath, logfilename, Logger::DEBUG)
17
+
18
+ hlsdownloader.startdownload
19
+
20
+ puts "End!"
21
+
22
+ rescue Exception => e
23
+ puts "Error: #{e.message}, Trace: #{e.backtrace.inspect}"
24
+ end
@@ -0,0 +1,138 @@
1
+ #file: JOCHLSDownloader.rb
2
+
3
+ # CJOCHLSDownloader -- Downloads all HLS data from a URL
4
+ # Copyright (c) 2014 Jordi Cenzano (www.jordicenzano.name)
5
+
6
+
7
+ #Checked with ruby 1.9.3 perhaps it works with lower versions
8
+ if RUBY_VERSION < "1.9.3"
9
+ fail "JOCHLSDownloader requires ruby >= 1.9.3."
10
+ end
11
+
12
+ require 'logger'
13
+ require 'net/http'
14
+ require 'uri'
15
+
16
+ #CJOCHLSDownloader class downloads all HLS data from a URL
17
+ class CJOCHLSDownloader
18
+
19
+ #Initialize CJOCHLSDownloader
20
+ # @param url [String] the HLS source string
21
+ # @param path [String] base path of all downloaded content
22
+ # @param logfile [String] log file to save the logs (optional)
23
+ # @param loglevel log level, could be Logger::FATAL, Logger::ERROR, Logger::WARN, Logger::INFO, Logger::DEBUG (optional)
24
+ def initialize(url, path, logfile = nil, loglevel = Logger::INFO)
25
+
26
+ @sourceurl = URI(url)
27
+ @destpath = path
28
+
29
+ #Start logger
30
+ # Keep data for today and the past 3 days.
31
+ @logger = nil
32
+ if logfile != nil
33
+ @logger = Logger.new(logfile)
34
+ @logger.level = loglevel
35
+ end
36
+ end
37
+
38
+ #Start to donwload the HLS content
39
+ def startdownload ()
40
+
41
+ Log Logger::DEBUG, "Start download of #{@sourceurl} to #{@destpath}"
42
+
43
+ #Get domain
44
+ domain = @sourceurl.host
45
+ url = @sourceurl.path
46
+
47
+ Log Logger::DEBUG, "URI host: #{domain}, path: #{url}"
48
+
49
+ downloadHLS domain, url, @destpath
50
+
51
+ end
52
+
53
+ private
54
+
55
+ def downloadHLS (domain, url, basedir)
56
+ Log Logger::DEBUG, "downloadHLS domain: #{domain}, URL: #{url}, basedir: #{basedir}"
57
+
58
+ if Dir.exist?(basedir) == false
59
+ Dir.mkdir(basedir)
60
+ Log Logger::INFO, "Created dir: #{basedir}"
61
+ end
62
+
63
+ #Extract from url
64
+ destpathfile = File.join basedir , File.basename(url)
65
+
66
+ Log Logger::DEBUG, "Start downloading domain: #{domain}, URL: #{url}, To: #{destpathfile}"
67
+
68
+ Net::HTTP.start(domain) do |http|
69
+ resp = http.get(url)
70
+ open(destpathfile, "wb") do |file|
71
+ file.write(resp.body)
72
+ end
73
+ end
74
+ Log Logger::DEBUG, "End downloading domain: #{domain}, URL: #{url}, To: #{destpathfile}"
75
+
76
+ #Process downloaded file if it is m3u8
77
+ ext = File.extname (destpathfile)
78
+ if ext.casecmp(".m3u8") == 0
79
+ Log Logger::DEBUG, "Start parsing: #{destpathfile}"
80
+ parsem3u8 destpathfile,domain, url, basedir
81
+ Log Logger::DEBUG, "End parsing: #{destpathfile}"
82
+ end
83
+ end
84
+
85
+ def parsem3u8 (file, domain, url, basedir)
86
+
87
+ File.open( file ).each do |line|
88
+
89
+ #Discard spaces
90
+ linetrim = line.strip!
91
+ if linetrim == nil
92
+ linetrim = line
93
+ end
94
+
95
+ #Discard if lines starts with #
96
+ if ((linetrim != "") && (line.chars.first != "#"))
97
+ #Process
98
+ Log Logger::DEBUG, line
99
+ urlwithoutfile = File.dirname (url)
100
+ newurl = File.join urlwithoutfile, line
101
+ newpath = File.dirname line
102
+ if (newpath != ".")
103
+ newbasedir = File.join basedir, newpath
104
+ else
105
+ newbasedir = basedir
106
+ end
107
+
108
+ downloadHLS domain, newurl, newbasedir
109
+ end
110
+ end
111
+ end
112
+
113
+ def Log (type, message)
114
+
115
+ #To test
116
+ puts type.to_s + ": " + message
117
+
118
+ if @logger != nil
119
+ if type == Logger::FATAL
120
+ @logger.fatal(message)
121
+ end
122
+ if type == Logger::ERROR
123
+ @logger.error(message)
124
+ end
125
+ if type == Logger::WARN
126
+ @logger.warn(message)
127
+ end
128
+ if type == Logger::INFO
129
+ @logger.info(message)
130
+ end
131
+ if type == Logger::DEBUG
132
+ @logger.debug(message)
133
+ end
134
+ end
135
+
136
+ end
137
+
138
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: JOCHLSDownloader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jordi Cenzano
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-07-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: It is very simple code that downloads in a recursive way all contens
15
+ linked in a m3u8 file
16
+ email: jordicenzano@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/JOCHLSDownloader.rb
22
+ - README.md
23
+ homepage: http://rubygems.org/gems/JOCHLSDownloader
24
+ licenses:
25
+ - MIT
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.23
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: It donwloads all contents linked to m3u8 file
48
+ test_files: []