-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbetaseries-to-trakt
executable file
·163 lines (129 loc) · 4.01 KB
/
betaseries-to-trakt
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
#!/usr/bin/env php
<?php
// You must create an application to obtain the followings
// https://trakt.tv/oauth/applications/new
$config = [
'client_id' => '',
'client_secret' => ''
];
/***** CONFIGURATION END *****/
function usage($binName) {
exit("Usage: ${binName} \"path/to/betaseries_file.csv\"\n");
}
function call($method, $url, $data = false, $customHeaders = false)
{
global $config;
$curl = curl_init();
switch ($method)
{
case 'POST':
curl_setopt($curl, CURLOPT_POST, TRUE);
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
}
break;
case 'PUT':
curl_setopt($curl, CURLOPT_PUT, TRUE);
break;
default:
if ($data) {
$url = sprintf("%s?%s", $url, http_build_query($data));
}
}
$headers = [
'Content-Type: application/json',
'trakt-api-version: 2',
'trakt-api-key: ' . $config['client_id']
];
if ($customHeaders) {
$headers = array_merge($headers, $customHeaders);
}
curl_setopt($curl, CURLOPT_URL, 'https://api-v2launch.trakt.tv' . $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result);
}
/***** BEGINING OF LOGIC *****/
if (!isset($config['client_secret']) || !isset($config['client_secret'])) {
exit("Please check the configuration variables at the top of the file.\n");
}
if (!isset($argv[1])) {
usage($argv[0]);
}
$file = fopen($argv[1], 'r');
if ($file === FALSE) {
exit("{$argv[1]} not found\n");
}
echo "Please go at your app PIN url ( https://trakt.tv/pin/XXXXX ) and authorize your application.\n";
echo "After, a PIN of 8 digits will be displayed (only valid 1 time). Please enter it here.\n";
$authPin = readline("Pin: ");
$postDatas = [
'code' => $authPin,
'client_id' => $config['client_id'],
'client_secret' => $config['client_secret'],
'redirect_uri' => 'urn:ietf:wg:oauth:2.0:oob',
'grant_type' => 'authorization_code'
];
echo "Authentificating...";
$obj = call('POST', '/oauth/token', $postDatas);
if (!isset($obj->access_token)) {
echo "\nERROR: $obj->error: $obj->error_description\n";
exit("Unable to retrieve the access_token\n");
}
$accessToken = $obj->access_token;
echo "\t\tOK\n";
echo "Building query...";
$body = [
'shows' => []
];
while (($line = fgetcsv($file, 0, ',')) !== FALSE) {
// Skip first line
if ($line[0] === 'id') {
continue;
}
$tvdbId = $line[1];
$name = $line[2];
preg_match('/S([0-9]+)E([0-9]+)/', $line[4], $lastSeen);
$status = $line[6];
$show = [
'title' => $name,
'ids' => [
'tvdb' => $tvdbId
],
'watched_at' => 'released'
];
if ($status != 100) {
$show['seasons'] = [];
for ($s = 1; $s <= $lastSeen[1]; $s++) {
$season = [
'number' => $s,
'watched_at' => 'released'
];
array_push($show['seasons'], $season);
if ($s == $lastSeen[1]) {
$sIdx = $s - 1;
$show['seasons'][$sIdx]['episodes'] = [];
for ($e = 1; $e <= $lastSeen[2]; $e++) {
$episode = [
'number' => $e,
'watched_at' => 'released'
];
array_push($show['seasons'][$sIdx]['episodes'], $episode);
}
}
}
}
array_push($body['shows'], $show);
}
echo "\t\tOK\n";
echo "Sending datas to Trakt.tv...";
$resp = call('POST', '/sync/history', $body, ["Authorization: Bearer $accessToken"]);
echo "\tOK\n";
if (isset($resp->added)) {
echo "We have added:\n";
echo "\t{$resp->added->movies} movies\n";
echo "\t{$resp->added->episodes} episodes\n";
}