ripdiru 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ log/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ripdiru.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 harupong
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Ripdiru
2
+
3
+ ruby gem to rip Radiru\*Radiru, NHK netradio
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ripdiru'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ripdiru
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## TODO
24
+
25
+ - add metadata w/ radiru API
26
+ - らじる★らじるの番組表API - 別館 子子子子子子(ねこのここねこ) http://d.hatena.ne.jp/riocampos+tech/20130405/1365145830
27
+
28
+ ## Special thanks
29
+
30
+ [matchy2 (MACHIDA Hideki)](https://github.com/matchy2)
31
+
32
+ https://gist.github.com/5310409.git
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/ripdiru ADDED
@@ -0,0 +1,154 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'net/https'
4
+ require 'nokogiri'
5
+ require 'uri'
6
+ require 'pathname'
7
+ require 'base64'
8
+ require 'open-uri'
9
+ require 'date'
10
+ require 'fileutils'
11
+
12
+ class DownloadTask
13
+
14
+ PLAYER_URL = "http://www3.nhk.or.jp/netradio/files/swf/rtmpe.swf"
15
+ TMPDIR = ENV['TMPDIR'] || '/tmp'
16
+
17
+ attr_accessor :station, :cache, :buffer, :outdir, :bitrate
18
+
19
+ def initialize(station = nil, duration = 1800, *args)
20
+ unless station
21
+ abort "Usage: ripdiru [station-id]"
22
+ end
23
+ @station = station
24
+ @channel = channel
25
+ @duration = duration
26
+ @cache = CacheDir.new(TMPDIR)
27
+ @buffer = ENV['RIPDIRU_BUFFER'] || 60
28
+ @outdir = ENV['RIPDIRU_OUTDIR'] || "#{ENV['HOME']}/Music/Radiko"
29
+ @bitrate = ENV['RIPDIRU_BITRATE'] || '48k'
30
+ end
31
+
32
+ def channel
33
+ case station
34
+ when "NHK1"
35
+ @rtmp="rtmpe://netradio-r1-flash.nhk.jp"
36
+ @playpath="NetRadio_R1_flash@63346"
37
+ @xmlpath="http://cgi4.nhk.or.jp/hensei/api/sche-nr.cgi?tz=all&ch=netr1"
38
+ when "NHK2"
39
+ @rtmp="rtmpe://netradio-r2-flash.nhk.jp"
40
+ @playpath="NetRadio_R2_flash@63342"
41
+ @xmlpath="http://cgi4.nhk.or.jp/hensei/api/sche-nr.cgi?tz=all&ch=netr2"
42
+ when "FM"
43
+ @rtmp="rtmpe://netradio-fm-flash.nhk.jp"
44
+ @playpath="NetRadio_FM_flash@63343"
45
+ @xmlpath="http://cgi4.nhk.or.jp/hensei/api/sche-nr.cgi?tz=all&ch=netfm"
46
+ else
47
+ puts "invalid channel"
48
+ end
49
+ end
50
+
51
+ def val(node, xpath)
52
+ node.xpath(".//#{xpath}").text
53
+ end
54
+
55
+ def parse_time(str)
56
+ DateTime.strptime("#{str}+0900", "%Y-%m-%d %H:%M:%S%Z").to_time
57
+ end
58
+
59
+ def now_playing(station)
60
+ today = Date.today
61
+ now = Time.now
62
+ doc = Nokogiri::XML(open(@xmlpath))
63
+
64
+ node = doc.xpath("//program").first
65
+ node.xpath(".//item").each do |item|
66
+ from, to = parse_time(val(item, "starttime")), parse_time(val(item, "endtime"))
67
+ start_time = now.to_i + buffer
68
+ if from.to_i <= start_time && start_time < to.to_i
69
+ return Program.new(
70
+ id: now.strftime("%Y%m%d%H%M%S") + "-#{station}",
71
+ station: station,
72
+ title: val(item, "title"),
73
+ from: from,
74
+ to: to,
75
+ duration: to.to_i - from.to_i,
76
+ info: val(item, "link"),
77
+ )
78
+ end
79
+ end
80
+ end
81
+
82
+ def run
83
+ program = now_playing(station)
84
+
85
+ duration = program.recording_duration + buffer
86
+
87
+ tempfile = "#{TMPDIR}/#{program.id}.mp3"
88
+ puts "Streaming #{program.title} ~ #{program.to.strftime("%H:%M")} (#{duration}s)"
89
+ puts "Ripping audio file to #{tempfile}"
90
+
91
+ command = %W(
92
+ rtmpdump --live --quiet
93
+ -r #{@rtmp}
94
+ --playpath #{@playpath}
95
+ --app "live"
96
+ -W #{PLAYER_URL}
97
+ --live --stop #{duration} -o - |
98
+ ffmpeg -y -i - -vn
99
+ -loglevel error
100
+ -metadata author="NHK"
101
+ -metadata artist="#{program.station}"
102
+ -metadata title="#{program.title} #{program.effective_date.strftime}"
103
+ -metadata album="#{program.title}"
104
+ -metadata genre=Radio
105
+ -metadata year="#{program.effective_date.year}"
106
+ -acodec libmp3lame -ar 44100 -ab #{bitrate} -ac 2
107
+ -id3v2_version 3
108
+ #{tempfile}
109
+ )
110
+
111
+ system command.join(" ")
112
+
113
+ FileUtils.mkpath(outdir)
114
+ File.rename tempfile, "#{outdir}/#{program.id}.mp3"
115
+
116
+ end
117
+
118
+ def abort(msg)
119
+ puts msg
120
+ exit 1
121
+ end
122
+ end
123
+
124
+ class Program
125
+ attr_accessor :id, :station, :title, :from, :to, :duration, :info
126
+ def initialize(args = {})
127
+ args.each do |k, v|
128
+ send "#{k}=", v
129
+ end
130
+ end
131
+
132
+ def effective_date
133
+ time = from.hour < 5 ? from - 24 * 60 * 60 : from
134
+ Date.new(time.year, time.month, time.day)
135
+ end
136
+
137
+ def recording_duration
138
+ (to - Time.now).to_i
139
+ end
140
+ end
141
+
142
+ class CacheDir
143
+ attr_accessor :dir
144
+ def initialize(dir)
145
+ @dir = dir
146
+ @paths = {}
147
+ end
148
+
149
+ def [](name)
150
+ @paths[name] ||= Pathname.new(File.join(@dir, name))
151
+ end
152
+ end
153
+
154
+ DownloadTask.new(*ARGV).run
@@ -0,0 +1,3 @@
1
+ module Ripdiru
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ripdiru.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "ripdiru/version"
2
+
3
+ module Ripdiru
4
+ # Your code goes here...
5
+ end
data/ripdiru.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ripdiru/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ripdiru"
8
+ spec.version = Ripdiru::VERSION
9
+ spec.authors = ["harupong"]
10
+ spec.email = ["harupong@gmail.com"]
11
+ spec.description = %q{Radiru*Radiru ripper}
12
+ spec.summary = %q{Streams and rips NHK radio programs}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'nokogiri'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ripdiru
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - harupong
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Radiru*Radiru ripper
63
+ email:
64
+ - harupong@gmail.com
65
+ executables:
66
+ - ripdiru
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/ripdiru
76
+ - lib/ripdiru.rb
77
+ - lib/ripdiru/version.rb
78
+ - ripdiru.gemspec
79
+ homepage: ''
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.23
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Streams and rips NHK radio programs
104
+ test_files: []