-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathTraktScrobble.ts
More file actions
95 lines (84 loc) · 1.99 KB
/
TraktScrobble.ts
File metadata and controls
95 lines (84 loc) · 1.99 KB
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
import { TraktItem } from '../models/TraktItem';
import { EventDispatcher } from '../services/Events';
import { Messaging } from '../services/Messaging';
import { RequestException, Requests } from '../services/Requests';
import { Shared } from '../services/Shared';
import { TraktApi } from './TraktApi';
export interface TraktScrobbleData {
movie?: {
ids: {
trakt: number;
};
};
episode?: {
ids: {
trakt: number;
};
};
progress: number;
}
class _TraktScrobble extends TraktApi {
START: number;
PAUSE: number;
STOP: number;
paths: Record<number, string>;
constructor() {
super();
this.START = 1;
this.PAUSE = 2;
this.STOP = 3;
this.paths = {
[this.START]: '/start',
[this.PAUSE]: '/pause',
[this.STOP]: '/stop',
};
}
start = async (item: TraktItem): Promise<void> => {
if (!Shared.isBackgroundPage) {
await Messaging.toBackground({ action: 'start-scrobble' });
}
await this.send(item, this.START);
};
pause = async (item: TraktItem): Promise<void> => {
await this.send(item, this.PAUSE);
};
stop = async (item: TraktItem): Promise<void> => {
await this.send(item, this.STOP);
if (!Shared.isBackgroundPage) {
await Messaging.toBackground({ action: 'stop-scrobble' });
}
};
send = async (item: TraktItem, scrobbleType: number): Promise<void> => {
const path = this.paths[scrobbleType];
try {
const data = {} as TraktScrobbleData;
if (item.type === 'show') {
data.episode = {
ids: {
trakt: item.id,
},
};
} else {
data.movie = {
ids: {
trakt: item.id,
},
};
}
data.progress = item.progress;
await Requests.send({
url: `${this.SCROBBLE_URL}${path}`,
method: 'POST',
body: data,
});
await EventDispatcher.dispatch('SCROBBLE_SUCCESS', null, { item, scrobbleType });
} catch (err) {
await EventDispatcher.dispatch('SCROBBLE_ERROR', null, {
item,
scrobbleType,
error: err as RequestException,
});
}
};
}
export const TraktScrobble = new _TraktScrobble();