-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspotiplay.rb
160 lines (138 loc) · 4.44 KB
/
spotiplay.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
require "hallon"
require "hallon-openal"
class SpotiPlay
attr_accessor :player, :playlist, :index, :playing, :external_playlist, :local_playlist
def initialize (username, password)
# Setting up variables
self.playing = false
self.playlist = []
self.index = 0
self.local_playlist = Time.new.strftime('%Y-%m-%d.txt')
self.external_playlist = nil
# This is a quick sanity check, to make sure we have all the necessities in order.
appkey_path = File.expand_path('./spotify_appkey.key')
unless File.exists?(appkey_path)
abort <<-ERROR
Your Spotify application key could not be found at the path:
#{appkey_path}
Please adjust the path in examples/common.rb or put your application key in:
#{appkey_path}
You may download your application key from:
https://developer.spotify.com/en/libspotify/application-key/
ERROR
end
hallon_appkey = IO.read(appkey_path)
# Make sure the credentials are there. We don’t want to go without them.
if username.empty? or password.empty?
abort "Sorry, you must supply both username and password for Hallon to be able to log in."
end
session = Hallon::Session.initialize(hallon_appkey) do
on(:log_message) do |message|
puts "[LOG] #{message}"
end
on(:credentials_blob_updated) do |blob|
puts "[BLOB] #{blob}"
end
on(:connection_error) do |error|
Hallon::Error.maybe_raise(error)
end
on(:logged_out) do
abort "[FAIL] Logged out!"
end
end
session.login!(username, password)
self.player = Hallon::Player.new(Hallon::OpenAL)
end
#TODO: INFO, Remove console output
def p_play (track)
Thread.new {
puts "Playing: #{track.name} by #{track.artist.name}"
self.playlist[index][:status] = "playing"
self.player.play!(track)
puts "Song ended: #{track.name} by #{track.artist.name}"
self.playlist[index][:status] = "done"
if self.playlist.empty?
puts "No more songs in playlist, trying to open external playlist"
unless self.external_playlist.nil?
puts "No more songs in playlist, playing random song from external playlist"
self.p_play (self.external_playlist.tracks[rand(self.external_playlist.tracks.size)])
else
puts "else"
end
self.player.stop
else
puts "Starting nex song"
new_next
end
}
end
def new_next
self.playlist[index][:status] = "done"
puts "#{self.index}/#{self.playlist.size}"
puts "index ++"
self.index += 1
puts "#{self.index}/#{self.playlist.size}"
while self.index != self.playlist.size and self.playlist[index][:status] == "removed"
puts "removed, index ++"
self.index += 1
end
puts "#{self.index}/#{self.playlist.size}"
unless self.index == self.playlist.size
self.p_play (self.playlist[self.index][:track])
else
puts "no more tracks in playlist, stopping playback"
self.playing = false
end
end
# Play the next song in the playlist
def p_next
unless self.playlist.empty?
if self.playing
puts "Next song"
self.playlist.shift
self.p_play (self.playlist.first[:track])
else
puts "player is not playing a song right now, starting playback"
self.playing = true
p_next
end
else
"no tracks in playlist, add some please"
end
end
def set_playlist (playlist = null)
puts "Spotithin.set_playlist"
self.external_playlist = playlist
#self.p_play (self.external_playlist.tracks[rand(self.external_playlist.tracks.size)])
end
def p_pause
self.player.pause
end
def p_resume
self.player.play
end
def play_pause
if self.playing
self.playing = false
self.player.pause
"pausing player"
else
self.paying = true
"un-pausing player"
end
end
# Adds a track to the current playlist, takes a hash. {:track=>Hallon::Track, :user=>String}
def add_to_playlist (item)
item[:status] = "queued"
self.playlist.push (item)
"Added #{item[:track].name} to the playlist!"
File.open("tmp/" + self.local_playlist, "a") do |f|
f.write (item[:track].name + " - " + item[:track].artist.name + "\n")
end
unless self.player.status == :playing
self.p_play (self.playlist[self.index][:track])
end
end
end