bin/srd in sifttter-redux-0.3.1 vs bin/srd in sifttter-redux-0.3.2

- old
+ new

@@ -1,236 +1,239 @@ #!/usr/bin/env ruby -#------------------------------------------------------------------------------------------------------------- +# Encoding: utf-8 +#-------------------------------------------------------------------- # Sifttter Redux # -# A modification of Craig Eley's Sifttter that allows for smart installation on a standalone *NIX -# device (such as a Raspberry Pi). +# A modification of Craig Eley's Sifttter that allows for smart +# installation on a standalone *NIX device (such as a Raspberry Pi). # # Sifttter copyright Craig Eley 2014 <http://craigeley.com> # # Copyright (c) 2014 # Aaron Bach <bachya1208@gmail.com> -# +# # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation # files (the "Software"), to deal in the Software without # restriction, including without limitation the rights to use, # copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following # conditions: -# +# # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. -#------------------------------------------------------------------------------------------------------------- +#-------------------------------------------------------------------- -require 'colored' require 'fileutils' require 'gli' -require 'sifttter-redux' -require 'time' -require 'yaml' +require 'sifttter_redux' include GLI::App +version SifttterRedux::VERSION +program_desc 'Sifttter Redux -program_desc "Sifttter Redux + A customized IFTTT-to-Day One service that allows for + smart installation and automated running on a standalone + *NIX device (such as a Raspberry Pi).' - A modification of Craig Eley's Sifttter (an IFTTT-to-Day One service) - that allows for smart installation and automated running on a standalone - *NIX device (such as a Raspberry Pi)." +# ====================================================== +# Global Flags and Switches +# ====================================================== -version SifttterRedux::VERSION +switch( + [:verbose], + desc: 'Turns on verbose output' +) -#| ====================================================== -#| Pre, Post, and Error -#| ====================================================== +# ====================================================== +# Pre, Post, and Error +# ====================================================== -pre do |global,command,options,args| - $config = ConfigManager.instance - $date_range_maker = DateRangeMaker.new +pre do |global, command, options, args| + SifttterRedux::Configuration.load(SifttterRedux::SRD_CONFIG_FILEPATH) + SifttterRedux::DBU.load( + File.join( + SifttterRedux::Configuration['db_uploader']['local_filepath'], + 'dropbox_uploader.sh' + ) + ) - if !File.exists?(SifttterRedux::SRD_CONFIG_FILEPATH) - CliMessage.info("It doesn't look like you've initlized Sifttter Redux yet. Doing that now...") - initialize_procedures - end - - $db_uploader = File.join($config.db_uploader["local_filepath"], "dropbox_uploader.sh") + init unless File.exists?(SifttterRedux::SRD_CONFIG_FILEPATH) true end -post do |global,command,options,args| - # Post logic here - # Use skips_post before a command to skip this - # block on that command only -end +# ====================================================== +# Commands +# ====================================================== -on_error do |exception| - # Error logic here - # return false to skip default error handling - true -end +# ------------------------------------------------------ +# exec command +# +# Executes the script. +# ------------------------------------------------------ -#| ====================================================== -#| Commands -#| ====================================================== - -#| ------------------------------------------------------ -#| exec command -#| -#| Executes the script. -#| ------------------------------------------------------ - desc 'Execute the script' -command :exec do |c| - +command :exec do |c| + c.flag( [:f], - :desc => 'Run catch-up mode with this start date', + desc: 'Run catch-up mode with this start date' ) - + c.flag( [:n], - :desc => 'Run catch-up mode for the last N days' + desc: 'Run catch-up mode for the last N days' ) - + c.flag( [:t], - :desc => 'Run catch-up mode with this end date (must be accompanied by -f)', + desc: 'Run catch-up mode with this end date (must also have -f)' ) - + c.flag( [:w], - :desc => 'Run catch-up mode for the last N weeks' + desc: 'Run catch-up mode for the last N weeks' ) - + c.switch( [:c], - :desc => 'Run catch-up mode for the current week (i.e., the beginning of the week to yesterday)' + desc: 'Run catch-up mode from the beginning of the week to yesterday' ) - + c.switch( [:i], - :desc => 'Include today\'s date in catch-up' + desc: "Include today's date in catch-up" ) - + c.switch( + [:verbose], + desc: 'Turns on verbose output' + ) + + c.switch( [:y], - :desc => 'Run catch-up mode for yesterday' + desc: 'Run catch-up mode for yesterday' ) - + c.action do |global_options, options, args| - command_complete = false - CliMessage.section('EXECUTING...') - - if (options[:c] || options[:n] || options[:w] || options[:y] || options[:f] || options[:t]) + SifttterRedux.verbose = global_options[:verbose] || options[:verbose] - # Current Week - if (!command_complete && options[:c]) - dates = $date_range_maker.last_n_weeks(0, {:include_today => options[:i]}) - command_complete = true - end - - # Last N Days - if (!command_complete && options[:n]) - dates = $date_range_maker.last_n_days(options[:n].to_i, {:include_today => options[:i]}) - command_complete = true - end - - # Yesterday - if (!command_complete && options[:y]) - dates = $date_range_maker.yesterday - command_complete = true - end - - # Last N Weeks - if (!command_complete && options[:w]) - dates = $date_range_maker.last_n_weeks(options[:w].to_i, {:include_today => options[:i]}) - command_complete = true - end - - # Specific Range - if (!command_complete && (options[:f] || options[:t])) - begin - dates = $date_range_maker.range(options[:f], options[:t], {:include_today => options[:i]}) - - if (dates.last > Date.today) - CliMessage.warning("Ignoring overextended end date and using today's date (#{Date.today})") - dates = (dates.first..Date.today) + SifttterRedux::CliMessage.section_block('EXECUTING...') do + if options[:c] || options[:n] || options[:w] || options[:y] || options[:f] || options[:t] + + command_complete = false + + # Current Week + if !command_complete && options[:c] + dates = SifttterRedux::DateRangeMaker.last_n_weeks(0, options[:i]) + command_complete = true + end + + # Last N Days + if !command_complete && options[:n] + dates = SifttterRedux::DateRangeMaker.last_n_days(options[:n].to_i, options[:i]) + command_complete = true + end + + # Yesterday + if !command_complete && options[:y] + dates = SifttterRedux::DateRangeMaker.yesterday + command_complete = true + end + + # Last N Weeks + if !command_complete && options[:w] + dates = SifttterRedux::DateRangeMaker.last_n_weeks(options[:w].to_i, options[:i]) + command_complete = true + end + + # Specific Range + if !command_complete && (options[:f] || options[:t]) + begin + dates = SifttterRedux::DateRangeMaker.from_to(options[:f], options[:t], options[:i]) + + if dates.last > Date.today + SifttterRedux::CliMessage.warning("Ignoring overextended end date and using today's date (#{ Date.today })") + dates = (dates.first..Date.today) + end + rescue ArgumentError => e + SifttterRedux::CliMessage.error(e) end - rescue DateRangeMakerError - CliMessage.error($!) end - command_complete = true - end - else - dates = $date_range_maker.today - command_complete = true - end - - if (!dates.nil?) - first_date = dates.first - second_date = dates.reverse_each.first - - if (first_date == second_date) - CliMessage.info("Creating entry for #{first_date.strftime('%B %d, %Y')}...") else - CliMessage.info("Creating entries for dates from #{first_date.strftime('%B %d, %Y')} to #{second_date.strftime('%B %d, %Y')}...") + dates = SifttterRedux::DateRangeMaker.today end - - download_sifttter_files - - dates.each do |date| - run_sifttter(date) + + unless dates.nil? + first_date = dates.first + second_date = dates.reverse_each.first + + if first_date == second_date + date_string = first_date.strftime('%B %d, %Y') + SifttterRedux::CliMessage.info("Creating entry for #{ date_string }...") + else + date_string = "#{ first_date.strftime('%B %d, %Y') } to #{ second_date.strftime('%B %d, %Y') }" + SifttterRedux::CliMessage.info("Creating entries for dates from #{ date_string }...") + end + + SifttterRedux::DBU.local_target = '/tmp/sifttter' + SifttterRedux::DBU.remote_target = '/Apps/ifttt/sifttter' + SifttterRedux::DBU.message = 'Downloading Sifttter files...' + SifttterRedux::DBU.download + + dates.each do |date| + SifttterRedux::Sifttter.run(date) + end + + # Upload any Day One entries to Dropbox (if there are any). + unless Dir[SifttterRedux::Configuration['sifttter_redux']['dayone_local_filepath'] + '/*'].empty? + SifttterRedux::DBU.local_target = "#{SifttterRedux::Configuration['sifttter_redux']['dayone_local_filepath']}/*" + SifttterRedux::DBU.remote_target = SifttterRedux::Configuration['sifttter_redux']['dayone_remote_filepath'] + SifttterRedux::DBU.message = 'Uploading Day One entries to Dropbox...' + SifttterRedux::DBU.upload + end + + # Remove any downloaded local files that we no longer need. + dirs = [ + SifttterRedux::Configuration['sifttter_redux']['dayone_local_filepath'], + SifttterRedux::Configuration['sifttter_redux']['sifttter_local_filepath'] + ] + + SifttterRedux::CliMessage.info_block('Removing temporary local files...') { dirs.each { |d| FileUtils.rm_rf(d) } } end - - # Upload any Day One entries to Dropbox (if there are any). - if (!Dir[$config.sifttter_redux["dayone_local_filepath"] + "/*"].empty?) - CliMessage.info("Uploading Day One entries to Dropbox...", false) - output = `#{$db_uploader} upload #{$config.sifttter_redux["dayone_local_filepath"] + "/*"} #{$config.sifttter_redux["dayone_remote_filepath"]}` - CliMessage.finish_message('DONE.') - end - - # Remove any downloaded local files that we no longer need. - CliMessage.info("Removing downloaded Day One files...", false) - FileUtils.rm_rf($config.sifttter_redux["dayone_local_filepath"]) if Dir.exists?($config.sifttter_redux["dayone_local_filepath"]) - CliMessage.finish_message('DONE.') - - CliMessage.info("Removing downloaded Sifttter files...", false) - FileUtils.rm_rf($config.sifttter_redux["sifttter_local_filepath"]) if Dir.exists?($config.sifttter_redux["sifttter_local_filepath"]) - CliMessage.finish_message('DONE.') end - - CliMessage.section('EXECUTION COMPLETE!') end + end -#| ------------------------------------------------------ -#| init command -#| -#| Initializes the script. -#| ------------------------------------------------------ +# ------------------------------------------------------ +# init command +# +# Initializes the script. +# ------------------------------------------------------ desc 'Install and initialize dependencies' command :init do |c| c.action do |global_options, options, args| - CliMessage.section('INITIALIZING...') - if File.exists?($config.configFile) - initialize_procedures if CliMessage.prompt("It looks like you've already initialized Sifttter Redux. Do you want to re-initialize?", "N").downcase == 'y' - else - initialize_procedures + SifttterRedux::CliMessage.section_block('INITIALIZING...') do + if File.exists?(SifttterRedux::Configuration.config_path) + SifttterRedux.init if SifttterRedux::CliMessage.prompt("It looks like you've already initialized Sifttter Redux. Do you want to re-initialize?", 'N').downcase == 'y' + else + SifttterRedux.init + end end - - CliMessage.section('INITIALIZATION COMPLETE!') + end end exit run(ARGV)