-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.mjs
209 lines (193 loc) · 6.03 KB
/
sync.mjs
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 { program } from "commander";
import chalk from "chalk";
import inquirer from "inquirer";
import fetch from "node-fetch";
import Trakt from "trakt.tv";
import { createSpinner } from "nanospinner";
program
.name("Trakt Sync CLI")
.description("Synchronize your watch history from Simkl to Trakt.")
.version("1.0.0");
const validateInput = (input) => !!input || "This field cannot be empty!";
async function getSimklWatched(clientId) {
const spinner = createSpinner("Authorizing Simkl...").start();
const { user_code, verification_url } = await fetch(
`https://api.simkl.com/oauth/pin?client_id=${clientId}`
).then((res) => res.json());
console.log(
chalk.cyan(
`Please authorize the Simkl application by visiting: ${verification_url} and using this code: ${user_code}`
)
);
await inquirer.prompt({
type: "confirm",
name: "confirmed",
message: "Hit Enter once you have authorized.",
});
const { access_token } = await fetch(
`https://api.simkl.com/oauth/pin/${user_code}?client_id=${clientId}`
).then((res) => res.json());
const data = await fetch(
"https://api.simkl.com/sync/all-items/?extended=full&episode_watched_at=yes",
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${access_token}`,
"simkl-api-key": clientId,
},
}
).then((res) => res.json());
spinner.success({ text: "Simkl authorization successful." });
return data;
}
async function main() {
const answers = await inquirer.prompt([
{
type: "input",
message:
"Please input your Simkl client ID (get it from https://simkl.com/settings/developer/new by creating a new application)\n",
name: "simkl_client_id",
validate,
},
{
type: "input",
message:
"Please input your Trakt client ID (get it from https://trakt.tv/oauth/applications by creating a new application):\n",
name: "client_id",
validate,
},
{
type: "input",
message:
"Please input your Trakt client secret (you get it from the same place you got the client ID):\n",
name: "client_secret",
validate,
},
{
type: "confirm",
message: "Do you want to delete your previous Trakt history? ",
name: "remove_previous",
default: false,
},
]);
const spinner = createSpinner("Fetching watch history...").start();
const watched = await getSimklWatched(answers.simkl_client_id);
spinner.success({ text: "Watch history fetched successfully." });
const trakt = new Trakt({
client_id: answers.client_id,
client_secret: answers.client_secret,
});
try {
const poll = await trakt.get_codes();
console.log(
chalk.blue(
`Authorize the Trakt application via: ${poll.verification_url} using this code: ${poll.user_code}`
)
);
await trakt.poll_access(poll);
spinner.success({ text: "Trakt authorization successful." });
if (answers.remove_previous) {
try {
console.log("Getting previous watch history...");
const movies = await trakt.sync
.watched({ type: "movies" })
.then((movie) => movie.map((mv) => mv.movie));
const shows = await trakt.sync
.watched({ type: "shows" })
.then((show) => show.map((sh) => sh.show));
console.log(
`Removing ${movies.length} movies and ${shows.length} shows from your Trakt watchlist...`
);
await trakt.sync.history.remove({ movies, shows }).then((res) => {
console.log(
`Succesfully removed ${res.deleted.movies} movies and ${res.deleted.episodes} episodes your watch history.`
);
sync(watched);
});
} catch {
const answer = await inquirer.prompt([
{
name: "confirm",
message:
"The watch history could not be removed for various reasons (maybe your watch history is too big), continue syncing?",
default: true,
type: "boolean",
},
]);
if (answer) await sync(watched);
else process.exit(0);
}
}
await sync(watched, trakt);
} catch (error) {
spinner.error({ text: `Error: ${error.message}` });
}
}
async function sync(watched, trakt) {
const traktObject = {
shows: [],
movies: [],
};
watched.shows.forEach((show) => {
if (!show.last_watched_at) return;
traktObject.shows.push({
watched_at: show.last_watched_at,
title: show.show.title,
year: show.show.year,
seasons: show.seasons,
ids: {
mal: show.show.ids.mal,
imdb: show.show.ids.imdb,
tmdb: show.show.ids.tmdb,
anidb: show.show.ids.anidb,
},
});
});
watched.movies.forEach((movie) => {
if (!movie.last_watched_at) return;
traktObject.movies.push({
watched_at: movie.last_watched_at,
title: movie.movie.title,
year: movie.movie.year,
ids: {
slug: movie.movie.ids.slug,
imdb: movie.movie.ids.imdb,
tmdb: movie.movie.ids.tmdb,
},
});
});
watched.anime.forEach((anime) => {
if (!anime.last_watched_at) return;
traktObject.shows.push({
watched_at: anime.last_watched_at,
title: anime.show.title,
year: anime.show.year,
seasons: anime.seasons,
ids: {
mal: anime.show.ids.mal,
imdb: anime.show.ids.imdb,
tmdb: anime.show.ids.tmdb,
anidb: anime.show.ids.anidb,
},
});
});
console.log(
`Syncing ${traktObject.shows.length} shows (incl. anime) and ${traktObject.movies.length} movies to your Trakt account...`
);
await trakt.sync.history
.add(traktObject)
.then((res) =>
console.log(
`Successfully added ${res.added.movies} movies and ${res.added.episodes} episodes to your Trakt watch history!`
)
);
}
function validate(string) {
if (!string) return "Please input the required string!";
else return true;
}
program
.command("sync")
.description("Sync watch history from Simkl to Trakt")
.action(main);
program.parse(process.argv);