spotify-music-importer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a4a972face08ca5017576c6031ebdb88b350023e
4
+ data.tar.gz: c63e0f32185e76dc6a8b3dad534d021ac484f7fe
5
+ SHA512:
6
+ metadata.gz: ea1319db8d8ffc67541af815ed8866070474da82142702a1074659356a5175b60a60f7014e51bff63210385ac2a805c3acb0f4523b7d9c7ecf4b05307153a6c1
7
+ data.tar.gz: 38a91d7e42d43968f29044f20e0d315b44f3440764e619ac95122acd3efb435171140822755bfd25071ce9a43a778a431755b794b4897f1e85ad86c726a384cd
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ Gemfile.lock
31
+ .ruby-version
32
+ .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ gemspec
5
+
6
+ gem 'spotify-client', :github => 'X0nic/spotify-client', :branch => 'add-library'
7
+ gem 'colorize'
8
+
9
+ group :development do
10
+ gem 'pry'
11
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nathan Lee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,21 @@
1
+ # Spotify Music Importer
2
+
3
+ If you want to import your library of music into spotify, this will help. It uses CSV right now, as most library exports are in that format.
4
+
5
+ ## Installation
6
+
7
+ $ gem install spotify-music-importer
8
+
9
+ ## Usage
10
+
11
+ ```sh
12
+ bundle exec ./import.rb -f CSV_FILE -t SPOTIFY_ACCESS_TOKEN
13
+ ```
14
+
15
+ ## Contributing
16
+
17
+ 1. Fork it ( https://github.com/x0nic/spotify-music-importer/fork )
18
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
19
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
20
+ 4. Push to the branch (`git push origin my-new-feature`)
21
+ 5. Create a new Pull Request
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bundler/gem_tasks'
@@ -0,0 +1,42 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
4
+
5
+ require 'optparse'
6
+ require 'spotify/music/importer'
7
+
8
+ options = {}
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: import.rb [options]"
11
+
12
+ opts.on("-f", "--file FILENAME", "The filename to import") do |filename|
13
+ options[:filename] = filename
14
+ end
15
+
16
+ opts.on("-m", "--missing MISSING.json", "The output filename of bad matches") do |missing|
17
+ options[:missing] = missing
18
+ end
19
+
20
+ opts.on("-l", "--limit LIMIT", "Limit the number of lines to import") do |limit|
21
+ options[:limit] = limit.to_i
22
+ end
23
+
24
+ opts.on("-s", "--skip SKIP", "Skip the first X lines of import") do |skip|
25
+ options[:skip] = skip.to_i
26
+ end
27
+
28
+ opts.on("-t", "--access-token ACCESS_TOKEN", "Your access token to access the Spotify API. Find one here: https://developer.spotify.com/web-api/console/") do |token|
29
+ options[:access_token] = token
30
+ end
31
+
32
+ end.parse!
33
+
34
+ importer = Spotify::Music::Importer::Cli.new
35
+ importer.import(options[:filename], options)
36
+
37
+ if options[:missing]
38
+ File.open(options[:missing], 'w') do |f|
39
+ puts "Writing to file #{options[:missing]}"
40
+ f.write(JSON.pretty_generate(importer.missing))
41
+ end
42
+ end
@@ -0,0 +1,55 @@
1
+ require 'spotify/music/importer/spotify_library'
2
+ require 'spotify/music/importer/collection_record'
3
+ require 'spotify/music/importer/spotify_match'
4
+
5
+ require 'colorize'
6
+ require 'csv'
7
+ require 'spotify-client'
8
+
9
+
10
+ module Spotify
11
+ module Music
12
+ module Importer
13
+ class Cli
14
+ def initialize
15
+ end
16
+
17
+ def import(filename, options)
18
+ @options = options
19
+ @library = SpotifyLibrary.new(client)
20
+ limit = options.delete(:limit) { nil }
21
+ skip = options.delete(:skip) { 0 }
22
+
23
+ collection = CSV.read(filename, :headers => true)
24
+ collection.each_with_index do |row, index|
25
+ next if skip && skip > index+1
26
+ break if limit && index+1 > limit+skip
27
+
28
+ record = CollectionRecord.new(row, :clean_album => true, :clean_track => true)
29
+ results = SpotifyMatch.new(client.search(:track, format_query(record)), :clean_album => true, :clean_track => true)
30
+ @library.find_and_add_to_library(record, results, index)
31
+ end
32
+ end
33
+
34
+ def missing
35
+ @library.missing
36
+ end
37
+
38
+ def format_query(record)
39
+ "track:#{record.name} artist:#{record.artist} album:#{record.album}"
40
+ end
41
+
42
+ def client
43
+ @client ||= begin
44
+ if @options[:access_token]
45
+ Spotify::Client.new({:raise_errors => true}.merge(@options))
46
+ else
47
+ Spotify::Client.new(:raise_errors => true)
48
+ end
49
+ end
50
+ end
51
+
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,32 @@
1
+ require "spotify/music/importer/version"
2
+
3
+ module Spotify::Music::Importer
4
+ class AlbumNameCleaner
5
+ def initialize(album_name)
6
+ @album_name = album_name
7
+ end
8
+
9
+ def clean
10
+ cleaned_album = @album_name
11
+
12
+ extraneous_album_info.each do |album_info|
13
+ cleaned_album = cleaned_album.gsub(album_info, '').strip
14
+ end
15
+
16
+ cleaned_album
17
+ end
18
+
19
+ def extraneous_album_info
20
+ [
21
+ '(Special Edition)',
22
+ '(Deluxe Edition)',
23
+ '(Deluxe Edition Remastered)',
24
+ '(Remastered)',
25
+ '(Canadian Version)',
26
+ '(Non EU Version)',
27
+ '(UK Version)',
28
+ '(Brazilian Version)'
29
+ ]
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,22 @@
1
+ require "spotify/music/importer/version"
2
+
3
+ module Spotify::Music::Importer
4
+ class CollectionMatch
5
+ def initialize(collection_record, spotify_match)
6
+ @collection_record = collection_record
7
+ @spotify_match = spotify_match
8
+ end
9
+
10
+ def full_match
11
+ name_match && album_match
12
+ end
13
+
14
+ def name_match
15
+ @collection_record.name == @spotify_match.name
16
+ end
17
+
18
+ def album_match
19
+ @collection_record.album == @spotify_match.album
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,45 @@
1
+ require "spotify/music/importer/version"
2
+ require 'spotify/music/importer/track_name_cleaner'
3
+ require 'spotify/music/importer/album_name_cleaner'
4
+
5
+ module Spotify::Music::Importer
6
+ class CollectionRecord
7
+ def initialize(row, options = {})
8
+ @row = row
9
+ @clean_album = options.delete(:clean_album) { false }
10
+ @clean_track = options.delete(:clean_track) { false }
11
+ end
12
+
13
+ def name
14
+ if @clean_track
15
+ TrackNameCleaner.new(@row['Name']).clean
16
+ else
17
+ @row['Name']
18
+ end
19
+ end
20
+
21
+ def artist
22
+ if @clean_album
23
+ @row['Artist'].gsub('(Special Edition)', '').strip
24
+ else
25
+ @row['Artist']
26
+ end
27
+ end
28
+
29
+ def album
30
+ if @clean_album
31
+ AlbumNameCleaner.new(@row['Album']).clean
32
+ else
33
+ @row['Album']
34
+ end
35
+ end
36
+
37
+ def to_s
38
+ "Name: #{name} Album: #{album} Artist: #{artist}"
39
+ end
40
+
41
+ def to_json(options = {})
42
+ { :name => name, :album => album, :artist => artist }.to_json(options)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,53 @@
1
+ require "spotify/music/importer/version"
2
+ require "spotify/music/importer/collection_match"
3
+
4
+ module Spotify::Music::Importer
5
+ class SpotifyLibrary
6
+ def initialize(client)
7
+ @client = client
8
+ @missing = []
9
+ end
10
+
11
+ def find_and_add_to_library(record, results, index)
12
+ match = CollectionMatch.new(record, results)
13
+
14
+ if results.found_match?
15
+ print "[#{index+1}] "
16
+ if match.full_match
17
+ print results.to_s.green
18
+ add_to_library(results.id)
19
+ elsif match.name_match
20
+ print results.to_s.yellow
21
+ add_to_library(results.id)
22
+ elsif match.album_match
23
+ print results.to_s.colorize(:orange)
24
+ add_to_library(results.id)
25
+ puts "Collection Record: #{record}"
26
+ else
27
+ print results
28
+ add_to_library(results.id)
29
+ puts "Collection Record: #{record}"
30
+ end
31
+ else
32
+ puts "not found - #{record}".red
33
+ @missing << record
34
+ end
35
+ end
36
+
37
+ def missing
38
+ @missing
39
+ end
40
+
41
+ private
42
+
43
+ def add_to_library(track_id)
44
+ if @client.library?(track_id).first
45
+ puts " Ignored #{track_id}"
46
+ else
47
+ print ' Adding '
48
+ @client.add_library_tracks(track_id)
49
+ puts " Added #{track_id}"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ require "spotify/music/importer/version"
2
+
3
+ module Spotify::Music::Importer
4
+ class SpotifyMatch
5
+ def initialize(results, options = {})
6
+ @results = results
7
+ @clean_album = options.delete(:clean_album) { false }
8
+ @clean_track = options.delete(:clean_track) { false }
9
+ end
10
+
11
+ def found_match?
12
+ @results['tracks']['items'].count > 0
13
+ end
14
+
15
+ def name
16
+ if @clean_track
17
+ TrackNameCleaner.new(@results["tracks"]["items"].first["name"]).clean
18
+ else
19
+ @results["tracks"]["items"].first["name"]
20
+ end
21
+ end
22
+
23
+ def artist
24
+ @results["tracks"]['items'].first['artists'].first['name']
25
+ end
26
+
27
+ def album
28
+ if @clean_album
29
+ AlbumNameCleaner.new(@results["tracks"]['items'].first['album']['name']).clean
30
+ else
31
+ @results["tracks"]['items'].first['album']['name']
32
+ end
33
+ end
34
+
35
+ def id
36
+ @results ["tracks"]["items"].first["id"]
37
+ end
38
+
39
+ def to_s
40
+ "Name: #{name} Album: #{album} Artist: #{artist} id: #{id}"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,30 @@
1
+ require "spotify/music/importer/version"
2
+
3
+ module Spotify::Music::Importer
4
+ class TrackNameCleaner
5
+ def initialize(track_name)
6
+ @track_name = track_name
7
+ end
8
+
9
+ def clean
10
+ cleaned_track = @track_name
11
+
12
+ extraneous_track_info.each do |track_info|
13
+ cleaned_track = cleaned_track.gsub(track_info, '').strip
14
+ end
15
+
16
+ cleaned_track
17
+ end
18
+
19
+ def extraneous_track_info
20
+ [
21
+ '- Remastered',
22
+ '- Single',
23
+ '(Clean Album Version) (Clean)',
24
+ '(Album Version)',
25
+ '(Amended Album Version)',
26
+ '(Explicit Album Version)'
27
+ ]
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ module Spotify
2
+ module Music
3
+ module Importer
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+
2
+
3
+
4
+
5
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spotify/music/importer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "spotify-music-importer"
8
+ spec.version = Spotify::Music::Importer::VERSION
9
+ spec.authors = ["Nathan Lee"]
10
+ spec.email = ["nathan@globalphobia.com"]
11
+ spec.summary = %q{A utility to import a csv into your spotify music library}
12
+ # spec.description = %q{TODO: Write a longer description. Optional.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spotify-music-importer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - nathan@globalphobia.com
44
+ executables:
45
+ - spotify-music-importer
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".ruby-gemset"
51
+ - ".ruby-version"
52
+ - Gemfile
53
+ - Gemfile.lock
54
+ - LICENSE
55
+ - README.md
56
+ - Rakefile
57
+ - bin/spotify-music-importer
58
+ - lib/spotify/music/importer.rb
59
+ - lib/spotify/music/importer/album_name_cleaner.rb
60
+ - lib/spotify/music/importer/collection_match.rb
61
+ - lib/spotify/music/importer/collection_record.rb
62
+ - lib/spotify/music/importer/spotify_library.rb
63
+ - lib/spotify/music/importer/spotify_match.rb
64
+ - lib/spotify/music/importer/track_name_cleaner.rb
65
+ - lib/spotify/music/importer/version.rb
66
+ - lib/spotify_importer.rb
67
+ - spotify-music-importer.gemspec
68
+ homepage: ''
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A utility to import a csv into your spotify music library
92
+ test_files: []