# encoding: utf-8 module TokiCLI class App < Thor package_name "TokiCLI" %w{get_messages search_messages get_channels module status view authorize}.each {|r| require_relative "#{r}"} desc "scan", "Scan /Applications to resolve app names" def scan toki = create_toki(options) bundle_ids = toki.get_bundle_ids path = "#{Dir.home}/.toki_cli" Dir.mkdir path unless Dir.exist? path f = File.new "#{path}/apps.json", "w" f.write bundle_ids.to_json f.close puts "\nDone.\n\n" end desc "total", "Shows the total usage of all apps" option :adn, aliases: '-a', type: :boolean, desc: 'Select ADN channel as source (toki total -a)' option :json, aliases: '-j', type: :boolean, desc: 'Export the results as a JSON file' option :csv, aliases: '-c', type: :boolean, desc: 'Export the results as a CSV file' def total toki = create_toki(options) apps = get_total(toki, options) clear View.new.total_table(apps) export(options, apps, "toki-total-#{Time.now.to_s[0..9]}") if (options[:json] || options[:csv]) end desc "top", "Shows your most used apps" option :adn, aliases: '-a', type: :boolean, desc: 'Select ADN channel as source (toki top -a)' option :number, aliases: '-n', type: :numeric, desc: 'Specify the number of apps (toki top -n 10)' option :json, aliases: '-j', type: :boolean, desc: 'Export the results as a JSON file' option :csv, aliases: '-c', type: :boolean, desc: 'Export the results as a CSV file' def top toki, entries = init(options) clear hits = toki.top(entries, options[:number] || 5) View.new.hits_table(hits.reverse) export(options, hits, "toki-top-#{Time.now.to_s[0..9]}") if (options[:json] || options[:csv]) end desc "auth", "Input your App.net token for authorization" def auth begin ADNAuthorize::Authorize.new.authorize rescue Interrupt puts Status.canceled exit end end desc "log APP", "Shows the complete log for an app" option :adn, aliases: '-a', type: :boolean, desc: 'Select ADN channel as source (toki log -a)' option :json, aliases: '-j', type: :boolean, desc: 'Export the results as a JSON file' option :csv, aliases: '-c', type: :boolean, desc: 'Export the results as a CSV file' def log(*args) abort(Status.specify_name) if args.empty? asked = args[0] toki, entries = init(options) app_data = toki.get_app(asked, entries) View.new.app_table(asked, app_data) export(options, app_data, "toki-log-#{asked}-#{Time.now.to_s[0..9]}") if (options[:json] || options[:csv]) end desc "day DATE", "Shows all apps used on a specific day (toki day 2014-04-19)" option :adn, aliases: '-a', type: :boolean, desc: 'Select ADN channel as source (toki day 2014-04-19 -a)' option :json, aliases: '-j', type: :boolean, desc: 'Export the results as a JSON file' option :csv, aliases: '-c', type: :boolean, desc: 'Export the results as a CSV file' def day(*args) abort(Status.specify_day) if args.empty? begin date = DateTime.strptime(args[0], '%Y-%m-%d') rescue ArgumentError abort(Status.specify_day) end toki, entries = init(options) day_data = toki.get_day(date, entries, options) clear View.new.day_table(args[0], day_data) export(options, day_data, "toki-day-#{args[0]}") if (options[:json] || options[:csv]) end desc "range DAY1 DAY2", "Shows all apps used between day 1 and day 2 (toki range 2014-04-16 2014-04-18)" option :adn, aliases: '-a', type: :boolean, desc: 'Select ADN channel as source (toki range 2014-04-16 2014-04-18 -a)' option :json, aliases: '-j', type: :boolean, desc: 'Export the results as a JSON file' option :csv, aliases: '-c', type: :boolean, desc: 'Export the results as a CSV file' def range(*args) abort(Status.specify_range) if args.empty? || args.length != 2 begin day1 = DateTime.strptime(args[0], '%Y-%m-%d') day2 = DateTime.strptime(args[1], '%Y-%m-%d') rescue ArgumentError abort(Status.specify_range) end toki, entries = init(options) range_data = toki.get_range(day1, day2, entries, options) clear View.new.range_table(args[0], args[1], range_data) export(options, range_data, "toki-range-#{args[0]}_#{args[1]}") if (options[:json] || options[:csv]) end private def export(options, data, filename) case when options[:json] name = Dir.home + "/temp/toki/#{filename}.json" File.write(name, data.to_h.to_json) when options[:csv] name = Dir.home + "/temp/toki/#{filename}.csv" CSV.open(name, "wb") do |csv| data.to_a.each {|line| csv << line} end else exit end puts "File converted and exported to #{name}\n\n" end def init(options) toki = create_toki(options) return toki, toki.get_content(options) end def create_toki(options) options[:adn] ? total_adn : total_db end def total_adn clear puts Status.get_all init_toki_adn end def total_db clear #puts "getting data from db\n" init_toki_db end def get_total(toki, options) begin get_all(toki, options) rescue Interrupt abort Status.canceled rescue => e abort Status.error(e) end end def init_toki_db TokiCLI::Toki.new(nil, nil) end def init_toki_adn(channel_id = get_channel_id) TokiCLI::Toki.new("#{get_token}", channel_id) end def get_token filepath = Dir.home + '/.TokiCLI/config.json' if File.exist?(filepath) content = JSON.parse(File.read(filepath)) content['token'] else clear abort Status.run_auth end end def get_channel_id #TODO return ch['counts']['messages'] too channels = ADNChannels::GetChannels.new(get_token).get_channels channel_id = '' channels.each do |ch| if ch['type'] == 'us.kkob.toki.sync-b' channel_id = ch['id'] break end end return channel_id.to_i unless channel_id == '' abort Status.no_channel end def clear puts "\e[H\e[2J" end def get_all(t, options) content = t.get_content(options) t.all_data(content) end end end