Class: Henchman::Clean

Inherits:
Object
  • Object
show all
Defined in:
lib/clean.rb

Class Method Summary collapse

Class Method Details

.cleanup(track) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/clean.rb', line 44

def self.cleanup track
  filepath = track[:path]
  begin
    File.delete filepath
    @cache.delete track
    puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
         "Deleted #{filepath}"

    while File.dirname(filepath) != @config[:root]
      filepath = File.dirname(filepath)
      Dir.rmdir(filepath)
      puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
           "Deleted #{filepath}"
    end
  rescue SystemCallError => msg
    # do nothing
  end
end

.run(played_date) ⇒ Object



9
10
11
12
13
14
15
16
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
42
# File 'lib/clean.rb', line 9

def self.run played_date
  puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
       "Cleanup Starting"

  played_date = (played_date == 'true') ? true : false

  @appleScript = Henchman::AppleScript.new
  @cache = Henchman::Cache.new
  begin
    @config = YAML.load_file(File.expand_path('~/.henchman/config'))
    @cache.config @config
    @appleScript.setup @config
  rescue StandardError => err
    puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
         "Error opening config file. Try rerunning `henchman configure`"
    return
  end

  cutoff = DateTime.now - 1

  tracks = @appleScript.get_tracks_with_location
  tracks.each do |track|
    cache_time = @cache.get_time_last_downloaded track
    if track[:date] < cutoff && ((cache_time < cutoff) || played_date)
      cleanup track
    end
  end

  @cache.flush

  puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
       "Cleanup Finished"

end