-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplex-9-to-trakt.php
More file actions
executable file
·443 lines (367 loc) · 12.6 KB
/
plex-9-to-trakt.php
File metadata and controls
executable file
·443 lines (367 loc) · 12.6 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/env php
<?php
// This script adds your shows from Plex 9 to Trakt, including seen/unseen status.
define('PLEX_URL', 'http://<IP of your Plex 9 media server>:32400');
define('TRAKT_APIKEY', '<your Trakt.tv API key>');
define('TRAKT_USERNAME', '<your Trakt.tv username>');
define('TRAKT_PASSWORD', '<your Trakt.tv password>');
// DO NOT MODIFY ANYTHING BELOW THIS LINE.
echo("\n\n=== Starting the import. This may take some time. ===\n");
ini_set('memory_limit', '512M');
set_time_limit(6000);
// Load in command line options
$options = getargs(array("m" => "movies-only", "t" => "tv-only", "w" => "watched", "l" => "library", "d" => "debug"));
// Get XML with sections from Plex
$sections_xml = simplexml_load_string(file_get_contents(PLEX_URL . '/library/sections'));
// Loop through sections and store movie and show sections in proper array
$show_sections = array();
$movie_sections = array();
foreach($sections_xml->Directory AS $value)
{
if ((string) $value->attributes()->type == 'show')
{
$show_sections[] = (string) $value->attributes()->key;
}
elseif ((string) $value->attributes()->type == 'movie')
{
$movie_sections[] = (string) $value->attributes()->key;
}
}
// Loop through each section and parse out the correct data
if ($options["movies-only"] === false)
{
foreach ($show_sections AS $key)
{
// Get XML with section's shows from Plex.
$section_xml = simplexml_load_string(file_get_contents(PLEX_URL . '/library/sections/' . $key . '/all'));
$section_title = (string) $section_xml->attributes()->title1;
echo("\nParsing section \"" . $section_title . "\"...\n\n");
parse_show_section($section_xml);
}
}
if ($options["tv-only"] === false)
{
foreach ($movie_sections AS $key)
{
// Get XML with section's movies from Plex.
$section_xml = simplexml_load_string(file_get_contents(PLEX_URL . '/library/sections/' . $key . '/all'));
$section_title = (string) $section_xml->attributes()->title1;
echo("\nParsing section \"" . $section_xml->attributes()->title1 . "\"...\n\n");
parse_movie_section($section_xml);
}
}
echo("\n=== All Done! ===\n");
function parse_show_section($xml)
{
global $options;
$show_keys = array();
$shows = array();
// Loop through shows and store keys in array.
foreach ($xml->Directory AS $value)
{
$show_keys[] = (string) $value->attributes()->key;
}
// Loop through keys and get seasons.
foreach ($show_keys AS $key)
{
$xml = simplexml_load_string(file_get_contents(PLEX_URL . $key));
foreach ($xml->Directory AS $value)
{
if ((string) $value->attributes()->type == 'season')
{
$title = (string) $xml->attributes()->parentTitle;
if (!isset($shows[$title]))
{
$shows[$title]->year = (integer) $xml->attributes()->parentYear;
$shows[$title]->seasons = array();
}
$key = (string) $value->attributes()->key;
$season_no = (string) trim(str_replace('season', '', strtolower($value->attributes()->title)));
$shows[$title]->seasons[$season_no] = array();
// Get the episodes for this season.
$episodes_xml = simplexml_load_string(file_get_contents(PLEX_URL . $key));
foreach ($episodes_xml->Video AS $episode)
{
if ((string) $episode->attributes()->type == 'episode')
{
$shows[$title]->seasons[$season_no][(integer) $episode->attributes()->index] = isset($episode->attributes()->viewCount) ? true : false;
}
}
}
}
}
// So now we have all shows with episodes, start year and watch status. Add them to Trakt (as library items if unwatched, otherwise as seen)!
echo("=== Found " . count($shows) ." shows, adding them to Trakt now. ===\n\n");
foreach ($shows AS $title => $value)
{
echo("Adding {$title}\n");
$data_watched = array();
$data_unwatched = array();
foreach ($value->seasons AS $season => $episodes)
{
foreach ($episodes AS $episode => $watched)
{
$ep->season = $season;
$ep->episode = $episode;
if ($watched)
{
$data_watched[] = $ep;
}
else
{
$data_unwatched[] = $ep;
}
unset($ep);
}
}
if (count($data_watched) > 0)
{
$data->title = $title;
$data->year = $value->year;
$data->episodes = $data_watched;
if ($options["library"] === false)
{
add_show_watched($data);
}
else
{
add_show_unwatched($data);
}
unset($data);
}
if (($options["watched"] === false) && (count($data_unwatched) > 0))
{
$data->title = $title;
$data->year = $value->year;
$data->episodes = $data_unwatched;
add_show_unwatched($data);
unset($data);
}
}
}
function parse_movie_section($xml)
{
global $options;
$movie_keys = array();
$movies = array();
// Loop through movies and store keys in array.
foreach ($xml->Video AS $value)
{
$movie_keys[] = (string) $value->attributes()->key;
}
// loop through keys and get movie data
foreach ($movie_keys AS $key)
{
$xml = simplexml_load_string(file_get_contents(PLEX_URL . $key));
foreach ($xml->Video AS $value)
{
if ((string) $value->attributes()->type == 'movie')
{
// Use Plex's ratingKey attribute as a unique identifier
$puid = (integer) $value->attributes()->ratingKey;
if (!isset($movies[$puid]))
{
$movies[$puid]->title = (string) $value->attributes()->title;
$movies[$puid]->year = (integer) $value->attributes()->year;
if (isset($value->attributes()->viewCount))
{
$movies[$puid]->plays = (integer) $value->attributes()->viewCount;
if (isset($value->attributes()->lastViewedAt))
{
$movies[$puid]->last_played = (integer) $value->attributes()->lastViewedAt;
}
}
// Try to determine an IMDB number off the guid attribute
if (isset($value->attributes()->guid))
{
$guid = (string) $value->attributes()->guid;
if (strpos($guid, "com.plexapp.agents.imdb") !== false)
{
// Determine the start and end of the IMDB number
$imdb_start = strpos($guid, "tt");
$imdb_end = strpos($guid, "?lang=");
$movies[$puid]->imdb_id = (string) substr($guid, $imdb_start, $imdb_end - $imdb_start);
}
}
echo("Found " . $movies[$puid]->title . " (" . $movies[$puid]->year . ")\n");
}
}
}
}
echo("\n=== Found " . count($movies) . " movies");
if (count($movies) > 0)
{
echo(", adding them to Trakt now. Please be patient, this may take some time ===\n\n");
// So now we have all the movies. Add them to Trakt (as library items if unwatched, otherwise as seen)!
$movies_watched = array();
$movies_unwatched = array();
foreach ($movies AS $value)
{
if (isset($value->plays))
{
$movies_watched[] = $value;
}
else
{
$movies_unwatched[] = $value;
}
}
if (count($movies_watched) > 0)
{
$data->movies = $movies_watched;
if ($options["library"] === false)
{
add_movies_watched($data);
}
else
{
add_movies_unwatched($data);
}
unset($data);
}
if (($options["watched"] === false) && (count($movies_unwatched) > 0))
{
$data->movies = $movies_unwatched;
add_movies_unwatched($data);
unset($data);
}
}
else
{
echo(". ===\n\n");
}
}
// Some function for recurring actions.
function add_show_watched($data)
{
global $options;
$response = curl_post('http://api.trakt.tv/show/episode/seen/', $data);
if ($response == false)
{
echo("Communication Error when adding watched shows\n");
} elseif ($options["debug"] === true)
{
echo("Response from Trakt: " . $response);
}
}
function add_show_unwatched($data)
{
global $options;
$response = curl_post('http://api.trakt.tv/show/episode/library/', $data);
if ($response == false)
{
echo("Communication Error when adding unwatched shows\n");
} elseif ($options["debug"] === true)
{
echo("Response from Trakt: " . $response);
}
}
function add_movies_watched($data)
{
global $options;
$response = curl_post('http://api.trakt.tv/movie/seen/', $data);
if ($response == false)
{
echo("Communication Error when adding watched movies\n");
} elseif ($options["debug"] === true)
{
echo("Response from Trakt: " . $response);
}
}
function add_movies_unwatched($data)
{
global $options;
$response = curl_post('http://api.trakt.tv/movie/library/', $data);
if ($response == false)
{
echo("Communication Error when adding unwatched movies\n");
} elseif ($options["debug"] === true)
{
echo("Response from Trakt: " . $response);
}
}
function curl_post($url, $data)
{
set_time_limit(45);
$data->username = TRAKT_USERNAME;
$data->password = sha1(TRAKT_PASSWORD);
$data = json_encode($data);
$opt_array = array(
CURLOPT_URL => $url . TRAKT_APIKEY,
CURLOPT_POSTFIELDS => $data,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 0
);
$ch = curl_init();
curl_setopt_array($ch, $opt_array);
global $options;
if ($options["debug"] === true)
{
echo("Message sent to Trakt:\n");
print_r($opt_array);
}
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
function getargs($arguments)
{
$result_array = array();
$sys_args = $_SERVER['argv'];
array_shift($sys_args);
foreach ($arguments as $key => $value)
{
if (in_array('-' . rtrim($key, ":"), $sys_args))
{
$match_position = array_search('-' . rtrim($key, ":"), $sys_args);
if (stripos($key, ":") === false)
{
$result_array[$key] = true;
$result_array[$value] = true;
unset($sys_args[$match_position]);
}
else
{
$key = rtrim($key, ":");
$value = rtrim($value, ":");
$result_array[$key] = $sys_args[$match_position + 1];
$result_array[$value] = $sys_args[$match_position + 1];
unset($sys_args[$match_position]);
unset($sys_args[$match_position + 1]);
}
}
else if (in_array('--' . rtrim($value, ":"), $sys_args))
{
$match_position = array_search('--' . rtrim($value, ":"), $sys_args);
if (stripos($value, ":") === false)
{
$result_array[$key] = true;
$result_array[$value] = true;
unset($sys_args[$match_position]);
}
else
{
$key = rtrim($key, ":");
$value = rtrim($value, ":");
$result_array[$key] = $sys_args[$match_position + 1];
$result_array[$value] = $sys_args[$match_position + 1];
unset($sys_args[$match_position]);
unset($sys_args[$match_position + 1]);
}
}
else
{
$key = rtrim($key, ":");
$value = rtrim($value, ":");
$result_array[$key] = false;
$result_array[$value] = false;
}
}
if (count($sys_args) > 0)
{
var_dump($sys_args);
}
return $result_array;
}
?>