Class: Henchman::Core

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

Class Method Summary collapse

Class Method Details

.cleanup(file_save_path, track) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/core.rb', line 164

def self.cleanup file_save_path, track
  File.delete file_save_path
  @cache.delete track
  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_and_update(track) ⇒ Object



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

def self.download_and_update track
  begin
    # first download the selected track
    dropbox_path   = @dropbox.search_for track
    file_save_path = @dropbox.download track, dropbox_path
    @cache.tag track

    # if we downloaded it, update the location of the track in iTunes
    unless !file_save_path
      updated = @appleScript.set_track_location track, file_save_path

      # if the update failed, remove that file
      if !updated
        cleanup file_save_path, track
        raise "Could not update location of #{track.reject{|k,v| k == :path || k == :id}.values.join(':')} to #{file_save_path}"
      end
    end
  rescue StandardError => err
    puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|#{err}"
    raise track[:id]
  end
end

.download_playlist(playlist, playlist_tracks) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/core.rb', line 121

def self.download_playlist playlist, playlist_tracks
  puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|Downloading Playlist \"#{playlist}\""
  skip = Array.new
  while true
    download_tracks playlist_tracks, skip
    playlist_tracks = @appleScript.get_playlist_tracks playlist, skip
    break if playlist_tracks.empty?
  end
end

.download_tracks(album_tracks, skip = []) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/core.rb', line 131

def self.download_tracks album_tracks, skip = []
  album_tracks.each do |album_track|
    begin
      download_and_update album_track
    rescue StandardError => bad_id
      skip.push bad_id
    end
  end
end

.itunes_is_active?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/core.rb', line 117

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

.missing_track_selected?(track) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/core.rb', line 113

def self.missing_track_selected? track
  track[:path] == '/missing value'
end

.run(args) ⇒ Object



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/core.rb', line 10

def self.run args
  debug = true if args[0] == 'debug'

  @appleScript = Henchman::AppleScript.new
  @cache = Henchman::Cache.new
  @update_cache = false
  threads = []

  while itunes_is_active?
    begin
      config_file = File.expand_path('~/.henchman/config')
      @config = YAML.load_file(config_file)

      # add this for backward compatability:
      if !(@config.include? :delimiter_major)
        @config[:delimiter_major] = Henchman::Templates.config[:delimiter_major]
        File.open(config_file, "w") { |f| f.write( @config.to_yaml ) }
      end
      if !(@config.include? :file_extensions)
        @config[:file_extensions] = Henchman::Templates.config[:file_extensions]
        File.open(config_file, "w") { |f| f.write( @config.to_yaml ) }
      end

      @cache.config @config
    rescue StandardError => err
      puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
           "Error opening config file. Try rerunning `henchman configure`. (#{err})"
      return
    end

    @appleScript.setup @config
    begin
      @dropbox = Henchman::DropboxAssistant.new @config, debug
    rescue StandardError => msg
      puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
           "Error connecting to Dropbox (#{msg}). Try rerunning `henchman configure`"
      return
    end

    track = @appleScript.get_selection

    if track_selected? track
      if (missing_track_selected? track) && !(@cache.ignore? :artist, track[:artist])
        @update_cache = true
        @cache.update_ignore :artist, track[:artist]

        opts = ['Album', 'Track']
        fetch = @appleScript.fetch? opts
        if opts.include? fetch
          begin
            # first download the selected track
            dropbox_path   = @dropbox.search_for track
            file_save_path = @dropbox.download track, dropbox_path
            @cache.tag track

            # if we downloaded it, update the location of the track in iTunes
            unless !file_save_path
              updated = @appleScript.set_track_location track, 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, track
                next
              end

              # now that we've gotten the selected track, if we're to also get the
              # album, spawn off another process to download the rest of the tracks
              # on the album - spatial locality FTW
              if fetch == 'Album'
                album_tracks = @appleScript.get_album_tracks_of track
                threads << Thread.new{ download_tracks album_tracks }
              end
            end
          rescue StandardError => err
            puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|#{err}"
            next
          end
        end
      end
    else
      playlist = @appleScript.get_playlist
      if playlist && !(@cache.ignore? :playlist, playlist)
        playlist_tracks = @appleScript.get_playlist_tracks playlist
        if !playlist_tracks.empty?
          @update_cache = true
          @cache.update_ignore :playlist, playlist
          if @appleScript.fetch?([playlist]) == playlist
            threads << Thread.new{ download_playlist playlist, playlist_tracks }
          end
        end
      end
    end
    sleep @config[:poll_track]
  end

  threads.each { |thr| thr.join }
  @cache.flush if @update_cache
end

.track_selected?(track) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/core.rb', line 109

def self.track_selected? track
  !track.empty?
end