spot_tracks 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +23 -2
- data/lib/spot_tracks/feed.rb +3 -13
- data/lib/spot_tracks/player.rb +1 -11
- data/lib/spot_tracks/track.rb +43 -0
- data/lib/spot_tracks/version.rb +1 -1
- data/lib/spot_tracks.rb +1 -0
- data/spec/fixtures/api.yml +16 -0
- data/spec/fixtures/track.yml +5 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/spot_tracks/feed_spec.rb +11 -22
- data/spec/spot_tracks/player_spec.rb +6 -24
- data/spec/spot_tracks/track_spec.rb +53 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 836a29e21cf32e9c681f3c699e3c61fd73e94445
|
4
|
+
data.tar.gz: 26685ca6cd55114faf3d774ee152b68a279d33f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff1e9f09539a926dc2a07639bb333968dc626ca9dbdf8267585f6cb686a642a82848b26c48205b90786e1101956bc087466bfe26035291c496adbf11ecaf43cf
|
7
|
+
data.tar.gz: 8e40593814c1267ac0bfe1db098bf7b9eba15126da25ad0c10b77ee02ccff9987a62767c3bfcfdd5f08a4ea38023a2463d7bbacb3d2b7afdb32c3894fbb7fc96
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# SpotTracks
|
2
2
|
|
3
|
-
|
3
|
+
SpotTracks is an easy way to get spotify play links from your recently played tracks.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,28 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
SpotTracks pulls your Last.fm RSS feed, and looks up those tracks on Spotify. It returns a json hash with your data:
|
22
|
+
|
23
|
+
SpotTracks::Feed.new('mttwrnr').to_json
|
24
|
+
#=> {
|
25
|
+
"title":"mttwrnr's Recently Played Tracks",
|
26
|
+
"url":"http://www.last.fm/user/mttwrnr",
|
27
|
+
"entries":[{
|
28
|
+
"title":"Duck Sauce – NRG - Radio Edit",
|
29
|
+
"urls":{
|
30
|
+
"lastfm":"http://www.last.fm/music/Duck+Sauce/_/NRG+-+Radio+Edit",
|
31
|
+
"spotify":"spotify:track:6MbFmc3lDtnlihb7DEabRA"
|
32
|
+
}
|
33
|
+
},{
|
34
|
+
"title":"The Magician – When The Night Is Over - Clancy Remix [feat. Newtimers]",
|
35
|
+
"urls":{
|
36
|
+
"lastfm":"http://www.last.fm/music/The+Magician/_/When+The+Night+Is+Over+-+Clancy+Remix+%5Bfeat.+Newtimers%5D",
|
37
|
+
"spotify":"spotify:track:58ScEoCoWkICs0HD7xHYKI"
|
38
|
+
}
|
39
|
+
}]
|
40
|
+
}
|
41
|
+
|
42
|
+
Enjoy!
|
22
43
|
|
23
44
|
## Contributing
|
24
45
|
|
data/lib/spot_tracks/feed.rb
CHANGED
@@ -18,15 +18,9 @@ module SpotTracks
|
|
18
18
|
client.url
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
21
|
+
def tracks
|
22
22
|
client.entries.map do |entry|
|
23
|
-
|
24
|
-
title: entry.title,
|
25
|
-
urls: {
|
26
|
-
lastfm: entry.url,
|
27
|
-
spotify: player.spotify_uri(entry.title)
|
28
|
-
}
|
29
|
-
}
|
23
|
+
SpotTracks::Track.search entry.title
|
30
24
|
end
|
31
25
|
end
|
32
26
|
|
@@ -34,7 +28,7 @@ module SpotTracks
|
|
34
28
|
{
|
35
29
|
title: title,
|
36
30
|
url: url,
|
37
|
-
|
31
|
+
tracks: tracks.map(&:to_h)
|
38
32
|
}.to_json
|
39
33
|
end
|
40
34
|
|
@@ -47,9 +41,5 @@ module SpotTracks
|
|
47
41
|
def rss_url
|
48
42
|
"#{HOST}/1.0/user/#{username}/recenttracks.rss"
|
49
43
|
end
|
50
|
-
|
51
|
-
def player
|
52
|
-
@player ||= SpotTracks::Player.new
|
53
|
-
end
|
54
44
|
end
|
55
45
|
end
|
data/lib/spot_tracks/player.rb
CHANGED
@@ -5,19 +5,9 @@ module SpotTracks
|
|
5
5
|
class Player
|
6
6
|
HOST = 'https://api.spotify.com'
|
7
7
|
|
8
|
-
def spotify_uri(title)
|
9
|
-
data = search(title)
|
10
|
-
return unless data.is_a?(Hash)
|
11
|
-
|
12
|
-
tracks = data['tracks'] || {}
|
13
|
-
items = tracks['items'] || [{}]
|
14
|
-
|
15
|
-
items.first['uri']
|
16
|
-
end
|
17
|
-
|
18
8
|
def search(term)
|
19
9
|
path = "v1/search?q=#{CGI.escape(term)}&type=track"
|
20
|
-
api_data("#{HOST}/#{path}")
|
10
|
+
SpotTracks::Track.parse api_data("#{HOST}/#{path}")
|
21
11
|
end
|
22
12
|
|
23
13
|
private
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module SpotTracks
|
2
|
+
class Track
|
3
|
+
attr_reader :name, :artist, :image, :uri
|
4
|
+
|
5
|
+
def initialize(params = {})
|
6
|
+
@name = params[:name]
|
7
|
+
@artist = params[:artist]
|
8
|
+
@image = params[:image]
|
9
|
+
@uri = params[:uri]
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.search(title)
|
13
|
+
SpotTracks::Player.new.search(title)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.parse(data = {})
|
17
|
+
tracks = data.fetch('tracks', {})
|
18
|
+
item = tracks.fetch('items', []).first
|
19
|
+
|
20
|
+
album = item['album']
|
21
|
+
artist = item['artists'].first
|
22
|
+
image = album['images'].first
|
23
|
+
|
24
|
+
new({
|
25
|
+
name: item['name'],
|
26
|
+
artist: artist['name'],
|
27
|
+
image: image['url'],
|
28
|
+
uri: item['uri']
|
29
|
+
})
|
30
|
+
end
|
31
|
+
|
32
|
+
def title
|
33
|
+
"#{artist} - #{name}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_h
|
37
|
+
{
|
38
|
+
title: title, name: name, artist: artist,
|
39
|
+
urls: { image: image, uri: uri }
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/spot_tracks/version.rb
CHANGED
data/lib/spot_tracks.rb
CHANGED
@@ -0,0 +1,16 @@
|
|
1
|
+
api:
|
2
|
+
tracks:
|
3
|
+
href: https://api.spotify.com/v1/search?query=Major+Lazer+-+Get+Free
|
4
|
+
items:
|
5
|
+
- name: Get Free
|
6
|
+
album:
|
7
|
+
name: Get Free
|
8
|
+
images:
|
9
|
+
- height: 640
|
10
|
+
width: 640
|
11
|
+
url: https://i.scdn.co/image/8c0c53664893cbce7f579b819d39d0e19d729710
|
12
|
+
artists:
|
13
|
+
- name: Major Lazer
|
14
|
+
external_urls:
|
15
|
+
uri: spotify:track:6nVEdIiB3h5b2TbQhFnQUk
|
16
|
+
|
data/spec/spec_helper.rb
CHANGED
@@ -1,25 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe SpotTracks::Feed do
|
4
|
-
let(:
|
5
|
-
let(:
|
6
|
-
let(:
|
7
|
-
{
|
8
|
-
title: 'baz',
|
9
|
-
urls: {
|
10
|
-
lastfm: 'qux',
|
11
|
-
spotify: 'spotify:track:qux'
|
12
|
-
}
|
13
|
-
}
|
14
|
-
}
|
15
|
-
let(:entry){ double(title: 'baz', url: 'qux')}
|
16
|
-
let(:player){ double(spotify_uri: 'spotify:track:qux') }
|
17
|
-
let(:client){ double(title: 'foo', url: 'http://bar', entries: [entry]) }
|
18
|
-
let(:rss_url){ "http://ws.audioscrobbler.com/1.0/user/#{user}/recenttracks.rss" }
|
4
|
+
let(:entry){ double(title: 'baz', url: 'qux') }
|
5
|
+
let(:feedjira){ double(title: 'foo', url: 'http://bar', entries: [entry]) }
|
6
|
+
let(:subject){ SpotTracks::Feed.new('mttwrnr') }
|
19
7
|
|
20
8
|
before do
|
21
|
-
|
22
|
-
|
9
|
+
param = "http://ws.audioscrobbler.com/1.0/user/mttwrnr/recenttracks.rss"
|
10
|
+
Feedjira::Feed.stub(:fetch_and_parse).with(param){ feedjira }
|
11
|
+
SpotTracks::Player.stub(:new){ double(search: {}) }
|
23
12
|
end
|
24
13
|
|
25
14
|
context 'attributes' do
|
@@ -36,15 +25,15 @@ describe SpotTracks::Feed do
|
|
36
25
|
end
|
37
26
|
end
|
38
27
|
|
39
|
-
context '#
|
40
|
-
it 'should return
|
41
|
-
subject.
|
28
|
+
context '#tracks' do
|
29
|
+
it 'should return an array of tracks' do
|
30
|
+
subject.tracks.should be_a_kind_of(Array)
|
42
31
|
end
|
43
32
|
end
|
44
33
|
|
45
34
|
context '#to_json' do
|
46
|
-
it 'should return a json version of
|
47
|
-
result = "{\"title\":\"foo\",\"url\":\"http://bar\",\"
|
35
|
+
it 'should return a json version of tracks values' do
|
36
|
+
result = "{\"title\":\"foo\",\"url\":\"http://bar\",\"tracks\":[{}]}"
|
48
37
|
subject.to_json.should eq(result)
|
49
38
|
end
|
50
39
|
end
|
@@ -1,33 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe SpotTracks::Player do
|
4
|
-
let(:json){ "{\"tracks\":{\"items\":[{\"uri\":\"spotify:track:foo\"}]}}" }
|
5
4
|
let(:subject){ SpotTracks::Player.new }
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
subject.spotify_uri('track').should match(/^spotify:.*/)
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should handle a missing uri' do
|
14
|
-
subject.stub(:get){ "{\"tracks\":{\"items\":[{}]}}" }
|
15
|
-
subject.spotify_uri('track').should be_nil
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'should handle missing items' do
|
19
|
-
subject.stub(:get){ "{\"tracks\":{}}" }
|
20
|
-
subject.spotify_uri('track').should be_nil
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'should handle missing tracks' do
|
24
|
-
subject.stub(:get){ "{}" }
|
25
|
-
subject.spotify_uri('track').should be_nil
|
26
|
-
end
|
6
|
+
before do
|
7
|
+
subject.stub(:open){ double(read: fixture(:api).to_json ) }
|
8
|
+
end
|
27
9
|
|
28
|
-
|
29
|
-
|
30
|
-
subject.
|
10
|
+
context '#search' do
|
11
|
+
it 'returns a ticket' do
|
12
|
+
subject.search('foo').should be_a_kind_of(SpotTracks::Track)
|
31
13
|
end
|
32
14
|
end
|
33
15
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SpotTracks::Track do
|
4
|
+
let(:data){ fixture(:api) }
|
5
|
+
let(:subject){ SpotTracks::Track.new({
|
6
|
+
name: 'foo', artist: 'bar',
|
7
|
+
image: 'baz', uri: 'qux'
|
8
|
+
}) }
|
9
|
+
|
10
|
+
context 'attributes' do
|
11
|
+
it 'accepts a hash of attributes' do
|
12
|
+
subject.name.should eq('foo')
|
13
|
+
subject.artist.should eq('bar')
|
14
|
+
subject.image.should eq('baz')
|
15
|
+
subject.uri.should eq('qux')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context '#search' do
|
20
|
+
it 'uses the player search' do
|
21
|
+
SpotTracks::Player.any_instance.should_receive(:search).with('bar - foo').once
|
22
|
+
SpotTracks::Track.search('bar - foo')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context '#parse' do
|
27
|
+
let(:track){ SpotTracks::Track.parse(data) }
|
28
|
+
it 'converts json data' do
|
29
|
+
track.name.should eq('Get Free')
|
30
|
+
track.artist.should eq('Major Lazer')
|
31
|
+
track.image.should eq('https://i.scdn.co/image/8c0c53664893cbce7f579b819d39d0e19d729710')
|
32
|
+
track.uri.should eq('spotify:track:6nVEdIiB3h5b2TbQhFnQUk')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context '#title' do
|
37
|
+
it 'should combine the track and artist' do
|
38
|
+
subject.title.should eq('bar - foo')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context '#to_h' do
|
43
|
+
let(:formatted){
|
44
|
+
{ title: 'bar - foo',
|
45
|
+
name: 'foo', artist: 'bar',
|
46
|
+
urls: { image: 'baz', uri: 'qux' } }
|
47
|
+
}
|
48
|
+
|
49
|
+
it 'returns the data in hash format' do
|
50
|
+
subject.to_h.should eq(formatted)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spot_tracks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Werner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|
@@ -89,6 +89,7 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
91
|
- ".rspec"
|
92
|
+
- CHANGELOG.md
|
92
93
|
- Gemfile
|
93
94
|
- LICENSE.txt
|
94
95
|
- README.md
|
@@ -97,10 +98,14 @@ files:
|
|
97
98
|
- lib/spot_tracks.rb
|
98
99
|
- lib/spot_tracks/feed.rb
|
99
100
|
- lib/spot_tracks/player.rb
|
101
|
+
- lib/spot_tracks/track.rb
|
100
102
|
- lib/spot_tracks/version.rb
|
103
|
+
- spec/fixtures/api.yml
|
104
|
+
- spec/fixtures/track.yml
|
101
105
|
- spec/spec_helper.rb
|
102
106
|
- spec/spot_tracks/feed_spec.rb
|
103
107
|
- spec/spot_tracks/player_spec.rb
|
108
|
+
- spec/spot_tracks/track_spec.rb
|
104
109
|
- spot_tracks.gemspec
|
105
110
|
homepage: ''
|
106
111
|
licenses:
|
@@ -127,6 +132,9 @@ signing_key:
|
|
127
132
|
specification_version: 4
|
128
133
|
summary: A small gem to get recent spotify tracks
|
129
134
|
test_files:
|
135
|
+
- spec/fixtures/api.yml
|
136
|
+
- spec/fixtures/track.yml
|
130
137
|
- spec/spec_helper.rb
|
131
138
|
- spec/spot_tracks/feed_spec.rb
|
132
139
|
- spec/spot_tracks/player_spec.rb
|
140
|
+
- spec/spot_tracks/track_spec.rb
|