Skip to content

Commit

Permalink
Internationalization prep, run working (win10)
Browse files Browse the repository at this point in the history
  • Loading branch information
JBlackN committed Sep 14, 2015
1 parent c49564c commit d752a0f
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/pkg/
/spec/reports/
/tmp/
/lib/budik/config/sources.yml
1 change: 1 addition & 0 deletions budik.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
84 changes: 44 additions & 40 deletions lib/budik.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#require 'awesome_print'
require 'commander'
require 'date'
require 'fileutils'
require 'json'
require 'net/http'
require 'open3'
require 'r18n-core'
require 'singleton'
require 'socket'
require 'terminal-table'
require 'uri'
require 'ya2yaml'
require 'yaml'
Expand All @@ -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'
Expand All @@ -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
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/budik/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
32 changes: 30 additions & 2 deletions lib/budik/config/lang/en.yml
Original file line number Diff line number Diff line change
@@ -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."
8 changes: 4 additions & 4 deletions lib/budik/config/options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,20 +20,20 @@ player:
rng:
hwrng:
source: ""
method: random.org
method: rand
random.org:
apikey:
sources:
categories: all
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
Expand Down
Empty file removed lib/budik/config/sources.yml
Empty file.
9 changes: 5 additions & 4 deletions lib/budik/devices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/budik/output.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 14 additions & 10 deletions lib/budik/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/budik/sources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d752a0f

Please sign in to comment.