/
account.ts
209 lines (177 loc) · 4.48 KB
/
account.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import Client from './client'
import { requestJSON, requestXML, RequestOptions } from './utils/request'
import { parseResourceContainer } from './types/resources'
import { parseUser } from './types/user'
import { parsePin } from './types/pin'
import { parseDeviceList } from './types/device'
import { parseSyncList } from './types/sync-list'
const PLEX_API = 'https://plex.tv'
/**
* A plex.tv account
*
* @class Account
* @param {string} [authToken] Plex auth token
*/
export default class Account {
public client: Client
public authToken: string
constructor (client: Client, authToken?: string) {
this.client = client
this.authToken = authToken
}
/**
* Headers we need to send to Plex whenever we make a request
*
* @private
*/
headers (): Record<string, string> {
return {
...this.client.headers(),
'X-Plex-Token': this.authToken || undefined,
}
}
/**
* Make a JSON request to the Plex.tv API
*
* @private
*/
fetch (path: string, options: RequestOptions = {}) {
const url = PLEX_API + path
return requestJSON(url, {
...options,
headers: {
...options.headers,
...this.headers(),
},
})
}
/**
* Make an XML request to the Plex.tv API
*
* @private
*/
fetchXML (path: string, options: RequestOptions = {}) {
const url = PLEX_API + path
return requestXML(url, {
...options,
headers: {
...options.headers,
...this.headers(),
},
})
}
/**
* Log in to a Plex account
*
* @param {string} username
* @param {string} password
* @returns {Promise} - User info
*/
async authenticate (username: string, password: string) {
const formData = new URLSearchParams()
formData.append('login', username)
formData.append('password', password)
formData.append('rememberMe', 'true')
// we use requestJSON instead of this.fetch because if you send
// the X-Plex-Token header it won't actually switch accounts.
const res = await requestJSON(`${PLEX_API}/api/v2/users/signin`, {
method: 'POST',
headers: {
...this.client.headers(),
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
body: formData,
})
const user = await parseUser(res)
this.authToken = user.authToken
return user
}
async requestPin () {
const res = await this.fetch('/pins.json', { method: 'POST' })
const pin = parsePin(res)
return pin
}
async checkPin (pinId: string) {
const res = await this.fetch(`/pins/${pinId}.json`)
const pin = parsePin(res)
if (pin.authToken != null) {
this.authToken = pin.authToken
}
return pin
}
/**
* Fetch information about the currently logged in user
*
* @returns {Promise}
*/
async info () {
const res = await this.fetch('/api/v2/user')
return parseUser(res)
}
/**
* Fetch a list of servers and clients connected to this plex account.
* Useful for figuring out which server to connect to.
*
* Note: this API doesn't support JSON at the moment, so we need
* to parse the response as XML.
*
* @returns {Promise}
*/
async resources () {
const res = await this.fetchXML('/api/resources', {
searchParams: {
includeHttps: '1',
includeRelay: '1',
},
})
return parseResourceContainer(res)
}
/**
* Fetch a list of all the servers
*
* @returns {Promise}
*/
async servers () {
const resources = await this.resources()
return {
...resources,
devices: resources.devices.filter((device) =>
device.provides.includes('server'),
),
}
}
/**
* Fetch a list of devices attached to an account
*
* @returns {Promise}
*/
async devices () {
const res = await this.fetchXML('/devices.xml')
return parseDeviceList(res)
}
/**
* Remove a device from the account
*/
async removeDevice (deviceId: string) {
await this.fetch(`/devices/${deviceId}.json`, {
method: 'DELETE',
})
return true
}
/**
* Fetch a list of items that are syncing to a device
*/
async syncItems (deviceId: string) {
const res = await this.fetchXML(`/devices/${deviceId}/sync_items`)
return parseSyncList(res)
}
/**
* Remove an item from the sync list
*/
async removeSyncItem (deviceId: string, syncItemId: string) {
await this.fetch(`/devices/${deviceId}/sync_items/${syncItemId}`, {
method: 'DELETE',
})
return true
}
}