itunes_track 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +6 -0
- data/bin/itunes_track +5 -0
- data/itunes_track.gemspec +28 -0
- data/lib/itunes_track.rb +74 -0
- data/lib/itunes_track/cli.rb +59 -0
- data/lib/itunes_track/version.rb +3 -0
- data/spec/itunes_track_spec.rb +70 -0
- data/spec/spec_helper.rb +2 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0fbbd938d1446138a1f1b951cb67117c43d90cbc
|
4
|
+
data.tar.gz: 449230076e358b736f02e84157868bc1ba6034c7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b1818c2f4466a830b59ac10d501d3d55691213546aa0ac22c8c9349c089c998481903dc54d00171b2a715c44e47a0f3300a7dbb6574bbcf3173846a42800985e
|
7
|
+
data.tar.gz: d9ac139c83537bb051d53c946acff1ef431a6ef111b67ffa6040c36c710aa5a297716213fcc765de69a80ae576d4593656f71051694dd8fd0669efecd833cc19
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 kyoendo
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# ItunesTrack
|
2
|
+
|
3
|
+
`ItunesTrack` is a object wrapper of iTunes music tracks. You can get the track information and save them in a CSV file.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'itunes_track'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install itunes_track
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
In your terminal, try followings;
|
22
|
+
|
23
|
+
# Show help
|
24
|
+
% itunes_track
|
25
|
+
|
26
|
+
# Show number of tracks with ARTIST match
|
27
|
+
% itunes_track size beatles
|
28
|
+
|
29
|
+
# Create CSV file
|
30
|
+
% itunes_track csv itunes.csv
|
31
|
+
|
32
|
+
In your ruby script;
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'itunes_track'
|
36
|
+
|
37
|
+
# to build track data for `beatles` with 'name' and 'artist' fields
|
38
|
+
ItunesTrack.build(:name, :artist) do |t|
|
39
|
+
t.artist.get.match /beatles/i
|
40
|
+
end
|
41
|
+
|
42
|
+
# create csv for the builded track data
|
43
|
+
ItunesTrack.to_csv('beatles.csv')
|
44
|
+
```
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/itunes_track
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'itunes_track/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "itunes_track"
|
8
|
+
spec.version = ItunesTrack::VERSION
|
9
|
+
spec.authors = ["kyoendo"]
|
10
|
+
spec.email = ["postagie@gmail.com"]
|
11
|
+
spec.description = %q{Object wrapper of iTunes music tracks}
|
12
|
+
spec.summary = %q{Object wrapper of iTunes music tracks}
|
13
|
+
spec.homepage = "https://github.com/melborne/itunes_track"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
spec.required_ruby_version = ">= 2.0.0"
|
21
|
+
|
22
|
+
spec.add_dependency "rb-appscript", "~> 0.6"
|
23
|
+
spec.add_dependency "thor"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
spec.add_development_dependency "rspec"
|
28
|
+
end
|
data/lib/itunes_track.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'appscript'
|
2
|
+
require 'csv'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
require 'itunes_track/version'
|
6
|
+
|
7
|
+
include Appscript
|
8
|
+
|
9
|
+
class ItunesTrack
|
10
|
+
ATTRS = %i(name time artist album genre rating played_count year composer track_count track_number disc_count disc_number lyrics)
|
11
|
+
|
12
|
+
require 'itunes_track/cli'
|
13
|
+
|
14
|
+
class Track < OpenStruct
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def build(*attrs, &blk)
|
19
|
+
tracks.clear
|
20
|
+
@attrs = attrs.empty? ? ATTRS : attrs
|
21
|
+
track_attrs = []
|
22
|
+
itunes_tracks(&blk).each do |track|
|
23
|
+
track_attrs << begin
|
24
|
+
@attrs.inject({}) do |h, attr|
|
25
|
+
val = track.send(attr).get rescue ''
|
26
|
+
if val.is_a?(String) && val.encoding.to_s!='UTF-8'
|
27
|
+
val = val.force_encoding('UTF-8')
|
28
|
+
end
|
29
|
+
h[attr.intern] = val
|
30
|
+
h
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
tracks.push *track_attrs.map { |attrs| Track.new attrs }
|
35
|
+
end
|
36
|
+
|
37
|
+
def full_build(&blk)
|
38
|
+
build(*track_properties, &blk)
|
39
|
+
end
|
40
|
+
|
41
|
+
def tracks
|
42
|
+
@tracks ||= []
|
43
|
+
end
|
44
|
+
|
45
|
+
def itunes
|
46
|
+
@itunes ||= app('iTunes').tracks
|
47
|
+
end
|
48
|
+
|
49
|
+
def itunes_tracks(&blk)
|
50
|
+
block_given? ? itunes.get.select(&blk) : itunes.get
|
51
|
+
end
|
52
|
+
|
53
|
+
def size(&blk)
|
54
|
+
itunes_tracks(&blk).size
|
55
|
+
end
|
56
|
+
|
57
|
+
def track_properties
|
58
|
+
itunes.properties.map(&:intern)
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_csv(path, attrs=@attrs)
|
62
|
+
raise 'Should be built first' if tracks.empty?
|
63
|
+
CSV.open(path, 'wb') do |csv|
|
64
|
+
csv << attrs
|
65
|
+
tracks.each { |t| csv << attrs.map{ |attr| t.send attr } }
|
66
|
+
end
|
67
|
+
rescue => e
|
68
|
+
puts "Something go wrong during csv building: #{e}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
ITunesTrack = ItunesTrack
|
74
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'thor'
|
2
|
+
|
3
|
+
class ItunesTrack
|
4
|
+
class CLI < Thor
|
5
|
+
desc "size [ARTIST]", "Show track size for ARTIST match"
|
6
|
+
def size(artist=nil)
|
7
|
+
cond = artist ? ->t{ t.artist.get.match /#{artist}/i } : ->t{ true }
|
8
|
+
puts ItunesTrack.size(&cond)
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "tracks", "Show tracks"
|
12
|
+
option :artist, aliases:"-a"
|
13
|
+
option :album, aliases:"-l"
|
14
|
+
def tracks
|
15
|
+
artist, album = options[:artist], options[:album]
|
16
|
+
if artist && album
|
17
|
+
cond = ->t{ t.artist.get.match(/#{artist}/i) && t.album.get.match(/#{album}/i) }
|
18
|
+
elsif artist
|
19
|
+
cond = ->t{ t.artist.get.match(/#{artist}/i) }
|
20
|
+
elsif album
|
21
|
+
cond = ->t{ t.album.get.match(/#{album}/i) }
|
22
|
+
else
|
23
|
+
cond = ->t{ true }
|
24
|
+
end
|
25
|
+
attrs = %i(name time artist album played_count)
|
26
|
+
res = ItunesTrack.itunes_tracks(&cond)
|
27
|
+
unless res.empty?
|
28
|
+
res.map { |t|
|
29
|
+
puts attrs.inject("") { |str, attr| [str, t.send(attr).get.to_s].join(" ") }
|
30
|
+
}
|
31
|
+
else
|
32
|
+
puts "No tracks found"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "csv PATH", "Create CSV file from tracks data"
|
37
|
+
option :fields, aliases:"-f", default: ItunesTrack::ATTRS.join(",")
|
38
|
+
option :artist, aliases:"-a"
|
39
|
+
def csv(path)
|
40
|
+
fields = options[:fields].split(",")
|
41
|
+
cond =
|
42
|
+
if options[:artist]
|
43
|
+
->t{ t.artist.get.match(/#{options[:artist]}/i) }
|
44
|
+
else
|
45
|
+
->t{ true }
|
46
|
+
end
|
47
|
+
puts "I am working on csv..."
|
48
|
+
ItunesTrack.build(*fields, &cond)
|
49
|
+
ItunesTrack.to_csv(path, fields)
|
50
|
+
puts "CSV file successfully created at #{path}."
|
51
|
+
end
|
52
|
+
|
53
|
+
desc "version", "Show ItunesTrack version"
|
54
|
+
def version
|
55
|
+
puts "ItunesTrack #{ItunesTrack::VERSION} (c) 2013 kyoendo"
|
56
|
+
end
|
57
|
+
map "-v" => :version
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ItunesTrack do
|
4
|
+
before(:each) do
|
5
|
+
ITunesTrack.tracks.clear
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should have a version number' do
|
9
|
+
ItunesTrack::VERSION.should_not be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '.itunes' do
|
13
|
+
it 'returns a Appscript::Reference' do
|
14
|
+
expect(ITunesTrack.itunes).to be_a_kind_of(Appscript::Reference)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.size' do
|
19
|
+
it 'returns number of tracks at iTunes' do
|
20
|
+
expect(ITunesTrack.size).to be >= 1000
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns number of tracks for ABBA' do
|
24
|
+
size = ITunesTrack.size { |t| t.artist.get.match /ABBA/ }
|
25
|
+
expect(size).to eq 1
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '.track_properties' do
|
30
|
+
it 'returns track properties' do
|
31
|
+
properties = %i(name time artist album genre rating played_count year composer
|
32
|
+
track_count track_number disc_count disc_number played_count lyrics)
|
33
|
+
expect(ITunesTrack.track_properties).to include(*properties)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '.build' do
|
38
|
+
context 'with :name, :artist arguments' do
|
39
|
+
it 'builds track objects with name and artist attrs' do
|
40
|
+
ITunesTrack.build(:name,:artist)
|
41
|
+
expect(ITunesTrack.tracks.all? { |t| t.is_a? ITunesTrack::Track }).to be_true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'with a block' do
|
46
|
+
it 'builds track objects under the block condition' do
|
47
|
+
ITunesTrack.build(:name, :artist) do |t|
|
48
|
+
t.artist.get.match /beatles/i
|
49
|
+
end
|
50
|
+
expect(ITunesTrack.tracks.size).to be <= 300
|
51
|
+
expect(ITunesTrack.tracks.all? { |t| t.artist.match /beatles/i }).to be_true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '.tracks' do
|
57
|
+
context 'before build' do
|
58
|
+
it 'should be empty' do
|
59
|
+
expect(ITunesTrack.tracks).to be_empty
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'after build' do
|
64
|
+
it 'should not be empty' do
|
65
|
+
ITunesTrack.build(:name)
|
66
|
+
expect(ITunesTrack.tracks).not_to be_empty
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: itunes_track
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kyoendo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rb-appscript
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: thor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Object wrapper of iTunes music tracks
|
84
|
+
email:
|
85
|
+
- postagie@gmail.com
|
86
|
+
executables:
|
87
|
+
- itunes_track
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- .rspec
|
93
|
+
- .travis.yml
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/itunes_track
|
99
|
+
- itunes_track.gemspec
|
100
|
+
- lib/itunes_track.rb
|
101
|
+
- lib/itunes_track/cli.rb
|
102
|
+
- lib/itunes_track/version.rb
|
103
|
+
- spec/itunes_track_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: https://github.com/melborne/itunes_track
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 2.0.0
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.1.11
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Object wrapper of iTunes music tracks
|
129
|
+
test_files:
|
130
|
+
- spec/itunes_track_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
has_rdoc:
|