shellify 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6dc23ed10c36ddf92d2881d109271119d606b8ea380183a1d277353f13693f16
4
+ data.tar.gz: d66f3374654de5851cf5565a4c2c1ede72971c80a223a2748cc8af6d2f25daf7
5
+ SHA512:
6
+ metadata.gz: 32d3d1b078e5fe7fa1877cefed381104ce05df8ac2d72b3a23ef984ebba6efac5a3e37e2f6de70c5ece2495dd16cec8228a6db385235d43a67309d78da0a0f6e
7
+ data.tar.gz: 8d80c7a94c9ebc89408236324e3b2f6f2f10f3ce15bb26b88a012ac9ac5d91eb6e9f198553f34646e1f43d92341a35e81f3103b04c77ce64abd4d8e513e803cd
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Derek Povah <http://derekpovah.com>
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,56 @@
1
+ Shellify
2
+ ========
3
+
4
+ Use Spotify from the command line
5
+
6
+ Installation
7
+ ------------
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```bash
12
+ $ gem install shellify
13
+ ```
14
+
15
+ #### Setup
16
+
17
+ 1. Create a Spotify application in the [Spotify Developer Dashboard](https://developer.spotify.com/dashboard).
18
+ 1. Get your Spotify OAUTH `access_token` and `refresh_token`. (Shelilfy currently doesn't currently implement a two way OAUTH flow, but Spotify has [sample apps](https://github.com/spotify/web-api-auth-examples) that you can use to get your initial keys. Shellify will handle exchanging refresh tokens for access tokens after initial setup.)
19
+ 1. Create two json files in `~/.config/shellify`
20
+ `config.json`
21
+ ```json
22
+ {
23
+ "client_id": "xxxxxxxxxxxxxxxx",
24
+ "client_secret": "xxxxxxxxxxxxxxx"
25
+ }
26
+ ```
27
+
28
+ `spotify_user.json`
29
+ ```json
30
+ {
31
+ "id": "spotify_user_id",
32
+ "token": "xxxxxxxxxxxxxxx",
33
+ "refresh_token": "xxxxxxxxxxxxxxx"
34
+ }
35
+ ```
36
+
37
+ Commands
38
+ --------
39
+
40
+ ```
41
+ $ shellify
42
+ Now Playing:
43
+ 3 Libras - A Perfect Circle - 02:24/03:39 - ♥
44
+ ```
45
+
46
+ See `shellify help` for a full list of commands.
47
+
48
+ RSpotify
49
+ --------
50
+
51
+ Shellify is basically a just a wrapper for [RSpotify](https://github.com/guilhermesad/rspotify) and wouldn't be possible (without significantly more effort) thanks to the work that's already been done over there.
52
+
53
+ License
54
+ -------
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/exe/shellify ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'shellify'
5
+
6
+ begin
7
+ Shellify::Cli.new.run
8
+ rescue Interrupt
9
+ exit 1
10
+ end
@@ -0,0 +1,166 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'commander'
4
+ require 'shellify/utils'
5
+
6
+ module Shellify
7
+ class Cli
8
+ include Commander::Methods
9
+ include Shellify::Utils
10
+
11
+ def initialize
12
+ $VERBOSE = nil # Suppress warnings from RSpotify's rest-client implementation by default
13
+ @config = Shellify::Config.new
14
+ @user = Shellify::User.new(@config.config_dir)
15
+ end
16
+
17
+ def run
18
+ program :name, 'Shellify'
19
+ program :version, Shellify::VERSION
20
+ program :description, 'Use Spotify from the command line'
21
+
22
+ command :devices do |c|
23
+ c.description = 'List available playback devices'
24
+ c.action do
25
+ devices = @user.devices
26
+ devices.each do |device|
27
+ puts " #{device.name}#{" - \e[1m𝅘𝅥𝅮\e[22m" if device.is_active}"
28
+ end
29
+ end
30
+ end
31
+
32
+ command :playing do |c|
33
+ c.description = 'List information about the current song'
34
+ c.action do
35
+ return puts " Nothing playing" unless @user.player.playing?
36
+
37
+ print_current_song
38
+ end
39
+ end
40
+
41
+ command :volume do |c|
42
+ c.description = 'Set the volume of the current playback device'
43
+ c.action do |args, options|
44
+ @user.player.volume(args[0])
45
+ end
46
+ end
47
+
48
+ command :like do |c|
49
+ c.description = 'Save the current song to your library'
50
+ c.action do
51
+ @user.save_tracks!([@user.player.currently_playing])
52
+ end
53
+ end
54
+
55
+ command :unlike do |c|
56
+ c.description = 'Remove the current song from your library'
57
+ c.action do
58
+ @user.remove_tracks!([@user.player.currently_playing])
59
+ end
60
+ end
61
+
62
+ command :playlists do |c|
63
+ c.description = 'List your playlists'
64
+ c.action do
65
+ @user.playlists.each do |playlist|
66
+ puts " #{playlist.name} - #{playlist.owner.display_name}#{" - Collaborative" if playlist.collaborative}"
67
+ end
68
+ end
69
+ end
70
+
71
+ command :add do |c|
72
+ c.description = 'Add the current song to the provided playlist'
73
+ c.action do |args, options|
74
+ return puts " Nothing playing" unless @user.player.playing?
75
+
76
+ playlist = @user.playlists.find { |p| p.name == args[0] }
77
+ return puts " Playlist not found" unless playlist
78
+
79
+ playlist.add_tracks!([@user.player.currently_playing])
80
+ end
81
+ end
82
+
83
+ command :remove do |c|
84
+ c.description = 'Remove the currently playing song from the provided playlist'
85
+ c.action do |args, options|
86
+ return puts " Nothing playing" unless @user.player.playing?
87
+
88
+ playlist = @user.playlists.find { |p| p.name == args[0] }
89
+ return puts " Playlist not found" unless playlist
90
+
91
+ playlist.remove_tracks!([@user.player.currently_playing])
92
+ end
93
+ end
94
+
95
+ command :play do |c|
96
+ c.description = 'Play or Pause on the currently playing device'
97
+ c.action do
98
+ begin
99
+ if @user.player.playing?
100
+ @user.player.pause
101
+ else
102
+ @user.player.play
103
+ print_current_song
104
+ end
105
+ rescue RestClient::NotFound
106
+ @user.player.play(@user.devices.first.id)
107
+ end
108
+ end
109
+ end
110
+
111
+ command :next do |c|
112
+ c.description = 'Skip to the next song in the queue'
113
+ c.action do
114
+ @user.player.next
115
+ print_current_song
116
+ end
117
+ end
118
+
119
+ command :previous do |c|
120
+ c.description = 'Skip the the previous song in the queue'
121
+ c.action do
122
+ @user.player.previous
123
+ print_current_song
124
+ end
125
+ end
126
+
127
+ command :restart do |c|
128
+ c.description = 'Restart the currently playing song'
129
+ c.action do
130
+ @user.player.seek 0
131
+ print_current_song
132
+ end
133
+ end
134
+
135
+ command :seek do |c|
136
+ c.description = 'Seek to the specified time in the current song'
137
+ c.action do |args, option|
138
+ @user.player.seek(time_to_ms(args[0]))
139
+ print_current_song
140
+ end
141
+ end
142
+
143
+ default_command :playing
144
+ alias_command :pause, :play
145
+ alias_command :back, :previous
146
+ alias_command :skip, :next
147
+
148
+ run!
149
+ end
150
+
151
+ private
152
+
153
+ def print_current_song
154
+ playing = @user.player.currently_playing
155
+ puts ' Now Playing:'
156
+ puts " #{playing.name} - #{playing.artists.first.name} - "\
157
+ "#{duration_to_s(@user.player.progress)}/#{duration_to_s(playing.duration_ms)}"\
158
+ "#{" - ♥" if @user.saved_tracks?([playing]).first}"
159
+ end
160
+
161
+ def exit_with_message(message, code = 1)
162
+ puts message
163
+ exit code
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shellify
4
+ class Config
5
+ attr_reader :client_id, :client_secret, :config_dir
6
+
7
+ CONFIG_DIR = ENV['HOME'] + '/.config/shellify'
8
+ CONFIG_FILE = CONFIG_DIR + '/config.json'
9
+
10
+ def initialize
11
+ @config_dir = CONFIG_DIR
12
+ @config_file = CONFIG_FILE
13
+ create_config_file
14
+ load_config
15
+ RSpotify.authenticate(@client_id, @client_secret)
16
+ end
17
+
18
+ private
19
+
20
+ def load_config
21
+ JSON.parse(File.read(CONFIG_FILE)).each_pair { |k,v| instance_variable_set("@#{k}", v) }
22
+ end
23
+
24
+ def create_config_file
25
+ return if File.exists?(CONFIG_FILE)
26
+
27
+ FileUtils.mkdir_p(CONFIG_DIR)
28
+ FileUtils.touch(CONFIG_FILE)
29
+ write_default_config
30
+ end
31
+
32
+ def write_default_config
33
+ File.open(CONFIG_FILE, 'w') do |file|
34
+ file.write(JSON.pretty_generate({client_id: '', client_secret: ''}))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shellify
4
+ class User < RSpotify::User
5
+
6
+ USER_FILE = '/spotify_user.json'
7
+
8
+ def initialize(config_dir)
9
+ @config_dir = config_dir
10
+ create_user_file
11
+ @spotify_user = load_persisted_user
12
+ super({
13
+ 'credentials' => {
14
+ 'token' => @spotify_user.token,
15
+ 'refresh_token' => @spotify_user.refresh_token,
16
+ 'access_refresh_callback' => access_refresh_callback,
17
+ },
18
+ 'id' => @spotify_user.id,
19
+ })
20
+ end
21
+
22
+ private
23
+
24
+ def load_persisted_user
25
+ OpenStruct.new(JSON.parse(File.read(@config_dir + USER_FILE)))
26
+ end
27
+
28
+ def persist_user(access_token)
29
+ @spotify_user.token = access_token
30
+
31
+ File.open(@config_dir + USER_FILE, 'w') do |file|
32
+ file.write(JSON.pretty_generate(@spotify_user.to_h))
33
+ end
34
+ end
35
+
36
+ def access_refresh_callback
37
+ Proc.new do |new_access_token, _token_lifetime|
38
+ persist_user(new_access_token)
39
+ end
40
+ end
41
+
42
+ def create_user_file
43
+ return if File.exists?(@config_dir + USER_FILE)
44
+
45
+ FileUtils.mkdir_p(CONFIG_DIR)
46
+ FileUtils.touch(@config_dir + USER_FILE)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'time'
4
+
5
+ module Shellify
6
+ module Utils
7
+ def duration_to_s(duration)
8
+ secs, _millis = duration.divmod(1000)
9
+ mins, secs = secs.divmod(60)
10
+ hours, mins = mins.divmod(60)
11
+ hours = nil if hours.zero?
12
+ [hours, mins, secs].compact.map { |s| s.to_s.rjust(2, '0') }.join(':')
13
+ end
14
+
15
+ def time_to_ms(time)
16
+ time.split(':').map { |a| a.to_i }.inject(0) { |a, b| a * 60 + b} * 1000
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shellify
4
+ VERSION = '1.0.0'
5
+ end
data/lib/shellify.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'json'
5
+ require 'rspotify'
6
+ require 'shellify/version'
7
+
8
+ module Shellify
9
+ autoload :Cli, 'shellify/cli'
10
+ autoload :Config, 'shellify/config'
11
+ autoload :User, 'shellify/user'
12
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shellify
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Derek Povah
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: commander
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.6.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.6.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspotify
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.11.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.11.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 13.0.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 13.0.1
69
+ description:
70
+ email:
71
+ - derek@derekpovah.com
72
+ executables:
73
+ - shellify
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - LICENSE.txt
78
+ - README.md
79
+ - exe/shellify
80
+ - lib/shellify.rb
81
+ - lib/shellify/cli.rb
82
+ - lib/shellify/config.rb
83
+ - lib/shellify/user.rb
84
+ - lib/shellify/utils.rb
85
+ - lib/shellify/version.rb
86
+ homepage: https://github.com/derekpovah/shellify
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ allowed_push_host: https://rubygems.org
91
+ homepage_uri: https://github.com/derekpovah/shellify
92
+ source_code_uri: https://github.com/derekpovah/shellify
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: 2.4.0
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.3.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Use Spotify from the command line
112
+ test_files: []