Skip to content

Commit

Permalink
Streaming & install
Browse files Browse the repository at this point in the history
  • Loading branch information
JBlackN committed Sep 15, 2015
1 parent 4102c93 commit 819e4ca
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 63 deletions.
1 change: 1 addition & 0 deletions budik.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'awesome_print'
spec.add_dependency 'commander'
spec.add_dependency 'r18n-core'
spec.add_dependency 'sys-uname'
spec.add_dependency 'terminal-table'
spec.add_dependency 'ya2yaml'
spec.add_dependency 'youtube_addy'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ budik:
description: "Runs alarm with specified options. CLI options > options.yml."
options:
categories: "Limit selection by categories. Example usage: \"cat1.subcat1 cat2.subcat1.subsubcat1 .excludethis.subcat etc.\""
dl_keep: "Override download method set in your active options."
dl_method: "Override download method set in your active options."
number: "Override selection using random number generator by specifying fixed number."
player: "Override which player to use."
rng: "Override random number generation method specified in your options.yml file. Possible values: \"hwrng\", \"random.org\", \"rand-hwrng-seed\", \"rand\". Default value: \"hwrng\"."
Expand Down
39 changes: 39 additions & 0 deletions config/templates/options/linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
os: linux
lang: en
player:
omxplayer:
default_volume: -2100
path: omxplayer
volume_step_secs: 3
player: vlc
vlc:
default_volume: 128
fullscreen: true
path: vlc
rc_host: localhost
rc_port: 50000
volume_fadein_secs: 0.125
volume_step: 1.0
wait_secs_after_run: 5
wait_secs_if_http: 3
rng:
hwrng:
source: /dev/urandom
method: hwrng
random.org:
apikey: ""
sources:
download:
device: ""
dir: ~/.budik/downloads/
method: stream
mount: ""
partition: ""
sleep: ""
unmount: ""
path: ~/.budik/sources.yml
tv:
available: false
use_if_no_video: false
wait_secs_after_on: 0
39 changes: 39 additions & 0 deletions config/templates/options/rpi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
os: rpi
lang: en
player:
omxplayer:
default_volume: -2100
path: omxplayer
volume_step_secs: 3
player: omxplayer
vlc:
default_volume: 128
fullscreen: true
path: vlc
rc_host: localhost
rc_port: 50000
volume_fadein_secs: 0.125
volume_step: 1.0
wait_secs_after_run: 5
wait_secs_if_http: 3
rng:
hwrng:
source: /dev/random
method: hwrng
random.org:
apikey: ""
sources:
download:
device: /dev/sda
dir: /mnt/.budik/downloads/
method: keep
mount: "udisksctl mount -b $partition"
partition: /dev/sda1
sleep: ""
unmount: "udisksctl unmount -b $partition"
path: sources.yml
tv:
available: true
use_if_no_video: true
wait_secs_after_on: 15
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
---
---
os: windows
lang: en
player:
omxplayer:
player:
omxplayer:
default_volume: -2100
path: omxplayer
volume_step_secs: 3
player: vlc
vlc:
vlc:
default_volume: 128
fullscreen: true
path: "C:/Users/peter/Desktop/Portable Apps/VLCPortable/VLCPortable.exe"
path: vlc
rc_host: localhost
rc_port: 50000
volume_fadein_secs: 0.125
volume_step: 1.0
wait_secs_after_run: 5
wait_secs_if_http: 3
rng:
hwrng:
rng:
hwrng:
source: ""
method: rand
random.org:
apikey:
sources:
categories: all
download:
random.org:
apikey: ""
sources:
download:
device: ""
dir: downloads/
keep: false
mount: ''
dir: ~/.budik/downloads/
method: stream
mount: ""
partition: ""
sleep: ''
unmount: ''
path: ./lib/budik/config/sources.yml
tv:
sleep: ""
unmount: ""
path: ~/.budik/sources.yml
tv:
available: false
use_if_no_video: false
wait_secs_after_on: 0
File renamed without changes.
3 changes: 2 additions & 1 deletion lib/budik.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'r18n-core'
require 'singleton'
require 'socket'
require 'sys/uname'
require 'terminal-table'
require 'uri'
require 'ya2yaml'
Expand Down Expand Up @@ -85,7 +86,7 @@ def command_run(str_opts)

def command_run_options(c, str_opts)
c.option '-c', '--categories [string]', String, str_opts.categories
c.option '-d', '--dl-keep [string]', String, str_opts.dl_keep
c.option '-d', '--dl-method [string]', String, str_opts.dl_method
c.option '-n', '--number [integer]', Integer, str_opts.number
c.option '-p', '--player [string]', String, str_opts.player
c.option '-r', '--rng [string]', String, str_opts.rng
Expand Down
11 changes: 9 additions & 2 deletions lib/budik/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Budik
class Command
def initialize(command, opts)
@options = Config.instance.options
@dl_method = @options['sources']['download']['method']
@sources = Config.instance.sources
@strings = Config.instance.lang.command

Expand All @@ -23,13 +24,15 @@ def run(opts)

run_use_cli_opts(opts)
source = run_prepare(opts, sources, devices, rng, output)
run_download(source, @options['sources']['download']['method'], sources)
run_play(source, devices, player, sources)
end

