Commit a1c8f38

mo <mo.khan@gmail.com>
2018-06-14 17:16:08
add instructions on how to stream music from soundcloud.
1 parent 80fd02e
Changed files (2)
lib
ghetto
blaster
lib/ghetto/blaster/cli.rb
@@ -4,11 +4,15 @@ require 'net/hippie'
 module Ghetto
   module Blaster
     class CLI < Thor
-      class_option :client_id, default: ENV['CLIENT_ID']
+      class_option :client_id, default: ENV['SOUNDCLOUD_CLIENT_ID']
 
-      desc "play", "play music"
-      def play
-        say client.get(build_uri("/tracks?client_id=#{client_id}")).body
+      desc "play username", "play music by a specific user"
+      def play(username)
+        tracks_for(username).each do |track|
+          stream_url = "#{track[:stream_url]}s/?client_id=#{client_id}"
+          json = JSON.parse(client.get(stream_url).body, symbolize_names: true)
+          say json[json.keys.first]
+        end
       end
 
       desc "user username", "fetch user info"
@@ -18,8 +22,7 @@ module Ghetto
 
       desc "tracks username", "fetch tracks for user"
       def tracks(username)
-        user_id = resolve(username)[:location].split('?')[0].split('/').last
-        say client.get(build_uri("/users/#{user_id}/tracks?client_id=#{client_id}")).body
+        say tracks_for(username, parse: false)
       end
 
       desc "version", "print version"
@@ -29,6 +32,12 @@ module Ghetto
 
       private
 
+      def tracks_for(username, parse: true)
+        user_id = resolve(username)[:location].split('?')[0].split('/').last
+        body = client.get(build_uri("/users/#{user_id}/tracks?client_id=#{client_id}")).body
+        parse ? JSON.parse(body, symbolize_names: true) : body
+      end
+
       def client
         @client ||= Net::Hippie::Client.new
       end
README.md
@@ -1,28 +1,20 @@
 # Ghetto::Blaster
 
-Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ghetto/blaster`. To experiment with that code, run `bin/console` for an interactive prompt.
+A command line client for listening to soundcloud in your terminal.
 
-TODO: Delete this and the text above, and describe your gem
 
 ## Installation
 
-Add this line to your application's Gemfile:
-
-```ruby
-gem 'ghetto-blaster'
-```
-
-And then execute:
-
-    $ bundle
-
-Or install it yourself as:
-
     $ gem install ghetto-blaster
 
 ## Usage
 
-TODO: Write usage instructions here
+You will need to lookup your soundcloud client id. There are
+instructions on the internets on how to find this.
+
+```bash
+$ SOUNDCLOUD_CLIENT_ID=x ghetto-blaster play jodieoverland | xargs mplayer
+```
 
 ## Development
 
@@ -32,7 +24,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
 
 ## Contributing
 
-Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ghetto-blaster.
+Bug reports and pull requests are welcome on GitHub at https://github.com/mokhan/ghetto-blaster.
 
 ## License