rget 4.0.2 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/bin/rget +13 -3
  3. data/lib/freshlive.rb +67 -31
  4. data/rget.gemspec +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f0e0f7e7c5cf7075e2c0e5089d305080b0f093f4
4
- data.tar.gz: d6ed0e66efc6b0f5aaf6e03ed6699bb5ea8f8bc9
3
+ metadata.gz: 1e7390b3e41059e8ecbd831444e486d8a4e53344
4
+ data.tar.gz: f9954fa00090ecef43a12e6a3edb1748b5d6a3e1
5
5
  SHA512:
6
- metadata.gz: 52a0e6c1012a08e408ed63da75067ccad7e9bcc04168da44c1b0eb5480b0c1b20a6cf60fe25ad79745cb76cd89002c0b1c7d844404450cb1a1c62de0e1413fe9
7
- data.tar.gz: f5899e1f52c02b17b6e531fd09c6faf90b1e00d8dc59124eea066aa4238edf2e15a6566c8939a818fb2783e1ae86be74f111425883b62b9af98d88de6daf8469
6
+ metadata.gz: aace3fa444aed6bbc74ff1e2f46cd010bdd6bae3e2749c6cbdd077bbd7dfe84cd511b37da5b291ce4fe8617d3c157717f553bbc62b7d6f25321de6fecada3fd4
7
+ data.tar.gz: ead867d95d7761594d021cf6619cb4c3415843ca43d67c1a8528faae5dc6ac13aa1e2af13d07d1dc654971af8cac5852b4e99260633f6bbc09f05414db35b106
data/bin/rget CHANGED
@@ -29,8 +29,8 @@ class GetWebRadio < Thor
29
29
  exit 1
30
30
  end
31
31
 
32
- config = YAML::load_file(config_file)
33
- config['programs'].each do |command, params|
32
+ config = {'options' => {}, 'programs' => {}}.merge(YAML::load_file(config_file) || {})
33
+ (config['programs'] || {}).each do |command, params|
34
34
  desc command, params['desc']
35
35
  define_method(command) do
36
36
  opts = OpenStruct.new(config['options'].merge(options))
@@ -48,7 +48,17 @@ class GetWebRadio < Thor
48
48
  end
49
49
  end
50
50
 
51
- desc 'yaml', 'dump YAML of specified URL'
51
+ desc 'init', 'show initial config file, save to "rget.yaml" or "~/.rget"'
52
+ def init
53
+ puts <<~CONFIG
54
+ options:
55
+ mp3nize: ffmpeg -i '$1' -vn -acodec copy '$2' || ffmpeg -i '$1' -vn -y -ab 64k '$2'
56
+ path: .
57
+ programs:
58
+ CONFIG
59
+ end
60
+
61
+ desc 'yaml <URL>', 'dump YAML of specified URL'
52
62
  def yaml(url)
53
63
  dump = WebRadio({'url'=>url}, OpenStruct.new({dump: true})).dump
54
64
  puts YAML.dump(dump).lines.drop(1).map{|l| " #{l}"}
data/lib/freshlive.rb CHANGED
@@ -3,52 +3,88 @@ require 'nokogiri'
3
3
  require 'json'
4
4
 
5
5
  class FreshLive < WebRadio
6
- def initialize(url, options)
7
- super
8
- @archive = URI(File.join(@url + '/archive'))
9
- @doc = Nokogiri(open(@archive).read).root
10
- end
11
-
12
6
  def download
13
- offset = 0
14
- begin
15
- meta = program_meta(program_id(offset))
16
- serial = meta['data']['title'].scan(/\d+$/).first.to_i
17
- open("#{@label}##{'%02d' % serial}.ts", 'wb') do |w|
18
- ts_list(meta['data']['archiveStreamUrl']).each do |u|
19
- w.write(open(u, 'rb').read)
7
+ if URI(@url).path =~ %r|/search/|
8
+ archive = @url
9
+ else
10
+ archive = URI(File.join(@url + '/programs/archive'))
11
+ end
12
+
13
+ each_programs(Nokogiri(open(archive).read)) do |meta|
14
+ begin
15
+ serial = meta['data']['title'].scan(/\d+$/).first.to_i
16
+ src = "#{@label}##{'%02d' % serial}.ts"
17
+ open(src, 'wb') do |w|
18
+ print "getting #{src}..."
19
+ ts_list(meta['data']['archiveStreamUrl']).each do |u|
20
+ print '.'
21
+ w.write(open(u, 'rb').read)
22
+ end
20
23
  end