def run_use_cli_opts(opts)
@options['sources']['download']['keep'] = opts.dl_keep if opts.dl_keep
@options['player']['player'] = opts.player if opts.player
@options['rng']['method'] = opts.rng if opts.rng
return unless opts.dl_method
@options['sources']['download']['method'] = opts.dl_method
end

def run_prepare(opts, sources, devices, rng, output)
Expand All @@ -42,7 +45,11 @@ def run_prepare(opts, sources, devices, rng, output)
source = sources.get(number)

puts output.run_info_table(number, source[:name])
sources.download(source)
source
end

def run_download(source, dl_method, sources)
dl_method == 'stream' ? source : sources.download(source)
end

def run_play(source, devices, player, sources)
Expand Down
50 changes: 43 additions & 7 deletions lib/budik/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,60 @@ class Config
include Singleton

def initialize
@options = YAML.load_file('./lib/budik/config/options.yml')
@sources = YAML.load_file(@options['sources']['path'])
install unless installed?

R18n.default_places = './lib/budik/config/lang/'
@options = YAML.load_file(Dir.home + '/.budik/options.yml')
@sources = YAML.load_file(File.expand_path(@options['sources']['path']))
@lang = init_lang
end

def init_lang
R18n.default_places = Dir.home + '/.budik/lang/'
R18n.set(@options['lang'])
@lang = R18n.t
R18n.t
end

attr_accessor :lang
attr_accessor :options
attr_accessor :sources
attr_accessor :lang, :options, :sources

def platform?
os = Sys::Platform.linux? ? :linux : :windows
rpi?(os) ? :rpi : os
end

def edit(_opts)
# TODO
end

def install
dir = Dir.home + '/.budik/'
lang_dir = dir + 'lang/'
download_dir = dir + 'downloads/'

options = './config/templates/options/' + platform?.to_s + '.yml'
sources = './config/templates/sources/sources.yml'
lang = './config/templates/lang/en.yml'

FileUtils.mkdir_p([dir, lang_dir, download_dir])
FileUtils.cp options, dir + 'options.yml'
FileUtils.cp sources, dir
FileUtils.cp lang, lang_dir
end

def installed?
File.file?(Dir.home + '/.budik/options.yml')
end

def reset
# TODO
end

def rpi?(os)
return false unless os == :linux
cpuinfo = File.read('/proc/cpuinfo')
hardware = cpuinfo.scan(/[hH]ardware\s*:\s*(\w+)/).first.first
hardware =~ /BCM270[89]/
rescue
false
end
end
end
7 changes: 6 additions & 1 deletion lib/budik/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@ def vlc_cmd_add_items(source)
files = ''
source[:path].each do |item|
item_path = Sources.instance.locate_item(item).gsub(%r{^/}, '')
files += ('"file:///' + item_path + '" ')
files += (vlc_cmd_item_prefix(item_path) + item_path + '" ')
end
files += 'vlc://quit'
end

def vlc_cmd_item_prefix(item_path)
is_url = (item_path =~ /\A#{URI.regexp(%w(http https))}\z/)
is_url ? '"' : '"file:///'
end

def vlc_rc_connect
rc_host = @player_options['rc_host']
rc_port = @player_options['rc_port']
Expand Down
15 changes: 8 additions & 7 deletions lib/budik/sources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ class Sources

def initialize
@sources = []
@dir = Config.instance.options['sources']['download']['dir']
@keep = Config.instance.options['sources']['download']['keep']
dir = Config.instance.options['sources']['download']['dir']
@dir = File.expand_path(dir) + '/'
@method = Config.instance.options['sources']['download']['method']
end

attr_accessor :sources, :dir, :keep
attr_accessor :sources, :dir, :method

# FIXME: music, youtube.music
def apply_mods(mods)
@sources.delete_if do |source|
mods[:adds].all? { |mod| !(mod == (source[:category] & mod)) }
Expand All @@ -30,7 +32,6 @@ def download(source = nil)
source[:path].each do |path|
download_youtube(YouTubeAddy.extract_video_id(path))
end
source
else
@sources.each { |src| download(src) }
end
Expand All @@ -52,6 +53,7 @@ def get(number)
end

def locate_item(item)
return item if @method == 'stream'
is_url = (item =~ /\A#{URI.regexp(%w(http https))}\z/)
is_url ? @dir + YouTubeAddy.extract_video_id(item) + '.mp4' : item
end
Expand Down Expand Up @@ -113,12 +115,11 @@ def parse_mods(mods)
end

def remove(source = nil)
return if @keep
return unless @method == 'remove'

if source
source[:path].each do |path|
normalized_path = locate_item(path)
FileUtils.rm File.expand_path(normalized_path), force: true
FileUtils.rm File.expand_path(locate_item(path)), force: true
end
else
@sources.each { |src| remove(src) }
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/budik/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
context 'with default values' do
it 'loads configuration files' do
config = Budik::Config.instance
options = './lib/budik/config/options.yml'
sources = config.options['sources']['path']
options = Dir.home + '/.budik/options.yml'
sources = File.expand_path(config.options['sources']['path'])

expect(config.lang.lang.class).to eq R18n::TranslatedString
expect(config.options).to eq YAML.load_file(options)
Expand Down
Loading

0 comments on commit 819e4ca

Please sign in to comment.