Class: DubbletrackRemote::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions, Thor::RakeCompat
Defined in:
lib/dubbletrack_remote/cli.rb

Instance Method Summary collapse

Instance Method Details

#backfill(from_date = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dubbletrack_remote/cli.rb', line 142

def backfill(from_date = nil)
  ensure_setup

  from_date = from_date ? Date.parse(from_date) : Date.today - 7.days

  paths = []
  while from_date < Date.today
    paths << playlist_path_from_date(from_date)
    from_date += 1.day
  end

  paths.flatten!

  if paths.any?
    paths.each do |path|
      puts "importing #{path}"
      items = Reader.new(path, cuts_path: local_cuts_file).ingest
      items.each { |e| puts e.pretty_print }
    end
  else
    puts "could not find any files with args:"
  end
end

#configObject



53
54
55
# File 'lib/dubbletrack_remote/cli.rb', line 53

def config
  system("open #{settings_path}")
end

#consoleObject



176
177
178
179
180
181
182
183
# File 'lib/dubbletrack_remote/cli.rb', line 176

def console
  ensure_setup

  # turns on logging of SQL queries while in the task
  ActiveRecord::Base.logger = Logger.new(STDOUT)
  # starts a Ruby REPL session
  Pry.start
end

#import(*args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/dubbletrack_remote/cli.rb', line 127

def import(*args)
  ensure_setup
  paths = args_to_paths(args)
  if paths.any?
    paths.each do |path|
      puts "importing #{path}"
      items = Reader.new(path, cuts_path: local_cuts_file).ingest
      items.each { |e| puts e.pretty_print }
    end
  else
    puts "could not find any files with args:"
  end
end

#post(*args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dubbletrack_remote/cli.rb', line 75

def post(*args)
  ensure_setup
  paths = args_to_paths(args)

  if paths.any?
    items = paths.collect do |path|
      puts "reading #{path}"
      items = Reader.new(path, cuts_path: local_cuts_file).items
    end.flatten.sort_by(&:played_at)
    remaining = Item.where(id: items.pluck(:id)).remaining

    if remaining.count > 0
      puts "sending #{remaining.size} from playlist #{file_path}"
      items = Item.where(id: remaining.pluck(:id)).remaining.find_in_batches(batch_size: 25) do |items|
        client.send(items.to_a)
      end
    end
  else
    puts "could not find any files with args: #{args}"
  end
end

#read(*args) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dubbletrack_remote/cli.rb', line 108

def read(*args)
  ensure_setup
  paths = args_to_paths(args)
  if paths.any?
    items = paths.collect do |path|
      puts "reading #{path}"
      items = Reader.new(path, cuts_path: local_cuts_file).items
    end.flatten.sort_by(&:played_at)

    items.each do |e|
      puts e.pretty_print
      puts e.raw if options[:raw]
    end
  else
    puts "could not find any files with args: #{args}"
  end
end

#setupObject



58
59
60
61
# File 'lib/dubbletrack_remote/cli.rb', line 58

def setup
  create_settings_file
  ensure_setup
end

#statusObject



45
46
47
48
49
50
# File 'lib/dubbletrack_remote/cli.rb', line 45

def status
  ensure_setup
  puts "total remaining: #{Item.remaining.count}"
  puts "total transmitted: #{Item.successful.count}"
  puts "recent items needing transmission: #{Item.recent_remaining.count}"
end

#updateObject



64
65
66
67
68
69
70
71
72
# File 'lib/dubbletrack_remote/cli.rb', line 64

def update
  ensure_setup

  if todays_playlist_path
    [todays_playlist_path].flatten.each { |f| Reader.new(f, cuts_path: local_cuts_file).ingest }
  else
    puts "no file found matching pattern #{playlist_pattern}"
  end
end

#versionObject



186
187
188
189
190
# File 'lib/dubbletrack_remote/cli.rb', line 186

def version
  ensure_setup

  puts "version: #{DubbletrackRemote::VERSION}"
end

#watchObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dubbletrack_remote/cli.rb', line 17

def watch
  ensure_setup

  Reader.watch(watch_path, pattern: watch_pattern) do |file|
    puts "detected change in #{file}"
    Reader.new(file, cuts_path: local_cuts_file).ingest
  end

  Thread.new do
    loop do
      client.send(Item.next(25).to_a)
      update # this feels redundant with the watch above
      status
    rescue => e
      puts "errored, but will try to move on"
      puts e
    ensure
      sleep 10
    end
  end

  backfill

  sleep
end