24
+ dst = mp4nize(src)
25
+ puts 'done.'
26
+ move(dst)
27
+ return
28
+ rescue OpenURI::HTTPError
29
+ puts 'try next.'
30
+ next
31
+ rescue
32
+ puts 'fail.'
33
+ $stderr.puts 'faild to convert .ts => .mp4'
34
+ return
21
35
  end
22
- rescue OpenURI::HTTPError
23
- offset += 1
24
- retry
25
36
  end
37
+ puts 'fail.'
38
+ $stderr.puts 'free program not found.'
26
39
  end
27
40
 
28
41
  def dump
29
- tag = Pathname(@url).basename.to_s
30
- meta = JSON.parse(@doc.css('script').first)
31
-
32
- return {
33
- tag => {
34
- 'desc' => meta['name'],
35
- 'url' => @url,
36
- 'label' => tag
42
+ u = URI(@url)
43
+ if u.path =~ %r|/search/|
44
+ desc = URI.decode_www_form_component(Pathname(u.path).basename.to_s)
45
+ return {
46
+ 'freshlive_search' => {
47
+ 'desc' => desc,
48
+ 'url' => @url,
49
+ 'label' => desc
50
+ }
51
+ }
52
+ else
53
+ tag = Pathname(u.path).basename.to_s
54
+ meta = JSON.parse(Nokogiri(open(@url, &:read)).css('script').first)
55
+ return {
56
+ tag => {
57
+ 'desc' => meta['name'],
58
+ 'url' => @url,
59
+ 'label' => tag
60
+ }
37
61
  }
38
- }
62
+ end
39
63
  end
40
64
 
41
65
  private
42
- def program_id(offset = 0)
43
- Pathname(@doc.css('.ProgramTitle a')[offset].attr('href')).basename.to_s
44
- end
45
-
46
- def program_meta(id)
47
- JSON.parse(open("https://freshlive.tv/proxy/Programs;id=#{id}").read)
66
+ def each_programs(html)
67
+ x = "//section[h1[contains(text(),'アーカイブ')]]//*[contains(@class,'ProgramTitle')]/a/@href"
68
+ html.xpath(x).each do |href|
69
+ id = Pathname(href.value).basename.to_s
70
+ yield JSON.parse(open("https://freshlive.tv/proxy/Programs;id=#{id}", &:read))
71
+ end
48
72
  end
49
73
 
50
74
  def ts_list(rate_m3u8)
51
75
  ts_m3u8 = open(rate_m3u8).read.each_line.grep_v(/^#/)[1].chomp
52
76
  open(URI(rate_m3u8) + ts_m3u8).read.each_line.grep_v(/^#/).map{|u|URI(rate_m3u8) + u.chomp}
53
77
  end
78
+
79
+ def mp4nize(src)
80
+ dst = src.sub(/ts$/, 'mp4')
81
+ command = "ffmpeg -i #{src} -vcodec copy -strict -2 #{dst}"
82
+ result = Open3.capture3(command)
83
+ if result[2].to_i == 0
84
+ File.delete(src)
85
+ else
86
+ File.delete(dst) if File.exist?(dst)
87
+ end
88
+ return dst
89
+ end
54
90
  end
data/rget.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "rget"
7
- spec.version = "4.0.2"
7
+ spec.version = "4.1.0"
8
8
  spec.authors = ["TADA Tadashi"]
9
9
  spec.email = ["t@tdtds.jp"]
10
10
  spec.description = %q{Downloading newest radio programs on the web. Supported radio stations are hibiki, onsen, niconico and freshlive.}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rget
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.2
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TADA Tadashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-28 00:00:00.000000000 Z
11
+ date: 2017-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor