forked from fcannizzaro/spotify-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify-control.py
102 lines (73 loc) · 2.68 KB
/
spotify-control.py
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
import sublime_plugin
import _thread as th
import sublime
import time
from .py.api import api
from .py.server import listen
from .py.utils import settings, store, set_interval
def update():
status = api().currently_playing()
for view in sublime.active_window().views():
view.set_status('spotify', ' [%s] %s ♫ ' % (status['artist'], status['name']))
def on_track_selected(index):
if index < 0: return
tracks = [track['uri'] for track in settings('tracks')]
th.start_new_thread(api().play, (tracks[index:],))
def repeat_mode(value):
modes = ['track', 'context', 'off']
if isinstance(value, str):
return modes.index(value)
return modes[value]
def on_repeat_selected(index):
if index < 0: return
th.start_new_thread(api().repeat, (repeat_mode(index),))
def refresh_library():
spotify = api()
tracks = []
res = {}
while True:
res = spotify.tracks(res['offset'] + 50 if len(res) else 0)
if not res: break
tracks += [{key: item['track'][key] for key in ('uri', 'name')} for item in res['items']]
if not res['next']: break
if len(tracks):
settings('tracks', tracks)
def run(api):
global updating
if not api.access_token:
return listen(plugin_loaded)
if settings('spotify_status') in [True, None]:
updating = set_interval(update, 3)
refresh_library()
class SpotifyFlowCommand(sublime_plugin.TextCommand):
def exec(self, action):
spotify = api()
if action == 'tracks':
tracks = [track['name'] for track in settings('tracks')]
self.view.window().show_quick_panel(tracks, on_track_selected)
elif action == 'refresh':
refresh_library()
elif action == 'next':
spotify.next()
elif action == 'previous':
spotify.previous()
elif action == 'shuffle':
spotify.shuffle(not spotify.status['shuffle'])
elif action == 'repeat':
labels = ['repeat current', 'repeat all', 'no repeat']
if 'repeat' not in spotify.status: return
self.view.window().show_quick_panel(labels, on_repeat_selected, selected_index=repeat_mode(spotify.status['repeat']))
else:
if 'playing' in spotify.status and spotify.status['playing']:
spotify.pause()
else:
spotify.play()
update()
def run(self, edit, action='play/pause'):
th.start_new_thread(self.exec, (action,))
def plugin_loaded():
sublime.save_settings('Sublimify.sublime-settings')
th.start_new_thread(run, (api(),))
def plugin_unloaded():
if 'updating' in globals() and updating:
updating.cancel()