Class: Zhangmen::Cli
- Inherits:
-
Object
- Object
- Zhangmen::Cli
- Defined in:
- lib/zhangmen/cli.rb
Overview
Command-line interface.
Instance Method Summary collapse
- #all ⇒ Object
- #categories ⇒ Object
-
#client_cache ⇒ Object
Last snapshot of the client’s metadata cache.
-
#client_cache=(new_contents) ⇒ Object
Saves a new snapshot of the client’s metadata cache.
-
#client_cache_path ⇒ Object
Points to the client’s metadata cache file.
- #playlist(list_id) ⇒ Object
-
#run(args) ⇒ Object
Runs a command.
-
#save_client_cache ⇒ Object
Saves a new snapshot of the client’s metadata cache.
- #scan_categories ⇒ Object
Instance Method Details
#all ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/zhangmen/cli.rb', line 59 def all scan_categories do |id, playlists| puts "Category #{id}" playlists.each do |list| puts " #{list[:name]} - #{list[:id]} - #{list[:song_count]} songs" playlist list[:id] end end end |
#categories ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/zhangmen/cli.rb', line 29 def categories scan_categories do |id, playlists| puts "Category #{id}" playlists.each do |list| puts " #{list[:name]} - #{list[:id]} - #{list[:song_count]} songs" end end end |
#client_cache ⇒ Object
Last snapshot of the client’s metadata cache.
75 76 77 78 79 80 81 |
# File 'lib/zhangmen/cli.rb', line 75 def client_cache if File.exist? client_cache_path YAML.load(File.read(client_cache_path)) || {} rescue {} else {} end end |
#client_cache=(new_contents) ⇒ Object
Saves a new snapshot of the client’s metadata cache.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/zhangmen/cli.rb', line 84 def client_cache=(new_contents) begin File.open(client_cache_path, 'w') do |f| YAML.dump new_contents, f end rescue ArgumentError => e # Encoding error; bummer, can't cache # puts "#{e.class.name}: #{e}" end end |
#client_cache_path ⇒ Object
Points to the client’s metadata cache file.
70 71 72 |
# File 'lib/zhangmen/cli.rb', line 70 def client_cache_path File. '~/.zhangmen_cache' end |
#playlist(list_id) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/zhangmen/cli.rb', line 38 def playlist(list_id) songs = @client.playlist :id => list_id # save_client_cache songs.each do |song| print "#{song[:author]} - #{song[:title]} ... " bits = @client.song song if bits FileUtils.mkdir_p song[:author] filename = File.join song[:author], "#{song[:author]} - #{song[:title]}.mp3" File.open filename, 'w' do |f| f.write bits end print "ok\n" else print "FAIL\n" end end end |
#run(args) ⇒ Object
Runs a command.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/zhangmen/cli.rb', line 101 def run(args) = {} if proxy_server = ENV['http_proxy'] || ENV['all_proxy'] [:proxy] = proxy_server end @logger = Logger.new STDERR [:logger] = @logger @client = Zhangmen::Client.new @client.cache = client_cache begin case args[0] when 'list' categories when 'fetch' args[1..-1].each { |arg| playlist arg } when 'all' all end rescue Exception => e @logger.error "#{e.class.name}: #{e}\n#{e.backtrace.join("\n")}\n" end end |
#save_client_cache ⇒ Object
Saves a new snapshot of the client’s metadata cache.
96 97 98 |
# File 'lib/zhangmen/cli.rb', line 96 def save_client_cache self.client_cache = @client.cache end |
#scan_categories ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/zhangmen/cli.rb', line 10 def scan_categories category_id = 1 empty_categories = 0 loop do playlists = @client.category category_id save_client_cache if playlists.empty? empty_categories += 1 break if empty_categories == 5 else empty_categories = 0 yield category_id, playlists end category_id += 1 end category_id end |