spotty 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/spotty.rb +6 -0
- data/lib/spotty/client.rb +165 -0
- data/lib/spotty/version.rb +3 -0
- data/spotty.gemspec +28 -0
- metadata +153 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fd96378f3be83ab76b821b6b65449392ee11c2f7
|
4
|
+
data.tar.gz: fe01358750ea6492884c7a1988d8f16e451d6318
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a65fd9ee5b87837a325b1a8eb35e868c18b6afc808c5721b3a37de714b0d71be726921bb0c3b2a905bc13d8a081de11a3067cd2ce5cbf18c63b084e53d5ec2d3
|
7
|
+
data.tar.gz: 927728d2adca1c7b2caca9d0511105ad7b5001c1834e877ab70822eb118c6fd2858bd47c09941b0b3492f3dbe041601b7d9e7725aafb2cd501335d46fc0643e2
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Kyle King
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Spotty
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/spotty`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'spotty'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install spotty
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/spotty.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "spotty"
|
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
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/spotty.rb
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
require 'http'
|
2
|
+
require 'base64'
|
3
|
+
require 'webrick'
|
4
|
+
require 'multi_json'
|
5
|
+
require 'addressable'
|
6
|
+
|
7
|
+
class Spotty
|
8
|
+
class Client
|
9
|
+
SCOPES = [
|
10
|
+
"playlist-read-private",
|
11
|
+
"playlist-read-collaborative",
|
12
|
+
"playlist-modify-public",
|
13
|
+
"playlist-modify-private",
|
14
|
+
"streaming",
|
15
|
+
"user-follow-modify",
|
16
|
+
"user-follow-read",
|
17
|
+
"user-library-read",
|
18
|
+
"user-library-modify",
|
19
|
+
"user-read-private",
|
20
|
+
"user-read-birthdate",
|
21
|
+
"user-read-email",
|
22
|
+
"user-top-read"
|
23
|
+
]
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
refresh
|
27
|
+
end
|
28
|
+
|
29
|
+
def configure
|
30
|
+
require 'tty-prompt'
|
31
|
+
prompt = TTY::Prompt.new
|
32
|
+
|
33
|
+
prompt.puts "\nSpotty needs to be authorized as a Spotify Application to use Spotify's Web API. It only takes a minute to setup.\n\n"
|
34
|
+
prompt.puts "First, create an new application at: \n\n"
|
35
|
+
prompt.ok " https://developer.spotify.com/my-applications\n"
|
36
|
+
prompt.ask "Hit enter when you're ready to continue."
|
37
|
+
|
38
|
+
config = {}
|
39
|
+
config['id'] = prompt.ask "\nClient ID: "
|
40
|
+
config['secret'] = prompt.ask 'Client Secret: '
|
41
|
+
|
42
|
+
port = rand((1025..9999))
|
43
|
+
|
44
|
+
config['redirect'] = Addressable::URI.new(
|
45
|
+
port: port,
|
46
|
+
scheme: 'http',
|
47
|
+
host: 'localhost',
|
48
|
+
).to_s
|
49
|
+
|
50
|
+
url = Addressable::URI.new(
|
51
|
+
scheme: 'https',
|
52
|
+
path: '/authorize',
|
53
|
+
host: 'accounts.spotify.com',
|
54
|
+
query_values: {
|
55
|
+
client_id: config['id'],
|
56
|
+
scope: SCOPES.join(' '),
|
57
|
+
response_type: 'code',
|
58
|
+
redirect_uri: config['redirect']
|
59
|
+
}
|
60
|
+
).to_s
|
61
|
+
|
62
|
+
prompt.puts "\nAdd the following URL to your application's list of callbacks:\n\n"
|
63
|
+
prompt.ok " #{config['redirect']}\n"
|
64
|
+
prompt.ask "Remember to click 'Save' after adding the url. Hit enter when you're done."
|
65
|
+
prompt.puts "Visit the following URL to finish authorizing Spotty:\n\n"
|
66
|
+
prompt.ok " #{url}\n"
|
67
|
+
prompt.error "Waiting for authorization ..."
|
68
|
+
|
69
|
+
server = WEBrick::HTTPServer.new :Port => port
|
70
|
+
|
71
|
+
server.mount_proc '/' do |req, res|
|
72
|
+
if req.query['error']
|
73
|
+
prompt.error req.query['error']
|
74
|
+
res.body = req.query['error']
|
75
|
+
server.shutdown
|
76
|
+
elsif req.query['code']
|
77
|
+
config['code'] = req.query['code']
|
78
|
+
prompt.ok 'Authorization code received'
|
79
|
+
res.body = 'Spotty has successfully authenticated! You can close this window'
|
80
|
+
server.shutdown
|
81
|
+
else
|
82
|
+
res.body = 'Waiting for Spotify to send an authorization code ...'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
server.start
|
87
|
+
|
88
|
+
unless config['code']
|
89
|
+
raise 'Authorization failed: Spotify did not send an authorization code'
|
90
|
+
end
|
91
|
+
|
92
|
+
params = [config['id'],config['secret']]
|
93
|
+
basic = Base64.strict_encode64(params.join(':'))
|
94
|
+
client = HTTP.headers(Authorization: "Basic #{basic}")
|
95
|
+
|
96
|
+
response = client.post('https://accounts.spotify.com/api/token', form: {
|
97
|
+
grant_type: 'authorization_code',
|
98
|
+
code: config['code'],
|
99
|
+
redirect_uri: config['redirect']
|
100
|
+
})
|
101
|
+
|
102
|
+
if response.code != 200
|
103
|
+
raise "Authorization failed: #{response.body}"
|
104
|
+
end
|
105
|
+
|
106
|
+
save response.parse.merge(
|
107
|
+
'id': config['id'],
|
108
|
+
'secret': config['secret']
|
109
|
+
)
|
110
|
+
end
|
111
|
+
|
112
|
+
def method_missing method, *args, &block
|
113
|
+
if @http.respond_to? method
|
114
|
+
refresh
|
115
|
+
@http.send method, *args, &block
|
116
|
+
else
|
117
|
+
super method, @args, &block
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
def save config
|
124
|
+
config['created_at'] = Time.now
|
125
|
+
path = File.expand_path '~/.spotty'
|
126
|
+
File.write path, MultiJson.dump(config, pretty: true)
|
127
|
+
end
|
128
|
+
|
129
|
+
def refresh
|
130
|
+
if @http.nil?
|
131
|
+
path = File.expand_path '~/.spotty'
|
132
|
+
|
133
|
+
configure unless File.exists? path
|
134
|
+
@config = MultiJson.load(File.read(path))
|
135
|
+
@config['created_at'] = Time.parse @config['created_at']
|
136
|
+
|
137
|
+
@http = HTTP.headers(
|
138
|
+
Authorization: "Bearer #{@config['access_token']}"
|
139
|
+
)
|
140
|
+
end
|
141
|
+
|
142
|
+
if @config['created_at'] + @config['expires_in'] < Time.now
|
143
|
+
params = [@config['id'],@config['secret']]
|
144
|
+
basic = Base64.strict_encode64(params.join(':'))
|
145
|
+
client = HTTP.headers(Authorization: "Basic #{basic}")
|
146
|
+
|
147
|
+
response = client.post('https://accounts.spotify.com/api/token', form: {
|
148
|
+
grant_type: 'refresh_token',
|
149
|
+
refresh_token: @config['refresh_token']
|
150
|
+
})
|
151
|
+
|
152
|
+
if response.code != 200
|
153
|
+
raise "Authorization failed: #{response.body}"
|
154
|
+
end
|
155
|
+
|
156
|
+
@config.merge! response.parse
|
157
|
+
save @config
|
158
|
+
|
159
|
+
@http = HTTP.headers(
|
160
|
+
Authorization: "Bearer #{@config['access_token']}"
|
161
|
+
)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
data/spotty.gemspec
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 'spotty/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "spotty"
|
8
|
+
spec.version = Spotty::VERSION
|
9
|
+
spec.authors = ["Kyle King"]
|
10
|
+
spec.email = ["kylejking@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = 'A Spotify automation library for Ruby'
|
13
|
+
spec.homepage = "https://github.com/kylekyle/spotty"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "tty"
|
22
|
+
spec.add_dependency "http"
|
23
|
+
spec.add_dependency "multi_json"
|
24
|
+
spec.add_dependency "addressable"
|
25
|
+
spec.add_development_dependency "pry"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spotty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyle King
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: tty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: http
|
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: multi_json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: addressable
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
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: pry
|
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
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: bundler
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.12'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.12'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- kylejking@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- Gemfile
|
120
|
+
- LICENSE.txt
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- bin/console
|
124
|
+
- bin/setup
|
125
|
+
- lib/spotty.rb
|
126
|
+
- lib/spotty/client.rb
|
127
|
+
- lib/spotty/version.rb
|
128
|
+
- spotty.gemspec
|
129
|
+
homepage: https://github.com/kylekyle/spotty
|
130
|
+
licenses:
|
131
|
+
- MIT
|
132
|
+
metadata: {}
|
133
|
+
post_install_message:
|
134
|
+
rdoc_options: []
|
135
|
+
require_paths:
|
136
|
+
- lib
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
requirements: []
|
148
|
+
rubyforge_project:
|
149
|
+
rubygems_version: 2.6.4
|
150
|
+
signing_key:
|
151
|
+
specification_version: 4
|
152
|
+
summary: A Spotify automation library for Ruby
|
153
|
+
test_files: []
|