Class: Henchman::Core

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

Class Method Summary collapse

Class Method Details

.cleanup(file_save_path) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/core.rb', line 111

def self.cleanup file_save_path
  File.delete file_save_path
  while File.dirname(file_save_path) != @config[:root]
    file_save_path = File.dirname(file_save_path)
    begin
      Dir.rmdir(file_save_path)
    rescue SystemCallError => msg
      break
    end
  end
end

.download_album_tracks(selection, album_tracks) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/core.rb', line 87

def self.download_album_tracks selection, album_tracks
  album_tracks.each do |album_track|
    selection[:track] = album_track
    begin
      # first download the selected track
      dropbox_path   = @dropbox.search_for selection
      file_save_path = @dropbox.download selection, dropbox_path

      # if we downloaded it, update the location of the track in iTunes
      unless !file_save_path
        updated = @appleScript.set_track_location selection, file_save_path
        # if the update failed, remove that file
        if !updated
          cleanup file_save_path
          next
        end
      end
    rescue StandardError => err
      puts err
      next
    end
  end
end

.itunes_is_active?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/core.rb', line 83

def self.itunes_is_active?
  @appleScript.get_active_app == 'iTunes'
end

.runObject



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/core.rb', line 9

def self.run
  @appleScript = Henchman::AppleScript.new

  begin
    cache_file = File.expand_path("~/.henchman/cache")
    @ignore = YAML.load_file(cache_file)
  rescue StandardError => err
    puts "Error opening cache file (#{err})"
    @ignore = Hash.new
  end
  @ignore.default = 0

  threads = []
  update_cache = false

  while itunes_is_active?
    begin
      @config = YAML.load_file(File.expand_path('~/.henchman/config'))
    rescue StandardError => err
      puts "Error opening config file. Try rerunning `henchman configure`"
      return
    end

    @appleScript.setup @config
    begin
      @dropbox = Henchman::DropboxAssistant.new @config
    rescue
      puts "Error connecting to Dropbox. Try rerunning `henchman configure`"
      return
    end

    selection = Hash.new
    track_selected = @appleScript.get_selection selection

    if track_selected && @ignore[selection[:artist]] < (Time.now.to_i - @config[:reprompt_timeout])
      update_cache = true
      @ignore.delete selection[:artist]
      if @appleScript.fetch?
        puts "searching"
        begin
          # first download the selected track
          dropbox_path   = @dropbox.search_for selection
          file_save_path = @dropbox.download selection, dropbox_path

          # if we downloaded it, update the location of the track in iTunes
          unless !file_save_path
            updated = @appleScript.set_track_location selection, file_save_path
            # if the update failed, cleanup that directory and don't bother
            # doing the rest of the album
            if !updated
              cleanup file_save_path
              next
            end

            # now that we've gotten the selected track, spawn off another process
            # to download the rest of the tracks on the album - spatial locality FTW
            album_tracks = @appleScript.get_album_tracks_of selection
            threads << Thread.new{ download_album_tracks selection, album_tracks }
          end
        rescue StandardError => err
          puts err
          next
        end
      else
        @ignore[selection[:artist]] = Time.now.to_i
      end
    end
    sleep @config[:poll_track]
  end

  threads.each { |thr| thr.join }
  File.open(cache_file, "w") { |f| f.write( @ignore.to_yaml ) } if update_cache
end