itunes_api 1.4.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -2
- data/.rspec +3 -1
- data/Gemfile +0 -1
- data/LICENSE.txt +1 -1
- data/README.md +17 -14
- data/Rakefile +0 -1
- data/bin/console +1 -8
- data/bin/setup +1 -2
- data/itunes_api.gemspec +0 -2
- data/lib/itunes_api/music/album.rb +25 -32
- data/lib/itunes_api/music/artist.rb +23 -9
- data/lib/itunes_api/music/requests/albums.rb +41 -0
- data/lib/itunes_api/music/requests/artist.rb +32 -0
- data/lib/itunes_api/music/requests/search.rb +39 -0
- data/lib/itunes_api/music/requests/songs.rb +42 -0
- data/lib/itunes_api/music/requests.rb +12 -0
- data/lib/itunes_api/music/results/album.rb +35 -0
- data/lib/itunes_api/music/results/artist.rb +33 -0
- data/lib/itunes_api/music/results/song.rb +39 -0
- data/lib/itunes_api/music/results.rb +11 -0
- data/lib/itunes_api/music/song.rb +18 -0
- data/lib/itunes_api/music.rb +35 -0
- data/lib/itunes_api/request.rb +24 -0
- data/lib/itunes_api/version.rb +1 -1
- data/lib/itunes_api.rb +1 -24
- metadata +14 -40
- data/lib/itunes_api/music/album_lookup.rb +0 -21
- data/lib/itunes_api/music/all.rb +0 -6
- data/lib/itunes_api/music/artist_lookup.rb +0 -25
- data/lib/itunes_api/music/artist_search.rb +0 -31
- data/lib/itunes_api/music/track.rb +0 -46
- data/lib/itunes_api/requests/album_tracklist.rb +0 -36
- data/lib/itunes_api/requests/all.rb +0 -4
- data/lib/itunes_api/requests/base.rb +0 -37
- data/lib/itunes_api/requests/lookup.rb +0 -54
- data/lib/itunes_api/requests/search.rb +0 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b59950a6d1529d61e387ac0ef1306324959bc43d
|
4
|
+
data.tar.gz: d5bb63b84da411992c53a7178224e34023d2f02a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4cabff1c9f4a1d757231f46d1623ed2d5506efee42e289ae92e63aa979bff1a3a6944e8170c885d468d2a0f314715192b5108385609d8a91e50317b0b802ee33
|
7
|
+
data.tar.gz: 63d044a01c39946370f2133a0190a0fad390db937fca70d43ac4b1885134154bff678ddccb489906602144e3dea5b66815b87a39115af2f795c904a9236d9ec6
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ItunesApi
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/itunes_api.svg)](https://badge.fury.io/rb/itunes_api) [![Dependency Status](https://gemnasium.com/badges/github.com/mariodarco/itunes-api.svg)](https://gemnasium.com/github.com/mariodarco/itunes-api) [![CircleCI](https://circleci.com/gh/mariodarco/itunes-api/tree/master.svg?style=shield)](https://circleci.com/gh/mariodarco/itunes-api/tree/master)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/itunes_api.svg)](https://badge.fury.io/rb/itunes_api) [![Dependency Status](https://gemnasium.com/badges/github.com/mariodarco/itunes-api.svg)](https://gemnasium.com/github.com/mariodarco/itunes-api) [![CircleCI](https://circleci.com/gh/mariodarco/itunes-api/tree/master.svg?style=shield)](https://circleci.com/gh/mariodarco/itunes-api/tree/master)
|
4
4
|
|
5
5
|
A simple interface for the Itunes Api.
|
6
6
|
|
@@ -37,37 +37,40 @@ end
|
|
37
37
|
|
38
38
|
## Usage
|
39
39
|
|
40
|
-
To return all the Apple ids associated with the search for a Artist name
|
40
|
+
To return all the Apple ids associated with the search for a Artist name
|
41
41
|
|
42
42
|
```ruby
|
43
|
-
ItunesApi.
|
43
|
+
ItunesApi::Music.all_apple_ids('Pink Floyd')
|
44
44
|
```
|
45
45
|
|
46
|
-
To get all artists returning from a search term
|
46
|
+
To get all artists returning from a search term
|
47
47
|
|
48
48
|
```ruby
|
49
|
-
ItunesApi.
|
49
|
+
ItunesApi::Music.all_artists_by_name('Led Zeppelin')
|
50
50
|
```
|
51
51
|
|
52
|
-
To return a specific artist
|
52
|
+
To return a specific artist, based on the artist's Apple id
|
53
53
|
|
54
54
|
```ruby
|
55
|
-
ItunesApi.
|
55
|
+
ItunesApi::Music.find_by_apple_id(265766061)
|
56
56
|
```
|
57
57
|
|
58
|
-
|
58
|
+
Note that each request that returns artists does not automatically return albums.
|
59
|
+
They are lazily fetched when calling artist.albums.
|
60
|
+
|
61
|
+
To return only the albums for a specific artist, based on the artist's Apple id
|
59
62
|
|
60
63
|
```ruby
|
61
|
-
ItunesApi.
|
64
|
+
ItunesApi::Music.albums_for_artist(265766061)
|
62
65
|
```
|
63
66
|
|
64
|
-
An argument holding the country code can be used to look into different stores than the default one
|
67
|
+
An argument holding the country code can be used to look into different stores than the default one
|
65
68
|
|
66
69
|
```ruby
|
67
|
-
ItunesApi.
|
68
|
-
ItunesApi.
|
69
|
-
ItunesApi.
|
70
|
-
ItunesApi.
|
70
|
+
ItunesApi::Music.all_apple_ids('Pink Floyd', :it)
|
71
|
+
ItunesApi::Music.all_artists_by_name('Led Zeppelin', :fr)
|
72
|
+
ItunesApi::Music.find_by_apple_id(265766061, :us)
|
73
|
+
ItunesApi::Music.albums_for_artist(265766061, :es)
|
71
74
|
```
|
72
75
|
|
73
76
|
## Development
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -2,13 +2,6 @@
|
|
2
2
|
|
3
3
|
require 'bundler/setup'
|
4
4
|
require 'itunes_api'
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
5
|
require 'irb'
|
6
|
+
|
14
7
|
IRB.start
|
data/bin/setup
CHANGED
data/itunes_api.gemspec
CHANGED
@@ -27,10 +27,8 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_dependency 'selfies'
|
28
28
|
|
29
29
|
spec.add_development_dependency 'bundler'
|
30
|
-
spec.add_development_dependency 'coveralls'
|
31
30
|
spec.add_development_dependency 'rake'
|
32
31
|
spec.add_development_dependency 'rspec'
|
33
|
-
spec.add_development_dependency 'simplecov'
|
34
32
|
spec.add_development_dependency 'rubocop'
|
35
33
|
spec.add_development_dependency 'vcr'
|
36
34
|
end
|
@@ -3,64 +3,57 @@ module ItunesApi
|
|
3
3
|
module Music
|
4
4
|
# Wrapper for album results.
|
5
5
|
class Album
|
6
|
-
attr_reader_init :
|
7
|
-
private :data
|
6
|
+
attr_reader_init :artwork, :collection_id, :genre, :name, :release_on, :store
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
.reverse
|
13
|
-
end
|
14
|
-
|
15
|
-
def artwork
|
16
|
-
@artwork ||= data[:artworkUrl100]
|
17
|
-
end
|
18
|
-
|
19
|
-
def availability
|
20
|
-
prefix = pre_release? ? 'pre_release_' : ''
|
21
|
-
|
22
|
-
if apple_music?
|
23
|
-
"#{prefix}streaming".to_sym
|
24
|
-
else
|
25
|
-
"#{prefix}sale".to_sym
|
8
|
+
class << self
|
9
|
+
def for_artist(apple_id, store)
|
10
|
+
albums(apple_id, store).map { |album| new(*album.attributes) }
|
26
11
|
end
|
27
|
-
end
|
28
12
|
|
29
|
-
|
30
|
-
@collection_id ||= data[:collectionId]
|
31
|
-
end
|
13
|
+
private
|
32
14
|
|
33
|
-
|
34
|
-
|
15
|
+
def albums(apple_id, store)
|
16
|
+
Requests::Albums.find_by_apple_id(apple_id, store)
|
17
|
+
end
|
35
18
|
end
|
36
19
|
|
37
|
-
def
|
38
|
-
@
|
20
|
+
def availability
|
21
|
+
@availability ||= build_availability
|
39
22
|
end
|
40
23
|
|
41
24
|
def to_hash
|
42
25
|
{
|
43
26
|
artwork: artwork,
|
27
|
+
availability: availability,
|
44
28
|
collection_id: collection_id,
|
45
29
|
name: name,
|
46
|
-
|
47
|
-
availability: availability,
|
30
|
+
release_on: release_on,
|
48
31
|
store: store
|
49
32
|
}
|
50
33
|
end
|
51
34
|
|
35
|
+
def tracklist
|
36
|
+
@tracklist ||= songs.map { |song| Song.new(*song.attributes) }
|
37
|
+
end
|
38
|
+
|
52
39
|
private
|
53
40
|
|
54
41
|
def apple_music?
|
55
42
|
tracklist.any?(&:streamable?)
|
56
43
|
end
|
57
44
|
|
45
|
+
def build_availability
|
46
|
+
prefix = pre_release? ? 'pre_release_' : ''
|
47
|
+
suffix = apple_music? ? 'streaming' : 'sale'
|
48
|
+
"#{prefix}#{suffix}".to_sym
|
49
|
+
end
|
50
|
+
|
58
51
|
def pre_release?
|
59
|
-
|
52
|
+
release_on > Date.today
|
60
53
|
end
|
61
54
|
|
62
|
-
def
|
63
|
-
@
|
55
|
+
def songs
|
56
|
+
@songs ||= Requests::Songs.find_by_collection_id(collection_id, store)
|
64
57
|
end
|
65
58
|
end
|
66
59
|
end
|
@@ -2,19 +2,33 @@ module ItunesApi
|
|
2
2
|
module Music
|
3
3
|
# Artist or Band from the Apple catalog
|
4
4
|
class Artist
|
5
|
-
attr_reader_init :
|
6
|
-
private :data
|
5
|
+
attr_reader_init :apple_id, :genre, :name, :store
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
class << self
|
8
|
+
def all_apple_ids(name, store)
|
9
|
+
find_all_by_name(name, store).map(&:apple_id)
|
10
|
+
end
|
11
|
+
|
12
|
+
def find_all_by_name(name, store)
|
13
|
+
Requests::Search.artists(name, store).map do |result|
|
14
|
+
new(*result.attributes)
|
15
|
+
end
|
16
|
+
end
|
11
17
|
|
12
|
-
|
13
|
-
|
18
|
+
def find_by_apple_id(apple_id, store)
|
19
|
+
artist = artist(apple_id, store)
|
20
|
+
artist ? new(*artist.attributes) : nil
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def artist(apple_id, store)
|
26
|
+
Requests::Artist.find_by_apple_id(apple_id, store)
|
27
|
+
end
|
14
28
|
end
|
15
29
|
|
16
|
-
def
|
17
|
-
@
|
30
|
+
def albums
|
31
|
+
@albums ||= Album.for_artist(apple_id, store)
|
18
32
|
end
|
19
33
|
|
20
34
|
def to_hash
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module ItunesApi
|
2
|
+
module Music
|
3
|
+
module Requests
|
4
|
+
# Allows querying the API via lookup for albums.
|
5
|
+
class Albums
|
6
|
+
include Request
|
7
|
+
attr_reader_init :apple_id, :store
|
8
|
+
selfie :find_by_apple_id
|
9
|
+
|
10
|
+
def find_by_apple_id
|
11
|
+
unwrapped_results.map do |result|
|
12
|
+
Results::Album.new(result, store)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def action
|
19
|
+
'lookup'
|
20
|
+
end
|
21
|
+
|
22
|
+
def query
|
23
|
+
{
|
24
|
+
id: apple_id,
|
25
|
+
country: store.to_s.upcase,
|
26
|
+
entity: :album,
|
27
|
+
limit: LIMIT
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def unwrapped_results
|
32
|
+
return [] unless results.any?
|
33
|
+
|
34
|
+
results.find_all do |wrappers|
|
35
|
+
wrappers['wrapperType'] == 'collection'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module ItunesApi
|
2
|
+
module Music
|
3
|
+
module Requests
|
4
|
+
# Allows querying the API via lookup for artists.
|
5
|
+
class Artist
|
6
|
+
include Request
|
7
|
+
attr_reader_init :apple_id, :store
|
8
|
+
selfie :find_by_apple_id
|
9
|
+
|
10
|
+
def find_by_apple_id
|
11
|
+
return nil unless results.any?
|
12
|
+
|
13
|
+
Results::Artist.new(results.first, store)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def action
|
19
|
+
'lookup'
|
20
|
+
end
|
21
|
+
|
22
|
+
def query
|
23
|
+
{
|
24
|
+
id: apple_id,
|
25
|
+
country: store.to_s.upcase,
|
26
|
+
limit: LIMIT
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module ItunesApi
|
2
|
+
module Music
|
3
|
+
module Requests
|
4
|
+
# Allows querying the API via search for artists.
|
5
|
+
class Search
|
6
|
+
include Request
|
7
|
+
attr_reader_init :artist_name, :store
|
8
|
+
selfie :artists
|
9
|
+
|
10
|
+
def artists
|
11
|
+
@artists ||= decorated_results.find_all(&:relevant?)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def action
|
17
|
+
'search'
|
18
|
+
end
|
19
|
+
|
20
|
+
def decorated_results
|
21
|
+
results.map do |result|
|
22
|
+
Results::Artist.new(result, store, artist_name)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def query
|
27
|
+
{
|
28
|
+
attribute: 'artistTerm',
|
29
|
+
country: store.to_s.upcase,
|
30
|
+
entity: 'musicArtist',
|
31
|
+
limit: LIMIT,
|
32
|
+
media: 'music',
|
33
|
+
term: artist_name
|
34
|
+
}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module ItunesApi
|
2
|
+
module Music
|
3
|
+
module Requests
|
4
|
+
# Allows querying the API via lookup for album songs.
|
5
|
+
class Songs
|
6
|
+
include Request
|
7
|
+
attr_reader_init :collection_id, :store
|
8
|
+
selfie :find_by_collection_id
|
9
|
+
|
10
|
+
def find_by_collection_id
|
11
|
+
unwrapped_results.map do |result|
|
12
|
+
Results::Song.new(result)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def action
|
19
|
+
'lookup'
|
20
|
+
end
|
21
|
+
|
22
|
+
def query
|
23
|
+
{
|
24
|
+
entity: 'song',
|
25
|
+
id: collection_id,
|
26
|
+
country: store.to_s.upcase,
|
27
|
+
limit: LIMIT,
|
28
|
+
sort: 'trackNumber'
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
def unwrapped_results
|
33
|
+
return [] unless results.any?
|
34
|
+
|
35
|
+
results.find_all do |wrappers|
|
36
|
+
wrappers['wrapperType'] == 'track'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative 'requests/albums'
|
2
|
+
require_relative 'requests/artist'
|
3
|
+
require_relative 'requests/search'
|
4
|
+
require_relative 'requests/songs'
|
5
|
+
|
6
|
+
module ItunesApi
|
7
|
+
module Music
|
8
|
+
# Starting module for request wrappers
|
9
|
+
module Requests
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ItunesApi
|
2
|
+
module Music
|
3
|
+
module Results
|
4
|
+
# Wrapper for album search results.
|
5
|
+
class Album
|
6
|
+
attr_reader_init :data, :store
|
7
|
+
private :data
|
8
|
+
|
9
|
+
def artwork
|
10
|
+
@artwork ||= data['artworkUrl100']
|
11
|
+
end
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
[artwork, collection_id, genre, name, release_on, store]
|
15
|
+
end
|
16
|
+
|
17
|
+
def collection_id
|
18
|
+
@collection_id ||= data['collectionId']
|
19
|
+
end
|
20
|
+
|
21
|
+
def genre
|
22
|
+
@genre ||= data['primaryGenreName']
|
23
|
+
end
|
24
|
+
|
25
|
+
def name
|
26
|
+
@name ||= data['collectionName']
|
27
|
+
end
|
28
|
+
|
29
|
+
def release_on
|
30
|
+
@release_on ||= Date.parse(data['releaseDate'])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ItunesApi
|
2
|
+
module Music
|
3
|
+
module Results
|
4
|
+
# Wrapper for artist search results.
|
5
|
+
class Artist
|
6
|
+
attr_reader_init :data, :store, search_term: nil
|
7
|
+
private :data, :search_term
|
8
|
+
|
9
|
+
def apple_id
|
10
|
+
@apple_id ||= data['artistId']
|
11
|
+
end
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
[apple_id, genre, name, store]
|
15
|
+
end
|
16
|
+
|
17
|
+
def genre
|
18
|
+
@genre ||= data['primaryGenreName']
|
19
|
+
end
|
20
|
+
|
21
|
+
def name
|
22
|
+
@name ||= data['artistName']
|
23
|
+
end
|
24
|
+
|
25
|
+
def relevant?
|
26
|
+
return true unless search_term
|
27
|
+
|
28
|
+
name.downcase.include?(search_term.downcase)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module ItunesApi
|
2
|
+
module Music
|
3
|
+
module Results
|
4
|
+
# Wrapper for song search results.
|
5
|
+
class Song
|
6
|
+
attr_reader_init :data
|
7
|
+
private :data
|
8
|
+
|
9
|
+
def duration
|
10
|
+
@duration ||= track_lenght(data['trackTimeMillis'])
|
11
|
+
end
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
[duration, name, number, streamable]
|
15
|
+
end
|
16
|
+
|
17
|
+
def name
|
18
|
+
@name ||= data['trackName']
|
19
|
+
end
|
20
|
+
|
21
|
+
def number
|
22
|
+
@number ||= data['trackNumber']
|
23
|
+
end
|
24
|
+
|
25
|
+
def streamable
|
26
|
+
@streamable ||= data['isStreamable']
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def track_lenght(milliseconds)
|
32
|
+
minutes, milliseconds = milliseconds.divmod(1000 * 60)
|
33
|
+
seconds = milliseconds / 1000
|
34
|
+
"#{minutes}:#{seconds}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ItunesApi
|
2
|
+
module Music
|
3
|
+
# Wrapper for song results.
|
4
|
+
class Song
|
5
|
+
attr_reader_init :duration, :name, :number, :streamable
|
6
|
+
alias streamable? streamable
|
7
|
+
|
8
|
+
def to_hash
|
9
|
+
{
|
10
|
+
duration: duration,
|
11
|
+
name: name,
|
12
|
+
number: number,
|
13
|
+
stremable: streamable?,
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative 'request'
|
2
|
+
require_relative 'music/requests'
|
3
|
+
require_relative 'music/results'
|
4
|
+
require_relative 'music/album'
|
5
|
+
require_relative 'music/artist'
|
6
|
+
require_relative 'music/song'
|
7
|
+
|
8
|
+
module ItunesApi
|
9
|
+
# Public interface for the Music Api
|
10
|
+
module Music
|
11
|
+
class << self
|
12
|
+
def all_apple_ids(name, store = default_store)
|
13
|
+
Artist.all_apple_ids(name, store)
|
14
|
+
end
|
15
|
+
|
16
|
+
def all_artists_by_name(name, store = default_store)
|
17
|
+
Artist.find_all_by_name(name, store)
|
18
|
+
end
|
19
|
+
|
20
|
+
def find_by_apple_id(apple_id, store = default_store)
|
21
|
+
Music::Artist.find_by_apple_id(apple_id, store)
|
22
|
+
end
|
23
|
+
|
24
|
+
def albums_for_artist(apple_id, store = default_store)
|
25
|
+
Music::Album.for_artist(apple_id, store)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def default_store
|
31
|
+
ItunesApi::Configuration.instance.country_code || :gb
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'json'
|
3
|
+
module ItunesApi
|
4
|
+
# Allow requests to the iTunes API.
|
5
|
+
module Request
|
6
|
+
def results
|
7
|
+
@results ||= parsed_response.fetch('results') { [] }
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def parsed_response
|
13
|
+
JSON.parse(response.body)
|
14
|
+
end
|
15
|
+
|
16
|
+
def connection
|
17
|
+
Faraday.new(url: BASE_URL)
|
18
|
+
end
|
19
|
+
|
20
|
+
def response
|
21
|
+
connection.get(action, query)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/itunes_api/version.rb
CHANGED
data/lib/itunes_api.rb
CHANGED
@@ -1,36 +1,13 @@
|
|
1
1
|
require 'selfies'
|
2
2
|
require 'itunes_api/configuration'
|
3
|
-
require 'itunes_api/
|
4
|
-
require 'itunes_api/music/all'
|
3
|
+
require 'itunes_api/music'
|
5
4
|
|
6
5
|
# Interface to the Itunes Api
|
7
6
|
module ItunesApi
|
8
7
|
BASE_URL = 'https://itunes.apple.com'.freeze
|
9
8
|
LIMIT = 200
|
10
9
|
|
11
|
-
def self.artist_ids(name, store = default_store)
|
12
|
-
Requests::Search.artist_ids(name, store)
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.album_lookup(album_id, store = default_store)
|
16
|
-
Music::AlbumLookup.tracklist(album_id, store)
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.artist_lookup(artist_id, store = default_store)
|
20
|
-
Music::ArtistLookup.artist(artist_id, store)
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.artist_search(name, store = default_store)
|
24
|
-
Music::ArtistSearch.artists(name, store)
|
25
|
-
end
|
26
|
-
|
27
10
|
def self.configure
|
28
11
|
yield(Configuration.instance)
|
29
12
|
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
def self.default_store
|
34
|
-
Configuration.instance.country_code rescue :gb
|
35
|
-
end
|
36
13
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: itunes_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mario D’Arco
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: coveralls
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: rake
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,20 +80,6 @@ dependencies:
|
|
94
80
|
- - ">="
|
95
81
|
- !ruby/object:Gem::Version
|
96
82
|
version: '0'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: simplecov
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">="
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
111
83
|
- !ruby/object:Gem::Dependency
|
112
84
|
name: rubocop
|
113
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -155,18 +127,20 @@ files:
|
|
155
127
|
- itunes_api.gemspec
|
156
128
|
- lib/itunes_api.rb
|
157
129
|
- lib/itunes_api/configuration.rb
|
130
|
+
- lib/itunes_api/music.rb
|
158
131
|
- lib/itunes_api/music/album.rb
|
159
|
-
- lib/itunes_api/music/album_lookup.rb
|
160
|
-
- lib/itunes_api/music/all.rb
|
161
132
|
- lib/itunes_api/music/artist.rb
|
162
|
-
- lib/itunes_api/music/
|
163
|
-
- lib/itunes_api/music/
|
164
|
-
- lib/itunes_api/music/
|
165
|
-
- lib/itunes_api/requests/
|
166
|
-
- lib/itunes_api/requests/
|
167
|
-
- lib/itunes_api/
|
168
|
-
- lib/itunes_api/
|
169
|
-
- lib/itunes_api/
|
133
|
+
- lib/itunes_api/music/requests.rb
|
134
|
+
- lib/itunes_api/music/requests/albums.rb
|
135
|
+
- lib/itunes_api/music/requests/artist.rb
|
136
|
+
- lib/itunes_api/music/requests/search.rb
|
137
|
+
- lib/itunes_api/music/requests/songs.rb
|
138
|
+
- lib/itunes_api/music/results.rb
|
139
|
+
- lib/itunes_api/music/results/album.rb
|
140
|
+
- lib/itunes_api/music/results/artist.rb
|
141
|
+
- lib/itunes_api/music/results/song.rb
|
142
|
+
- lib/itunes_api/music/song.rb
|
143
|
+
- lib/itunes_api/request.rb
|
170
144
|
- lib/itunes_api/version.rb
|
171
145
|
homepage: https://github.com/mariodarco/itunes-api
|
172
146
|
licenses:
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module ItunesApi
|
2
|
-
module Music
|
3
|
-
# Album Tracklist resulting from a specific lookup
|
4
|
-
class AlbumLookup
|
5
|
-
attr_reader_init :album_id, :store
|
6
|
-
selfie :tracklist
|
7
|
-
|
8
|
-
def tracklist
|
9
|
-
return [] unless songs_data
|
10
|
-
|
11
|
-
Track.build(songs_data)
|
12
|
-
end
|
13
|
-
|
14
|
-
private
|
15
|
-
|
16
|
-
def songs_data
|
17
|
-
@songs_data ||= Requests::AlbumTracklist.songs(album_id, store)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
data/lib/itunes_api/music/all.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
module ItunesApi
|
2
|
-
module Music
|
3
|
-
# Artist or Band resulting from a specific lookup
|
4
|
-
class ArtistLookup
|
5
|
-
attr_reader_init :artist_id, :store
|
6
|
-
selfie :artist, :store
|
7
|
-
|
8
|
-
def artist
|
9
|
-
build_artist
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def build_artist
|
15
|
-
return lookup unless lookup
|
16
|
-
|
17
|
-
Artist.new(lookup, store)
|
18
|
-
end
|
19
|
-
|
20
|
-
def lookup
|
21
|
-
@lookup ||= Requests::Lookup.artist_with_albums(artist_id, store)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module ItunesApi
|
2
|
-
module Music
|
3
|
-
# Artists or Bands resulting from a search
|
4
|
-
class ArtistSearch
|
5
|
-
attr_reader_init :name, :store
|
6
|
-
selfie :artists
|
7
|
-
|
8
|
-
def artists
|
9
|
-
@artists ||= artist_ids.map { |id| build_artist(id) }.compact
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def artist_ids
|
15
|
-
@search ||= ItunesApi.artist_ids(name, store)
|
16
|
-
end
|
17
|
-
|
18
|
-
def build_artist(id)
|
19
|
-
if (lookup = lookup(id))
|
20
|
-
Artist.new(lookup, store)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def lookup(id)
|
25
|
-
Requests::Lookup.artist_with_albums(id, store)
|
26
|
-
rescue
|
27
|
-
nil
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
module ItunesApi
|
2
|
-
module Music
|
3
|
-
# Wrapper for song results.
|
4
|
-
class Track
|
5
|
-
attr_reader_init :data
|
6
|
-
private :data
|
7
|
-
|
8
|
-
def self.build(songs_data)
|
9
|
-
songs_data.map { |data| new(data) }
|
10
|
-
end
|
11
|
-
|
12
|
-
def duration
|
13
|
-
@duration ||= track_lenght(data[:trackTimeMillis])
|
14
|
-
end
|
15
|
-
|
16
|
-
def name
|
17
|
-
@name ||= data[:trackName]
|
18
|
-
end
|
19
|
-
|
20
|
-
def number
|
21
|
-
data[:trackNumber]
|
22
|
-
end
|
23
|
-
|
24
|
-
def streamable?
|
25
|
-
data[:isStreamable]
|
26
|
-
end
|
27
|
-
|
28
|
-
def to_hash
|
29
|
-
{
|
30
|
-
duration: duration,
|
31
|
-
name: name,
|
32
|
-
number: number,
|
33
|
-
stremable: stremable?,
|
34
|
-
}
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def track_lenght(milliseconds)
|
40
|
-
minutes, milliseconds = milliseconds.divmod(1000 * 60)
|
41
|
-
seconds = milliseconds / 1000
|
42
|
-
"#{minutes}:#{seconds}"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module ItunesApi
|
2
|
-
module Requests
|
3
|
-
# Allows querying the API via lookup for album songs.
|
4
|
-
class AlbumTracklist
|
5
|
-
include Base
|
6
|
-
attr_reader_init :album_id, :store
|
7
|
-
selfie :songs
|
8
|
-
|
9
|
-
def songs
|
10
|
-
songs_data.map do |song|
|
11
|
-
symbolize_keys(unwrapped(song))
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def action
|
18
|
-
'lookup'
|
19
|
-
end
|
20
|
-
|
21
|
-
def songs_data
|
22
|
-
results.find_all { |wrappers| wrappers['wrapperType'] == 'track' }
|
23
|
-
end
|
24
|
-
|
25
|
-
def query
|
26
|
-
{
|
27
|
-
entity: 'song',
|
28
|
-
id: album_id,
|
29
|
-
country: store.to_s.upcase,
|
30
|
-
limit: LIMIT,
|
31
|
-
sort: 'trackNumber'
|
32
|
-
}
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'faraday'
|
2
|
-
require 'json'
|
3
|
-
module ItunesApi
|
4
|
-
module Requests
|
5
|
-
# Allow requests to the iTunes API.
|
6
|
-
module Base
|
7
|
-
def results
|
8
|
-
@results ||= parsed_response.fetch('results') { [] }
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def parsed_response
|
14
|
-
JSON.parse(response.body)
|
15
|
-
end
|
16
|
-
|
17
|
-
def connection
|
18
|
-
Faraday.new(url: BASE_URL)
|
19
|
-
end
|
20
|
-
|
21
|
-
def response
|
22
|
-
connection.get(action, query)
|
23
|
-
end
|
24
|
-
|
25
|
-
def symbolize_keys(hash)
|
26
|
-
hash.each_with_object({}) do |(key, value), new_hash|
|
27
|
-
new_hash[key.to_sym] = value
|
28
|
-
new_hash
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def unwrapped(data_hash)
|
33
|
-
data_hash.tap { |hash| hash.delete('wrapperType') }
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
module ItunesApi
|
2
|
-
module Requests
|
3
|
-
# Allows querying the API via lookup.
|
4
|
-
class Lookup
|
5
|
-
include Base
|
6
|
-
attr_reader_init :artist_id, :store
|
7
|
-
selfie :artist_with_albums
|
8
|
-
|
9
|
-
def artist_with_albums
|
10
|
-
return artist unless artist
|
11
|
-
|
12
|
-
artist.merge(albums: albums)
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def action
|
18
|
-
'lookup'
|
19
|
-
end
|
20
|
-
|
21
|
-
def albums
|
22
|
-
albums_data.map do |album|
|
23
|
-
symbolize_keys(unwrapped(album))
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def albums_data
|
28
|
-
results.find_all { |wrappers| wrappers['wrapperType'] == 'collection' }
|
29
|
-
end
|
30
|
-
|
31
|
-
def artist
|
32
|
-
return artist_data unless artist_data
|
33
|
-
|
34
|
-
symbolize_keys(unwrapped(artist_data))
|
35
|
-
end
|
36
|
-
|
37
|
-
def artist_data
|
38
|
-
@artist_data ||= results.find do |wrappers|
|
39
|
-
wrappers['wrapperType'] == 'artist'
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def query
|
44
|
-
{
|
45
|
-
entity: 'album',
|
46
|
-
id: artist_id,
|
47
|
-
country: store.to_s.upcase,
|
48
|
-
limit: LIMIT,
|
49
|
-
sort: 'recent'
|
50
|
-
}
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
module ItunesApi
|
2
|
-
module Requests
|
3
|
-
# Fetch all the artist ids corresponding to a search term
|
4
|
-
class Search
|
5
|
-
include Base
|
6
|
-
attr_reader_init :artist_name, :store
|
7
|
-
selfie :artist_ids
|
8
|
-
|
9
|
-
def artist_ids
|
10
|
-
results.collect do |result|
|
11
|
-
name_in?(result['artistName']) && result['artistId']
|
12
|
-
end.compact.uniq
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def action
|
18
|
-
'search'
|
19
|
-
end
|
20
|
-
|
21
|
-
def name_in?(complete_name)
|
22
|
-
complete_name.downcase.include?(artist_name.downcase) ? true : nil
|
23
|
-
end
|
24
|
-
|
25
|
-
def query
|
26
|
-
{
|
27
|
-
attribute: 'artistTerm',
|
28
|
-
entity: 'musicArtist',
|
29
|
-
term: artist_name,
|
30
|
-
country: store.to_s.upcase,
|
31
|
-
limit: LIMIT,
|
32
|
-
media: 'music',
|
33
|
-
sort: 'recent'
|
34
|
-
}
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|