upnext-adapter-spotify 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +224 -0
- package/dist/src/applescript.d.ts +100 -0
- package/dist/src/applescript.d.ts.map +1 -0
- package/dist/src/applescript.js +197 -0
- package/dist/src/applescript.js.map +1 -0
- package/dist/src/desktop.d.ts +100 -0
- package/dist/src/desktop.d.ts.map +1 -0
- package/dist/src/desktop.js +194 -0
- package/dist/src/desktop.js.map +1 -0
- package/dist/src/errors.d.ts +61 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +93 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/http.d.ts +50 -0
- package/dist/src/http.d.ts.map +1 -0
- package/dist/src/http.js +113 -0
- package/dist/src/http.js.map +1 -0
- package/dist/src/index.d.ts +34 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +28 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/metadata.d.ts +39 -0
- package/dist/src/metadata.d.ts.map +1 -0
- package/dist/src/metadata.js +130 -0
- package/dist/src/metadata.js.map +1 -0
- package/dist/src/ref.d.ts +29 -0
- package/dist/src/ref.d.ts.map +1 -0
- package/dist/src/ref.js +150 -0
- package/dist/src/ref.js.map +1 -0
- package/dist/src/sampler.d.ts +68 -0
- package/dist/src/sampler.d.ts.map +1 -0
- package/dist/src/sampler.js +96 -0
- package/dist/src/sampler.js.map +1 -0
- package/dist/src/uri.d.ts +31 -0
- package/dist/src/uri.d.ts.map +1 -0
- package/dist/src/uri.js +92 -0
- package/dist/src/uri.js.map +1 -0
- package/dist/src/watch.d.ts +35 -0
- package/dist/src/watch.d.ts.map +1 -0
- package/dist/src/watch.js +131 -0
- package/dist/src/watch.js.map +1 -0
- package/dist/src/web.d.ts +115 -0
- package/dist/src/web.d.ts.map +1 -0
- package/dist/src/web.js +300 -0
- package/dist/src/web.js.map +1 -0
- package/package.json +42 -0
- package/src/applescript.ts +241 -0
- package/src/desktop.ts +250 -0
- package/src/errors.ts +119 -0
- package/src/http.ts +159 -0
- package/src/index.ts +51 -0
- package/src/metadata.ts +167 -0
- package/src/ref.ts +145 -0
- package/src/sampler.ts +173 -0
- package/src/uri.ts +104 -0
- package/src/watch.ts +145 -0
- package/src/web.ts +343 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# upnext-adapter-spotify
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/upnext-adapter-spotify)
|
|
4
|
+
[](https://github.com/tothienbao6a0/upnext/blob/main/LICENSE)
|
|
5
|
+
|
|
6
|
+
Plays Spotify for [**upnext-core**](https://www.npmjs.com/package/upnext-core) —
|
|
7
|
+
**two backends, with honestly different capabilities.**
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i upnext-core upnext-adapter-spotify
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Which one you want
|
|
14
|
+
|
|
15
|
+
| | `SpotifyDesktopAdapter` | `SpotifyWebAdapter` |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| **Needs** | the Spotify app, on macOS | an OAuth token + Premium + a live device |
|
|
18
|
+
| **Credentials** | none at all | your own, via `getAccessToken` |
|
|
19
|
+
| **Search** | ❌ | ✅ |
|
|
20
|
+
| **Runs on** | macOS | anywhere Node does |
|
|
21
|
+
| **Setup** | none | register an app with Spotify |
|
|
22
|
+
|
|
23
|
+
The desktop one works right now, on your machine, with nothing to sign up for:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { Runtime } from 'upnext-core';
|
|
27
|
+
import { SpotifyDesktopAdapter } from 'upnext-adapter-spotify';
|
|
28
|
+
|
|
29
|
+
const runtime = new Runtime({ adapters: [new SpotifyDesktopAdapter()] });
|
|
30
|
+
|
|
31
|
+
runtime.enqueue('https://open.spotify.com/track/6f3Slt0GbA2bPZlz0aIFXN');
|
|
32
|
+
runtime.enqueue('spotify:track:1OWBh1eVxUdA1Z6UA8r4nh');
|
|
33
|
+
|
|
34
|
+
await runtime.play();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The web one costs setup and buys reach:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { SpotifyWebAdapter } from 'upnext-adapter-spotify';
|
|
41
|
+
|
|
42
|
+
new SpotifyWebAdapter({
|
|
43
|
+
// Your OAuth, your storage, your refresh. This library never runs a flow,
|
|
44
|
+
// holds a client id, or opens a browser.
|
|
45
|
+
getAccessToken: () => myTokenStore.current(),
|
|
46
|
+
deviceId: 'optional-specific-speaker',
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Registering **both** is reasonable. They score the same on a Spotify URI, so the
|
|
51
|
+
runtime uses one and falls through to the other when it cannot deliver — the
|
|
52
|
+
desktop app while it is open, the Web API when it is not.
|
|
53
|
+
|
|
54
|
+
## Two adapters, one service, different truths
|
|
55
|
+
|
|
56
|
+
This is the clearest example in the project of why capabilities belong to an
|
|
57
|
+
*adapter* rather than to a *service*:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
{ // desktop web
|
|
61
|
+
endOfTrack: 'event', // 'event' 'event'
|
|
62
|
+
position: 'authoritative',
|
|
63
|
+
externalControl: true, // ← both: a human shares this backend with you
|
|
64
|
+
seek: true, pause: true, volume: true,
|
|
65
|
+
search: false, // ← desktop true ← web
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`search: false` is the load-bearing one. Spotify's AppleScript dictionary cannot
|
|
70
|
+
search a catalogue. That could be faked — scrape something, guess — and then
|
|
71
|
+
every `resolve` of a title would be a coin flip dressed up as a lookup. **An
|
|
72
|
+
adapter that says it cannot do a thing is correct and slightly limited; one that
|
|
73
|
+
says it can and then does it badly is broken.**
|
|
74
|
+
|
|
75
|
+
So the desktop adapter scores `0` on anything that is not already a Spotify link,
|
|
76
|
+
and the entry goes to a backend that can actually find it.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
runtime.can('search'); // false on desktop, true on web — whichever is loaded
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## When a human takes over
|
|
83
|
+
|
|
84
|
+
Spotify is the first backend in this project that somebody else can also touch.
|
|
85
|
+
It has its own transport bar, its own queue, and a listener holding a phone who
|
|
86
|
+
is entirely entitled to press next.
|
|
87
|
+
|
|
88
|
+
The hard part is that **"the track I loaded is not the track that is playing"
|
|
89
|
+
has two opposite causes**:
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
our track ended, Spotify rolled on a person hit next
|
|
93
|
+
│ │
|
|
94
|
+
▼ ▼
|
|
95
|
+
advance OUR queue the human wins
|
|
96
|
+
(event: ended) (event: external → adopted)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Get it backwards and the library breaks in the most visible way there is: either
|
|
100
|
+
an agent's carefully built queue silently defers to Spotify's autoplay after
|
|
101
|
+
every single song, or a listener who picks a song has it yanked away.
|
|
102
|
+
|
|
103
|
+
What separates them is **where the playhead was on the previous reading**. A
|
|
104
|
+
rollover can only happen at the end of a track. The core cannot make that call —
|
|
105
|
+
by the time it sees the mismatch, the evidence is gone — so the adapter keeps it.
|
|
106
|
+
That decision lives in [`sampler.ts`](./src/sampler.ts) as a pure function, and
|
|
107
|
+
every branch of it is a test.
|
|
108
|
+
|
|
109
|
+
Which way it goes is still the runtime's call, via `desyncPolicy`:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
new Runtime({ desyncPolicy: 'adopt' }); // default — the human wins
|
|
113
|
+
new Runtime({ desyncPolicy: 'correct' }); // put our track back
|
|
114
|
+
new Runtime({ desyncPolicy: 'ignore' }); // report it, change nothing
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## What the desktop adapter will never do
|
|
118
|
+
|
|
119
|
+
**Launch Spotify behind your back.** Reading state is guarded by
|
|
120
|
+
`if application "Spotify" is running`, because a bare `tell application` *starts*
|
|
121
|
+
the app — and a status poll running every second would boot a music player onto
|
|
122
|
+
the machine of someone who never opened one. Only an explicit `play()` may open
|
|
123
|
+
it, and it does not steal focus when it does.
|
|
124
|
+
|
|
125
|
+
**Quit your music player.** Spotify's dictionary has no stop that does not quit,
|
|
126
|
+
so `stop()` pauses. The queue moving on to a podcast is not a reason to close
|
|
127
|
+
somebody's app.
|
|
128
|
+
|
|
129
|
+
## Playlists and albums
|
|
130
|
+
|
|
131
|
+
A container is not a queue entry — this runtime holds one item at a time and owns
|
|
132
|
+
the ordering itself. So expanding is explicit, and on purpose: you get to see and
|
|
133
|
+
filter the list before it becomes thirty queue entries.
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
const tracks = await spotify.expandContext('spotify:playlist:37i9dQZF1DXcBWIGoYBM5M');
|
|
137
|
+
runtime.enqueueMany(tracks.slice(0, 10));
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Failures worth telling apart
|
|
141
|
+
|
|
142
|
+
Every error is a `SpotifyError` with a `reason`, because "your token expired" and
|
|
143
|
+
"your phone went to sleep" are both a failed play and an agent that cannot tell
|
|
144
|
+
them apart will nag for a login when all it needed to do was wait.
|
|
145
|
+
|
|
146
|
+
| `reason` | what it means | what to do |
|
|
147
|
+
|---|---|---|
|
|
148
|
+
| `unauthorized` | no usable session, or macOS Automation was declined | get a token / grant permission |
|
|
149
|
+
| `premium-required` | the account cannot control playback | nothing — it's a free account |
|
|
150
|
+
| `no-device` | nothing to play *on* | open Spotify somewhere |
|
|
151
|
+
| `rate-limited` | too many calls; `retryAfterMs` when Spotify says | wait |
|
|
152
|
+
| `not-found` | not in the catalogue, or not in this market | try another source |
|
|
153
|
+
| `unavailable` | this backend cannot run here at all | use the other adapter |
|
|
154
|
+
|
|
155
|
+
A backend that cannot start is reported rather than thrown — it is excluded from
|
|
156
|
+
selection and shows up in `getState().adapters` as `available: false` with the
|
|
157
|
+
reason. A host on Linux gets one clear message at startup instead of discovering
|
|
158
|
+
it one failed track at a time.
|
|
159
|
+
|
|
160
|
+
## What the Web API needs from you
|
|
161
|
+
|
|
162
|
+
- **Scopes:** `user-read-playback-state` and `user-modify-playback-state`.
|
|
163
|
+
- **Premium.** Every playback-control endpoint is Premium-only. There is nothing
|
|
164
|
+
an adapter can do about that, so a free account gets `premium-required` rather
|
|
165
|
+
than a mystery.
|
|
166
|
+
- **Somewhere to play.** The Web API *commands* a device, it is not one. Spotify
|
|
167
|
+
has to be open somewhere, or `deviceId` has to name something real.
|
|
168
|
+
|
|
169
|
+
## Options
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
new SpotifyDesktopAdapter({
|
|
173
|
+
id: 'spotify-desktop',
|
|
174
|
+
sampleIntervalMs: 1000, // how often to read the app while something plays
|
|
175
|
+
lookup: null, // turn off the credential-free metadata fill-in
|
|
176
|
+
osascript: myRunner, // injected for tests
|
|
177
|
+
scheduler: myScheduler,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
new SpotifyWebAdapter({
|
|
181
|
+
getAccessToken, // required
|
|
182
|
+
id: 'spotify-web',
|
|
183
|
+
deviceId: '…',
|
|
184
|
+
market: 'US', // so results are things this listener can play
|
|
185
|
+
sampleIntervalMs: 2000, // each sample is a real call against a shared limit
|
|
186
|
+
fetch: myFetch, // injected for tests
|
|
187
|
+
});
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### About `lookup`
|
|
191
|
+
|
|
192
|
+
The desktop adapter has no catalogue access, so a queue of share links would show
|
|
193
|
+
a column of raw URIs until each one started playing. To fill in titles it reads
|
|
194
|
+
Spotify's public embed page, which needs no credentials — but **is not a
|
|
195
|
+
documented API**, and Spotify can change it whenever it likes.
|
|
196
|
+
|
|
197
|
+
So it is a nicety, never a dependency: failures are silent, results are cached,
|
|
198
|
+
lookups are skipped when the ref already has what it needs, and `lookup: null`
|
|
199
|
+
turns it off. Nothing about whether a track *plays* runs through it.
|
|
200
|
+
|
|
201
|
+
## Testing
|
|
202
|
+
|
|
203
|
+
Everything is injectable, so all 77 tests run with **no macOS, no Spotify, no
|
|
204
|
+
account and no network** — including a captured reading from a real running copy
|
|
205
|
+
of the app, kept verbatim so a change to the wire format breaks a test rather
|
|
206
|
+
than a user.
|
|
207
|
+
|
|
208
|
+
## A third door
|
|
209
|
+
|
|
210
|
+
If you want queue-reading and search with no app registration at all, there is
|
|
211
|
+
[spogo](https://github.com/openclaw/spogo), a CLI that presents itself as the
|
|
212
|
+
Spotify web player using your browser's cookies. It is deliberately **not** a
|
|
213
|
+
dependency here: it works through a rotating secret that Spotify actively
|
|
214
|
+
changes, and pinning a published package to that means the treadmill lands on
|
|
215
|
+
this issue tracker.
|
|
216
|
+
|
|
217
|
+
The right home for it is
|
|
218
|
+
[`upnext-adapter-process`](https://www.npmjs.com/package/upnext-adapter-process),
|
|
219
|
+
which runs an adapter as a subprocess speaking JSON — so the moving target stays
|
|
220
|
+
on the far side of a process boundary.
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
Apache-2.0 · [source](https://github.com/tothienbao6a0/upnext/tree/main/packages/adapter-spotify)
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Talking to the Spotify desktop app through its AppleScript dictionary.
|
|
3
|
+
*
|
|
4
|
+
* The script text and the parsing are separated on purpose, and both are
|
|
5
|
+
* exported. AppleScript is a string until the moment it runs, so the part most
|
|
6
|
+
* likely to break — a field order that drifts, a property spelled wrong — is
|
|
7
|
+
* exactly the part a type checker cannot see. Keeping the parser pure means the
|
|
8
|
+
* wire format has real tests on a machine with no Spotify, no macOS and no
|
|
9
|
+
* network, which is where this package's CI runs.
|
|
10
|
+
*/
|
|
11
|
+
/** ASCII unit separator. Written as an escape because the character is
|
|
12
|
+
* invisible in an editor, and chosen over a tab or a pipe because a track title
|
|
13
|
+
* is free text — anything a person might plausibly type would split a title in
|
|
14
|
+
* half. */
|
|
15
|
+
export declare const FIELD = "\u001F";
|
|
16
|
+
/** How long any one script may take. A beachballing player, or a first run
|
|
17
|
+
* waiting on the macOS Automation consent prompt, must not hold a sample
|
|
18
|
+
* forever and let the next one stack behind it. */
|
|
19
|
+
export declare const SCRIPT_TIMEOUT_MS = 5000;
|
|
20
|
+
/** The one dependency on the outside world, injectable so every decision this
|
|
21
|
+
* package makes is testable without a Mac, a Spotify account, or an app. */
|
|
22
|
+
export type Osascript = (script: string) => Promise<string>;
|
|
23
|
+
export declare function runOsascript(script: string): Promise<string>;
|
|
24
|
+
/**
|
|
25
|
+
* One reading of the desktop app.
|
|
26
|
+
*
|
|
27
|
+
* `running: false` is a first-class answer, not an error. Spotify not being
|
|
28
|
+
* open is the ordinary state of most machines most of the time, and a backend
|
|
29
|
+
* that treats it as a failure would fill a host's logs with news it cannot act
|
|
30
|
+
* on.
|
|
31
|
+
*/
|
|
32
|
+
export interface Sample {
|
|
33
|
+
running: boolean;
|
|
34
|
+
status: 'playing' | 'paused' | 'idle';
|
|
35
|
+
positionMs: number;
|
|
36
|
+
durationMs: number | null;
|
|
37
|
+
/** What Spotify says it is playing, as a `spotify:` URI. */
|
|
38
|
+
nativeUri: string | null;
|
|
39
|
+
/** 0..1, or null when the app would not say. */
|
|
40
|
+
volume: number | null;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Read the app's state without ever launching it.
|
|
44
|
+
*
|
|
45
|
+
* `if application "Spotify" is running` is the load-bearing line. A bare
|
|
46
|
+
* `tell application "Spotify"` *launches* Spotify, so a sampler running every
|
|
47
|
+
* second would boot a music player onto the machine of someone who never opened
|
|
48
|
+
* one — and would do it on a poll the user never asked for. The guard is what
|
|
49
|
+
* makes this safe to run on a timer.
|
|
50
|
+
*
|
|
51
|
+
* Every property sits behind its own `try` so that one that errors — and
|
|
52
|
+
* `current track` errors whenever nothing is loaded — costs its own field
|
|
53
|
+
* rather than the whole reading.
|
|
54
|
+
*/
|
|
55
|
+
export declare function stateScript(): string;
|
|
56
|
+
/**
|
|
57
|
+
* Turn a reading into a `Sample`, or `null` if the text is not one.
|
|
58
|
+
*
|
|
59
|
+
* Anything malformed answers null rather than a half-filled sample: a sample
|
|
60
|
+
* with a NaN playhead would be read as a real position and could end a track
|
|
61
|
+
* that is still playing, where a missing sample simply means this tick learned
|
|
62
|
+
* nothing and the next one will.
|
|
63
|
+
*/
|
|
64
|
+
export declare function parseSample(raw: string): Sample | null;
|
|
65
|
+
/**
|
|
66
|
+
* Start a specific track.
|
|
67
|
+
*
|
|
68
|
+
* This is the one script allowed to launch Spotify, because it only ever runs
|
|
69
|
+
* as the direct result of something asking for playback. `activate` is
|
|
70
|
+
* deliberately absent: starting a song should not take the foreground away from
|
|
71
|
+
* whatever the person is actually looking at.
|
|
72
|
+
*
|
|
73
|
+
* A start offset needs the pause. `play track` returns as soon as the command
|
|
74
|
+
* is accepted, not when the track is loaded, and moving the playhead of a track
|
|
75
|
+
* that has not loaded yet either errors or is silently discarded — so the seek
|
|
76
|
+
* is retried until it takes, which costs nothing in the common case where
|
|
77
|
+
* `startAtMs` is zero and none of this runs.
|
|
78
|
+
*/
|
|
79
|
+
export declare function playTrackScript(nativeUri: string, startAtMs?: number): string;
|
|
80
|
+
/**
|
|
81
|
+
* Any other verb, wrapped so it cannot launch the app.
|
|
82
|
+
*
|
|
83
|
+
* There is nothing to pause, seek or re-level in an app that is not open, and
|
|
84
|
+
* launching one to tell it to be quiet would be absurd — so unlike
|
|
85
|
+
* `playTrackScript`, everything here is behind the running guard.
|
|
86
|
+
*/
|
|
87
|
+
export declare function commandScript(body: string): string;
|
|
88
|
+
export declare const commands: {
|
|
89
|
+
readonly play: "play";
|
|
90
|
+
/**
|
|
91
|
+
* Spotify's dictionary has no stop that does not quit the app, and quitting
|
|
92
|
+
* someone's music player because a queue moved to a different source would be
|
|
93
|
+
* far ruder than leaving it paused. `stop` therefore means pause here, and
|
|
94
|
+
* that is the honest description of what happens.
|
|
95
|
+
*/
|
|
96
|
+
readonly pause: "pause";
|
|
97
|
+
readonly seek: (positionMs: number) => string;
|
|
98
|
+
readonly volume: (volume: number) => string;
|
|
99
|
+
};
|
|
100
|
+
//# sourceMappingURL=applescript.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"applescript.d.ts","sourceRoot":"","sources":["../../src/applescript.ts"],"names":[],"mappings":"AAIA;;;;;;;;;GASG;AAEH;;;WAGW;AACX,eAAO,MAAM,KAAK,WAAW,CAAC;AAE9B;;mDAEmD;AACnD,eAAO,MAAM,iBAAiB,OAAQ,CAAC;AAEvC;4EAC4E;AAC5E,MAAM,MAAM,SAAS,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;AAE5D,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAkB5D;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,MAAM;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,SAAS,GAAG,QAAQ,GAAG,MAAM,CAAC;IACtC,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,4DAA4D;IAC5D,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,gDAAgD;IAChD,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,IAAI,MAAM,CA8BpC;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAwBtD;AAqBD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,SAAI,GAAG,MAAM,CAkBxE;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAMlD;AAED,eAAO,MAAM,QAAQ;;IAEnB;;;;;OAKG;;gCAEgB,MAAM;8BACR,MAAM;CAEf,CAAC"}
|