Class: LastfmItunes::CLI
- Inherits:
-
Object
- Object
- LastfmItunes::CLI
- Defined in:
- lib/lastfm-itunes/cli.rb
Instance Attribute Summary collapse
-
#itunes_xml_path ⇒ Object
readonly
Returns the value of attribute itunes_xml_path.
-
#lastfm_credentials ⇒ Object
readonly
Returns the value of attribute lastfm_credentials.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#m3u_path ⇒ Object
readonly
Returns the value of attribute m3u_path.
-
#search_type ⇒ Object
readonly
Returns the value of attribute search_type.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #ask_api_key(credentials = nil) ⇒ Object
- #ask_api_secret(credentials = nil) ⇒ Object
- #ask_itunes ⇒ Object
- #ask_limit(question_text) ⇒ Object
- #ask_m3u_path(options = {}) ⇒ Object
- #ask_overwrite_m3u ⇒ Object
- #ask_username ⇒ Object
- #choose_search_type ⇒ Object
- #get_lastfm_credentials ⇒ Object
- #m3u_default_path ⇒ Object
- #progressbar(total) ⇒ Object
- #with_time(filepath) ⇒ Object
Instance Attribute Details
#itunes_xml_path ⇒ Object (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_credentials ⇒ Object (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 |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
7 8 9 |
# File 'lib/lastfm-itunes/cli.rb', line 7 def limit @limit end |
#m3u_path ⇒ Object (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_type ⇒ Object (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 |
#username ⇒ Object (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_itunes ⇒ Object
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.("~/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(={}) m3u_path = ask("Where to store generated playlist? ") do |q| q.default = .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_m3u ⇒ Object
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 || .index = :number .index_suffix = ") " .prompt = "Overwite existing playlist file? " .choice "Yes" .choice "No" end end |
#ask_username ⇒ Object
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_type ⇒ Object
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 || .index = :number .index_suffix = ") " .prompt = "Search Artist global top tracks or User top tracks? " .choice "Artist" .choice "User" end end |
#get_lastfm_credentials ⇒ Object
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.("~/.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_path ⇒ Object
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 (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 |