Class: Henchman::DropboxAssistant

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DropboxAssistant

Returns a new instance of DropboxAssistant.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dropbox.rb', line 9

def initialize config
  begin
    @config = config
    @client = DropboxClient.new(@config[:dropbox][:access_token])
    true
  rescue DropboxError => msg
    puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
         "Couldn't connect to Dropbox (#{msg}). "\
         "Run `henchman stop` then `henchman configure` "\
         "to configure Dropbox connection."
    false
  end
end

Instance Method Details

#download(selection, dropbox_path) ⇒ Object



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
# File 'lib/dropbox.rb', line 23

def download selection, dropbox_path
  puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
       "Downloading #{selection.reject{|k,v| k == :path || k == :id}.values.join(':')}"
  begin
    # download the file
    content = @client.get_file(dropbox_path)

    # make sure we have the directory to put it in
    trgt_dir = File.join @config[:root], selection[:artist], selection[:album]
    system 'mkdir', '-p', trgt_dir

    # save the file
    file_save_path = File.join trgt_dir, File.basename(dropbox_path)
    open(file_save_path, 'w') {|f| f.puts content }
    file_save_path
  rescue DropboxError => msg
    puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
         "Error downloading Dropbox file #{dropbox_path}: #{msg}"
    false
  rescue StandardError => msg
    puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
         "Error saving Dropbox file #{dropbox_path} to #{trgt_dir}: #{msg}"
    false
  end
end

#search_for(selection) ⇒ Object



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
# File 'lib/dropbox.rb', line 49

def search_for selection
  puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
       "Searching for #{selection.reject{|k,v| k == :path || k == :id}.values.join(':')}"

  # search Dropbox for the file
  begin
    results = @client.search(@config[:dropbox][:root], selection[:track])
  rescue DropboxError => msg
    raise "Error accessing Dropbox Search API on #{selection.reject{|k,v| k == :path || k == :id}.values.join(':')}: #{msg}"
  end

  # get rid of any results that are directories
  results.reject! { |result| result['is_dir'] }

  # if we still don't have any results, try dropping any brackets and paranthesis
  if results.empty? && (selection[:track].match(%r( *\[.*\] *)) || selection[:track].match(%r( *\(.*\) *)))
    track = selection[:track].gsub(%r( *\[.*\] *), " ").gsub(%r( *\(.*\) *), " ")
    results = @client.search(@config[:dropbox][:root], track)
    results.reject! { |result| result['is_dir'] }
  end

  # if there were no results, raise err
  if results.empty?
    raise "Track not found in Dropbox: #{selection.reject{|k,v| k == :id}.values.join(':')}"

  # if there's only one result, return it
  elsif results.length == 1
    results[0]['path']

  # if there are multiple results, score them based on artist + album
  else
    scores = Hash.new 0
    results.each do |result|
      [:artist, :album].each do |identifier|
        tokens = selection[identifier].downcase
                                      .gsub(%r( +), " ")
                                      .gsub(%r(-+), "-")
                                      .strip
                                      .split(/[\s-]/)
        tokens.each do |token|
          scores[result['path']] += 1 if result['path'].downcase.include? token
        end
      end
    end

    # return the path that has the highest score
    (scores.sort_by { |path, score| score })[-1][0]
  end
end