Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Add shows command
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneLem committed Mar 18, 2013
1 parent d33fb2b commit 2ceaf9e
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -61,6 +61,31 @@ Like an old gray cat in winter, keepin' close to the wall
```

### Shows

Upcoming events are served via [last.fm/api][]

```
~ rdio current
Now playing: That Old Time Feeling / Rodney Crowell / This One's for Him: A Tribute to Guy Clark
~ rdio shows
Here are 10 upcoming events for Rodney Crowell
Toronto Canada Fri, 22 Mar 2013 20:00:00
North Bethesda, MD United States Fri, 29 Mar 2013 19:30:00
London United Kingdom Thu, 09 May 2013 20:00:00
Birmingham United Kingdom Fri, 10 May 2013 16:16:01
Belfast Ireland Sun, 12 May 2013 19:30:00
Dublin Ireland Mon, 13 May 2013 20:00:00
Brussels Belgium Mon, 20 May 2013 13:20:01
Paris France Wed, 22 May 2013 20:00:00
København C Denmark Sun, 26 May 2013 20:00:00
Berlin Germany Thu, 30 May 2013 00:38:01
~ rdio shows --artist="Johnny Cash"
No upcoming events for Johnny Cash
```

### Full usage help

```
Expand Down Expand Up @@ -96,6 +121,7 @@ COMMANDS
play - Plays the current track
previous, prev - Play previous track
quit, q - Quit Rdio
shows - Show upcoming events for an artist
snag - Add the current track or album to your collection
toggle - Toggle playback
user - Show the current Rdio user
Expand Down Expand Up @@ -125,3 +151,4 @@ Copyright (c) 2012 Wynn Netherland. See [LICENSE][] for details.
[node-rdio]: https://github.com/dstokes/rdio-cli
[GLI]: https://github.com/davetron5000/gli
[makeitpersonal.co]: http://makeitpersonal.co
[last.fm/api]: http://www.last.fm/api
52 changes: 52 additions & 0 deletions lib/rdio.rb
Expand Up @@ -46,6 +46,46 @@ def self.lyrics_for(artist, title)
return res.body
end

def self.shows_for(artist, count)
uri = URI('http://ws.audioscrobbler.com/2.0')
params = { :method => 'artist.getEvents', :artist => artist, :limit => count, :format => 'json', :autocorrect => 1, :api_key => '3c3e4b39c2aedcac5d745c70a898ee76' }
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)

json = JSON.parse(res.body)
return "Sorry, I’ve never heard of #{artist}" if json['error'] == 6

events = json['events']['event']
events = [events] if events.is_a?(Hash)
return "No upcoming events for #{artist}" if !events

corrected_artist_name = json['events']['@attr']['artist']

cities = []
countries = []
events.each do |event|
cities << event['venue']['location']['city']
countries << event['venue']['location']['country']
end

longest_city = cities.inject { |a, b| a.length > b.length ? a : b }
longest_country = countries.inject { |a, b| a.length > b.length ? a : b }

events.map! do |event|
location = event['venue']['location']

city = location['city']
city_spaces = (0..longest_city.length - city.length).map{' '}.join('')

country = location['country']
country_spaces = (0..longest_country.length - country.length).map{' '}.join('')

"#{city}#{city_spaces} #{country}#{country_spaces} #{event['startDate']} #{event['startTime']}"
end

"Here are #{count} upcoming events for #{corrected_artist_name}\n#{events.join("\n")}\n"
end

def self.rdio_config
{
:consumer_key => @consumer_key,
Expand Down Expand Up @@ -229,6 +269,18 @@ def self.add_to_collection(tracks)
end
end

skips_pre
desc 'Show upcoming events for an artist'
command :shows do |c|
c.flag :artist
c.flag :count, :default_value => 10
c.action do |global_options,options,args|
artist = options[:artist] || bridge.current_artist
count = options[:count]
say shows_for(artist, count)
end
end

### Authenticated methods

desc 'Show the current Rdio user'
Expand Down

0 comments on commit 2ceaf9e

Please sign in to comment.