/
CalendarRenderer.php
313 lines (249 loc) · 9.54 KB
/
CalendarRenderer.php
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
<?php
namespace Starsquare\Letterboxd;
use Buzz\Browser;
use Buzz\Client\Curl;
use Nyholm\Psr7\Factory\Psr17Factory;
use Buzz\Middleware\CallbackMiddleware;
use Buzz\Middleware\CookieMiddleware;
use Buzz\Message\RequestInterface;
use Eluceo\iCal\Domain\Entity\Event;
use Eluceo\iCal\Domain\Entity\TimeZone;
use Eluceo\iCal\Domain\ValueObject\Date;
use Eluceo\iCal\Domain\ValueObject\SingleDay;
use Eluceo\iCal\Domain\ValueObject\Uri;
class CalendarRenderer {
const PROD_ID = '-//StarSquare//LETTERBOXD//%s//EN';
const USER_AGENT = 'letterboxd-ics/%s (https://bux.re/letterboxd-ics) PHP/%s';
const CSRF_TOKEN = '__csrf';
const CSRF_PATTERN = '/CSRF = ("|\')(?P<token>.+?)\1/';
protected $urls = array(
'home' => 'https://letterboxd.com/',
'login' => 'https://letterboxd.com/user/login.do',
'export' => 'https://letterboxd.com/data/export/',
);
/**
* Name of headers to use from the CSV
*/
protected $headers = array(
'summary' => 'Name',
'date' => 'Watched Date',
'url' => 'Letterboxd URI',
'rating' => 'Rating',
'year' => 'Year',
);
/**
* Calendar options
*/
protected $options = array(
'version' => 'x.x.x',
'auth' => array(),
'output' => array(
'headers' => true,
'errors' => false,
'content-type' => 'text/calendar',
'charset' => 'utf-8',
),
);
/**
* Rendered output
*/
protected $output = null;
/**
* HTTP client
*/
protected $browser = null;
/**
* User-Agent to use with HTTP client
*/
protected $userAgent = null;
/**
* Path to CSV file
*/
protected $file = null;
/**
* Logger
*/
protected $log = null;
/**
* Calendar
*/
protected $calendar = null;
public function __construct($options = array()) {
$this->setOptions($options);
$this->calendar = new Calendar;
$this->calendar->setProductIdentifier(sprintf(static::PROD_ID, $this->options['version']));
if (!isset($this->options['log'])) {
$this->options['log'] = new Logger('php://stderr');
}
$this->log = $this->options['log'];
if (isset($this->options['calendar']['name'])) {
$this->calendar->setName($this->options['calendar']['name']);
}
if (isset($this->options['calendar']['description'])) {
$this->calendar->setDescription($this->options['calendar']['description']);
}
if (isset($this->options['calendar']['timezone'])) {
$this->calendar->addTimezone(new TimeZone($this->options['calendar']['timezone']));
}
}
public function setOptions($options) {
if (is_string($options)) {
if (!file_exists($options)) {
throw new Exception("Cannot find options file: $options");
}
$options = json_decode(file_get_contents($options), true);
if ($options === null) {
throw new Exception('Cannot parse options file as JSON');
}
}
if (!is_array($options)) {
throw new Exception('Options must be array or path to options file');
}
$this->options = array_replace_recursive($this->options, $options);
return $this;
}
protected function getUserAgent() {
if ($this->userAgent === null) {
$this->userAgent = sprintf(
static::USER_AGENT,
$this->options['version'],
phpversion()
);
}
return $this->userAgent;
}
public function getBrowser() {
if ($this->browser === null) {
$this->browser = new Browser(new Curl(new Psr17Factory), new Psr17Factory);
$this->browser->addMiddleware(new CallbackMiddleware(function ($request, $response = null) {
if ($response) {
return $response;
}
return $request->withHeader('User-Agent', $this->getUserAgent());
}));
$this->browser->addMiddleware(new CookieMiddleware);
}
return $this->browser;
}
protected function login() {
$auth = $this->options['auth'];
if (empty($auth['username']) || empty($auth['password'])) {
throw new Exception('Cannot log in: Missing username/password');
}
$browser = $this->getBrowser();
$home = $browser->get($this->urls['home']);
$content = (string) $home->getBody();
if (!preg_match(sprintf(static::CSRF_PATTERN, static::CSRF_TOKEN), $content, $matches)) {
throw new Exception('Cannot log in: Cannot find CSRF token');
}
$this->log->info('Logging in');
$auth[static::CSRF_TOKEN] = $matches['token'];
$loginResponse = $browser->submitForm($this->urls['login'], $auth);
if ($loginResponse->getStatusCode() != 200) {
$this->log->warn('Login HTTP Error ' . $loginResponse->getStatusCode());
throw new Exception('Cannot log in: Received HTTP ' . $loginResponse->getStatusCode());
}
if (($result = json_decode((string) $loginResponse->getBody())) === null) {
throw new Exception('Cannot log in: Could not decode response as JSON');
}
if ($result->result === 'error') {
$this->log->warn('Login error: ' . $result->messages[0]);
throw new Exception('Cannot log in: ' . $result->messages[0]);
}
$this->log->notice('Login completed');
}
protected function getFile() {
if ($this->file === null) {
if (isset($this->options['file'])) {
$this->file = $this->options['file'];
} else {
$this->login();
$this->log->info('Getting export file: ' . $this->urls['export']);
$export = $this->getBrowser()->get($this->urls['export']);
if ($export->getStatusCode() !== 200) {
throw new Exception('Cannot read export: Received HTTP ' . $export->getStatusCode());
}
$contentType = $export->getHeader('Content-Type');
if (strpos($contentType[0], 'application/zip') === false) {
throw new Exception('Cannot read export: Did not respond with a ZIP file');
}
$this->log->info('Creating local export ZIP');
$zipFile = tempnam(sys_get_temp_dir(), 'letterboxd-export');
file_put_contents($zipFile, $export->getBody());
$this->file = "zip://$zipFile#diary.csv";
}
}
return $this->file;
}
public function loadEvents() {
if (!iterator_count($this->calendar->getEvents())) {
$diary = @fopen($this->getFile(), 'r');
$headers = null;
if ($diary === false) {
$error = error_get_last();
throw new Exception("Cannot find event file: " . $error['message']);
}
$this->log->info('Parsing diary CSV');
while (false !== ($row = fgetcsv($diary))) {
if ($headers === null) {
$headers = $row;
} else {
$row = array_combine($headers, $row);
$date = new \DateTime($row[$this->headers['date']]);
$event = (new Event)
->setSummary($row[$this->headers['summary']])
->setDescription($this->getEventDescription($row))
->setOccurrence(new SingleDay(new Date($date)))
->setUrl(new Uri($row[$this->headers['url']]));
$this->calendar->addEvent($event);
}
}
$this->log->info('Parsing complete');
}
}
protected function getEventDescription(array $data) {
$template = implode("\n", [
'Year: %d',
'Rating: %s',
]);
$year = $data[$this->headers['year']];
$rating = $data[$this->headers['rating']];
$ratingText = str_repeat('★', intval($rating)) . (strpos($rating, '.5') ? '½' : '');
return sprintf($template, $year, $ratingText);
}
public function sendHeaders() {
if ($this->options['output']['headers'] && !headers_sent()) {
header(sprintf(
'Content-Type: %s; charset=%s',
$this->options['output']['content-type'],
$this->options['output']['charset']
));
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Sat, 29 Sep 1984 15:00:00 GMT');
header('Last-Modified: Sat, 29 Sep 1984 15:00:00 GMT');
header('ETag: "' . md5($this->output) . '"');
}
}
public function render() {
$this->loadEvents();
if ($this->output === null) {
$calendarComponent = (new CalendarFactory())->createCalendar($this->calendar);
$this->output = (string) $calendarComponent;
}
$this->sendHeaders();
return $this->output;
}
public function __toString() {
try {
return $this->render();
} catch (\Exception $ex) {
$message = $ex->getMessage();
if ($this->options['output']['errors']) {
return $message;
} else {
trigger_error($message, E_USER_ERROR);
return '';
}
}
}
}