Class: LastfmItunes::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/lastfm-itunes/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#itunes_xml_pathObject (readonly)

Returns the value of attribute itunes_xml_path.



7
8
9
# File 'lib/lastfm-itunes/cli.rb', line 7

def itunes_xml_path
  @itunes_xml_path
end

#lastfm_credentialsObject (readonly)

Returns the value of attribute lastfm_credentials.



8
9
10
# File 'lib/lastfm-itunes/cli.rb', line 8

def lastfm_credentials
  @lastfm_credentials
end

#limitObject (readonly)

Returns the value of attribute limit.



7
8
9
# File 'lib/lastfm-itunes/cli.rb', line 7

def limit
  @limit
end

#m3u_pathObject (readonly)

Returns the value of attribute m3u_path.



7
8
9
# File 'lib/lastfm-itunes/cli.rb', line 7

def m3u_path
  @m3u_path
end

#search_typeObject (readonly)

Returns the value of attribute search_type.



8
9
10
# File 'lib/lastfm-itunes/cli.rb', line 8

def search_type
  @search_type
end

#usernameObject (readonly)

Returns the value of attribute username.



8
9
10
# File 'lib/lastfm-itunes/cli.rb', line 8

def username
  @username
end

Instance Method Details

#ask_api_key(credentials = nil) ⇒ Object



44
45
46
47
48
# File 'lib/lastfm-itunes/cli.rb', line 44

def ask_api_key(credentials=nil)
  ask("What is your Lastfm API Key?  ") do |q|
    q.default = credentials['api_key'] if credentials
  end
end

#ask_api_secret(credentials = nil) ⇒ Object



50
51
52
53
54
# File 'lib/lastfm-itunes/cli.rb', line 50

def ask_api_secret(credentials=nil)
  ask("What is your Lastfm API Secret?  ") do |q|
    q.default = credentials['api_secret'] if credentials
  end
end

#ask_itunesObject



10
11
12
13
14
# File 'lib/lastfm-itunes/cli.rb', line 10

def ask_itunes
  @itunes_xml_path = ask("Where is your iTunes Library XML file?  ") do |q|
    q.default = File.expand_path("~/Music/iTunes/iTunes Music Library.xml")
  end
end

#ask_limit(question_text) ⇒ Object



68
69
70
71
72
73
# File 'lib/lastfm-itunes/cli.rb', line 68

def ask_limit(question_text)
  limit = ask(question_text) do |q|
    q.default = 'all'
  end
  @limit = limit.to_i > 0 ? limit.to_i : nil
end

#ask_m3u_path(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lastfm-itunes/cli.rb', line 16

def ask_m3u_path(options={})
  m3u_path = ask("Where to store generated playlist?  ") do |q|
    q.default = options.fetch(:m3u_path, m3u_default_path.to_s)
  end

  if File.exists?(m3u_path)
    answer = ask_overwrite_m3u
    if answer == 'No'
      m3u_path = ask_m3u_path(m3u_path: with_time(m3u_path))
    end
  end

  @m3u_path = m3u_path
end

#ask_overwrite_m3uObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lastfm-itunes/cli.rb', line 81

def ask_overwrite_m3u
  choose do |menu| 
    menu.index = :number
    menu.index_suffix = ") "

    menu.prompt = "Overwite existing playlist file?  "

    menu.choice "Yes"
    menu.choice "No"
  end
end

#ask_usernameObject



75
76
77
78
79
# File 'lib/lastfm-itunes/cli.rb', line 75

def ask_username
  @username = ask("Username?  ") do |q|
    q.validate = /\A\w+\Z/
  end
end

#choose_search_typeObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lastfm-itunes/cli.rb', line 56

def choose_search_type
  @search_type = choose do |menu| 
    menu.index = :number
    menu.index_suffix = ") "

    menu.prompt = "Search Artist global top tracks or User top tracks?  "

    menu.choice "Artist"
    menu.choice "User"
  end
end

#get_lastfm_credentialsObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lastfm-itunes/cli.rb', line 31

def get_lastfm_credentials
  puts "Find or create Lastfm API secret and key here: http://www.last.fm/api/accounts".color(:yellow)

  lastfm_yml_path = File.expand_path("~/.config/lastfm-itunes.yml")
  lastfm_credentials = if File.exists?(lastfm_yml_path)
                         YAML.load_file(lastfm_yml_path)
                       else
                         {}
                       end
  @lastfm_credentials = { api_key: ask_api_key(lastfm_credentials),
                          api_secret:    ask_api_secret(lastfm_credentials) }
end

#m3u_default_pathObject



100
101
102
# File 'lib/lastfm-itunes/cli.rb', line 100

def m3u_default_path
  Pathname(@itunes_xml_path).dirname.join('Lastfm Top Tracks.m3u')
end

#progressbar(total) ⇒ Object



93
94
95
96
97
98
# File 'lib/lastfm-itunes/cli.rb', line 93

def progressbar(total)
  ProgressBar.create(
    starting_at: 0,
    total: total,
    format: "%a".color(:cyan) + "|".color(:magenta) + "%B".color(:green) + "|".color(:magenta) +  "%p%%".color(:cyan))
end

#with_time(filepath) ⇒ Object



104
105
106
107
# File 'lib/lastfm-itunes/cli.rb', line 104

def with_time(filepath)
  time_str = Time.now.strftime("%Y-%m-%d_%H%M%S")
  filepath.gsub(/\.m3u\Z/, " #{time_str}.m3u")
end