From d752a0f5f78206b3312af51d4be30a6bac7dd56c Mon Sep 17 00:00:00 2001 From: Petr Schmied Date: Mon, 14 Sep 2015 12:18:40 +0200 Subject: [PATCH] Internationalization prep, run working (win10) --- .gitignore | 1 + budik.gemspec | 1 + lib/budik.rb | 84 +++++++++++++++++++----------------- lib/budik/command.rb | 4 +- lib/budik/config/lang/en.yml | 32 +++++++++++++- lib/budik/config/options.yml | 8 ++-- lib/budik/config/sources.yml | 0 lib/budik/devices.rb | 9 ++-- lib/budik/output.rb | 18 ++++++++ lib/budik/player.rb | 24 ++++++----- lib/budik/sources.rb | 2 +- 11 files changed, 121 insertions(+), 62 deletions(-) delete mode 100644 lib/budik/config/sources.yml create mode 100644 lib/budik/output.rb diff --git a/.gitignore b/.gitignore index 0cb6eeb..8b26323 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ /pkg/ /spec/reports/ /tmp/ +/lib/budik/config/sources.yml diff --git a/budik.gemspec b/budik.gemspec index 6762ce3..09ee194 100644 --- a/budik.gemspec +++ b/budik.gemspec @@ -30,6 +30,7 @@ Gem::Specification.new do |spec| spec.add_dependency "awesome_print" spec.add_dependency "commander" spec.add_dependency "r18n-core" + spec.add_dependency "terminal-table" spec.add_dependency "ya2yaml" spec.add_dependency "youtube_addy" spec.add_dependency "youtube-dl.rb" diff --git a/lib/budik.rb b/lib/budik.rb index 96ad799..52350b0 100644 --- a/lib/budik.rb +++ b/lib/budik.rb @@ -2,6 +2,7 @@ #require 'awesome_print' require 'commander' +require 'date' require 'fileutils' require 'json' require 'net/http' @@ -9,6 +10,7 @@ require 'r18n-core' require 'singleton' require 'socket' +require 'terminal-table' require 'uri' require 'ya2yaml' require 'yaml' @@ -18,6 +20,7 @@ require './lib/budik/command' require './lib/budik/config' require './lib/budik/devices' +require './lib/budik/output' require './lib/budik/player' require './lib/budik/rng' require './lib/budik/sources' @@ -27,52 +30,59 @@ module Budik class Budik include Commander::Methods + def initialize + @strings = Config.instance.lang.budik + end + def run - program :name, 'Budík' + program :name, 'Budik' program :version, '0.0.1' - program :description, 'Alarm clock which randomly plays a song or a video from YouTube or your local collection.' + program :description, @strings.description default_command :run command :config do |c| - c.syntax = 'Budík.rb config [options]' - c.summary = 'Edit program configuration.' - c.description = 'Open options file in your default editor.' - c.option '-r', '--reset [string]', String, 'Resets configuration file to default values.' + str_config = @strings.commands.config + str_opts = @strings.commands.config.options + + c.syntax = 'budik config [options]' + c.summary = str_config.summary + c.description = str_config.description + c.option '-r', '--reset [string]', String, str_opts.reset + c.action do |_args, opts| Command.instance.config(_args, opts) end end command :run do |c| - c.syntax = 'Budík.rb run [options]' - c.summary = 'DEFAULT: Runs alarm.' - c.description = 'Runs alarm with specified options. CLI options > options.yml.' - c.option '-c', '--categories [string]', String, 'Limit selection by categories. Example usage: "cat1.subcat1 cat2.subcat1.subsubcat1 .excludethis.subcat etc."' - c.option '-d', '--download-keep [string]', String, 'Override download method set in your active options.' - c.option '-n', '--number [integer]', Integer, 'Override selection using random number generator by specifying fixed number.' - c.option '-p', '--player [string]', String, 'Override which player to use.' - c.option '-r', '--rng [string]', String, 'Override random number generation method specified in your options.yml file. Possible values: "hwrng", "random.org", "rand-hwrng-seed", "rand". Default value: "hwrng".' - c.action do |_args, opts| - Command.instance.run(_args, opts) - end - end + str_run = @strings.commands.run + str_opts = @strings.commands.run.options + + c.syntax = 'budik run [options]' + c.summary = str_run.summary + c.description = str_run.description + c.option '-c', '--categories [string]', String, str_opts.categories + c.option '-d', '--dl-keep [string]', String, str_opts.dl_keep + 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 - command :set do |c| - c.syntax = 'Budík.rb set [options]' - c.summary = 'Set alarm.' - c.description = 'Set alarm using systemd timers, cron or schtasks.' c.action do |_args, opts| - Command.instance.set(_args, opts) + Command.instance.run(_args, opts) end end command :sources do |c| - c.syntax = 'Budík.rb sources [options]' - c.summary = 'Manage source(s).' - c.description = 'List or download source(s) or edit your sources file in your default editor (default: edit).' - c.option '-l', '--list [string]', String, 'List sources. Can be restricted by categories. DEFAULT option. Usage: list "all|category.subcategory .exclude.this". Default: list "all"' - c.option '-d', '--download [string]', String, 'Downloads source(s). Usage: download "all|number|{category: category, number: number}|path|name". Default: download "all".' + str_sources = @strings.commands.sources + str_opts = @strings.commands.sources.options + + c.syntax = 'budik sources [options]' + c.summary = str_sources.summary + c.description = str_sources.description + c.option '-l', '--list [string]', String, str_opts.list + c.option '-d', '--download [string]', String, str_opts.download + c.action do |_args, opts| Command.instance.sources(_args, opts) end @@ -81,20 +91,14 @@ def run alias_command :test, :'run', '--trace' command :translate do |c| - c.syntax = 'Budík.rb translate [options]' - c.summary = 'Translate app.' - c.description = 'Opens your language file in your default editor.' - c.action do |_args, opts| - Command.instance.translate(_args, opts) - end - end + str_translate = @strings.commands.translate + + c.syntax = 'budik translate [options]' + c.summary = str_translate.summary + c.description = str_translate.description - command :unset do |c| - c.syntax = 'Budík.rb unset [options]' - c.summary = 'Unset alarm.' - c.description = 'Unset alarm.' c.action do |_args, opts| - Command.instance.unset(_args, opts) + Command.instance.translate(_args, opts) end end diff --git a/lib/budik/command.rb b/lib/budik/command.rb index 95d23a8..2f42e4a 100644 --- a/lib/budik/command.rb +++ b/lib/budik/command.rb @@ -9,7 +9,7 @@ def run(_args, opts) options = Config.instance.options dl_options = options['sources']['download'] - dl_options['keep'] = opts.download_keep if opts.download_keep + dl_options['keep'] = opts.dl_keep if opts.dl_keep options['player']['player'] = opts.player if opts.player options['rng']['method'] = opts.rng if opts.rng @@ -21,6 +21,8 @@ def run(_args, opts) sources.parse(options['sources']['path'], opts.categories) devices.storage_mount number = opts.number ? opts.number : rng.generate(sources.count) + + puts Output.instance.run_info_table(number, sources.sources[number][:name]) sources.download(number) devices.tv_on diff --git a/lib/budik/config/lang/en.yml b/lib/budik/config/lang/en.yml index d005d98..ad38cab 100644 --- a/lib/budik/config/lang/en.yml +++ b/lib/budik/config/lang/en.yml @@ -1,5 +1,33 @@ lang: "en" # Do not remove this line. -config: - example: "This is an example string." +budik: + description: "Alarm clock which randomly plays a song or a video from YouTube or your local collection." + commands: + config: + summary: "Edit program configuration." + description: "Open options file in your default editor." + options: + reset: "Resets configuration file to default values." + run: + summary: "Runs alarm (default command)." + 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." + 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\"." + sources: + summary: "Manage source(s)." + description: "List or download source(s) or edit your sources file in your default editor (default: edit)." + options: + list: "List sources. Can be restricted by categories. DEFAULT option. Usage: list \"all|category.subcategory .exclude.this\". Default: list \"all\"" + download: "Downloads source(s). Usage: download \"all|number\". Default: download \"all\"." + translate: + summary: "Translate app." + description: "Opens your language file in your default editor." +output: + date: "DateTime" + number: "Number" + alarm: "Alarm" sources: invalid_item: "Item %1 is invalid." diff --git a/lib/budik/config/options.yml b/lib/budik/config/options.yml index 0ec5b1f..d9c4a2b 100644 --- a/lib/budik/config/options.yml +++ b/lib/budik/config/options.yml @@ -10,7 +10,7 @@ player: vlc: default_volume: 128 fullscreen: true - path: "C:/Users/Petr Schmied/Desktop/Portable Apps/VLCPortable/VLCPortable.exe" + path: "C:/Users/peter/Desktop/Portable Apps/VLCPortable/VLCPortable.exe" rc_host: localhost rc_port: 50000 volume_fadein_secs: 0.125 @@ -20,7 +20,7 @@ player: rng: hwrng: source: "" - method: random.org + method: rand random.org: apikey: sources: @@ -28,12 +28,12 @@ sources: download: device: "" dir: downloads/ - keep: true + keep: false mount: '' partition: "" sleep: '' unmount: '' - path: ./lib/budik/config/templates/sources_example.yml + path: ./lib/budik/config/sources.yml tv: available: false use_if_no_video: false diff --git a/lib/budik/config/sources.yml b/lib/budik/config/sources.yml deleted file mode 100644 index e69de29..0000000 diff --git a/lib/budik/devices.rb b/lib/budik/devices.rb index 04cece3..a4a14a1 100644 --- a/lib/budik/devices.rb +++ b/lib/budik/devices.rb @@ -6,11 +6,13 @@ def initialize options = Config.instance.options tv_options = options['tv'] + @tv = Hash.new if tv_options['available'] - @tv = Hash.new @tv[:use_if_no_video] = tv_options['use_if_no_video'] @tv[:wait_secs_after_on] = tv_options['wait_secs_after_on'] @tv[:on] = false + else + @tv[:on] = nil end storage_options = options['sources']['download'] @@ -69,11 +71,10 @@ def storage_sleep def tv_on unless @tv[:on] == nil || @tv[:on] == true system('echo "on 0" | cec-client -s') + sleep(@tv[:wait_secs_after_on]) unless @tv[:wait_secs_after_on] == nil + system('echo "as" | cec-client -s') end @tv[:on] = true - - sleep(@tv[:wait_secs_after_on]) unless @tv[:wait_secs_after_on] == nil - system('echo "as" | cec-client -s') end def tv_off diff --git a/lib/budik/output.rb b/lib/budik/output.rb new file mode 100644 index 0000000..5183a22 --- /dev/null +++ b/lib/budik/output.rb @@ -0,0 +1,18 @@ +module Budik + class Output + include Singleton + + def initialize + @strings = Config.instance.lang.output + end + + def run_info_table(number, name) + rows = [] + rows << [@strings.date, DateTime.now.strftime('%d/%m/%Y %H:%M')] + rows << [@strings.number, number.to_s] + rows << [@strings.alarm, name] + + Terminal::Table.new title: 'Budik', rows: rows + end + end +end diff --git a/lib/budik/player.rb b/lib/budik/player.rb index 601b218..2c5e4de 100644 --- a/lib/budik/player.rb +++ b/lib/budik/player.rb @@ -24,10 +24,10 @@ def play(source) def omxplayer(source) source[:path].each do |item| - command = @player_options['path'] + ' --vol ' + @player_options['defaultVolume'].to_s + ' ' + item + command = @player_options['path'] + ' --vol ' + @player_options['default_volume'].to_s + ' ' + item Open3.popen3(command) do |i, o, e, t| 7.times do - sleep(@player_options['volumeStepSecs']) + sleep(@player_options['volume_step_secs']) i.puts 'volup' end i.close @@ -36,30 +36,34 @@ def omxplayer(source) end def vlc(source) - vlc_path = @player_options['path'].gsub(' ', '\\ ') - rc = @player_options['rcHost'] + ':' + @player_options['rcPort'].to_s - command = vlc_path + ' --extraintf rc --rc-host ' + rc + ' --volume-step ' + @player_options['volumeStep'].to_s + (@player_options['fullscreen'] ? ' --fullscreen ' : ' ') + # TODO: is_url etc + vlc_path + + vlc_path = '"' + @player_options['path'] + '"'#.gsub(' ', '\\ ') + rc = @player_options['rc_host'] + ':' + @player_options['rc_port'].to_s + command = vlc_path + ' --extraintf rc --rc-host ' + rc + ' --volume-step ' + @player_options['volume_step'].to_s + (@player_options['fullscreen'] ? ' --fullscreen ' : ' ') source[:path].each do |item| + is_url = (item =~ /\A#{URI::regexp(['http', 'https'])}\z/) + item = Config.instance.options['sources']['download']['dir'] + YouTubeAddy.extract_video_id(item) + '.mp4' if is_url command += ('"file:///' + item + '" ') end command += 'vlc://quit' vlc_pid = spawn(command) - sleep(@player_options['waitSecsAfterVlcRuns']) + sleep(@player_options['wait_secs_after_run']) while true do begin - rc = TCPSocket.open(@player_options['rcHost'], @player_options['rcPort']) + rc = TCPSocket.open(@player_options['rc_host'], @player_options['rc_port']) break rescue next end end - rc.puts 'volume ' + @player_options['defaultVolume'].to_s + rc.puts 'volume ' + @player_options['default_volume'].to_s (1..128).each do |i| - sleep(@player_options['volumeFadeInSecs']) - rc.puts 'volup ' + @player_options['volumeStep'].to_s + sleep(@player_options['volume_fadein_secs']) + rc.puts 'volup ' + @player_options['volume_step'].to_s end Process.wait(vlc_pid) diff --git a/lib/budik/sources.rb b/lib/budik/sources.rb index 7cb281e..dead112 100644 --- a/lib/budik/sources.rb +++ b/lib/budik/sources.rb @@ -36,7 +36,7 @@ def download(number = nil) item[:path].each do |path| id = YouTubeAddy.extract_video_id(path) if id - path = File.expand_path(dir + id + '.mp4') + path = File.expand_path(dir + id + '.mp4') # TODO: remove, doesn't work download_youtube(id, dir) end end