-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GoodreadsBridge.php
95 lines (80 loc) · 3.52 KB
/
GoodreadsBridge.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
<?php
class GoodreadsBridge extends BridgeAbstract
{
const MAINTAINER = 'captn3m0';
const NAME = 'Goodreads Bridge';
const URI = 'https://www.goodreads.com/';
const CACHE_TIMEOUT = 0; // 30min
const DESCRIPTION = 'Various RSS feeds from Goodreads';
const CONTEXT_AUTHOR_BOOKS = 'Books by Author';
// Using a specific context because I plan to expand this soon
const PARAMETERS = [
'Books by Author' => [
'author_url' => [
'name' => 'Link to author\'s page on Goodreads',
'type' => 'text',
'required' => true,
'title' => 'Should look somewhat like goodreads.com/author/show/',
'pattern' => '^(https:\/\/)?(www.)?goodreads\.com\/author\/show\/\d+\..*$',
'exampleValue' => 'https://www.goodreads.com/author/show/38550.Brandon_Sanderson'
],
'published_only' => [
'name' => 'Show published books only',
'type' => 'checkbox',
'required' => false,
'title' => 'If left unchecked, this will return unpublished books as well',
'defaultValue' => 'checked',
],
],
];
private function collectAuthorBooks($url)
{
$regex = '/goodreads\.com\/author\/show\/(\d+)/';
preg_match($regex, $url, $matches);
$authorId = $matches[1];
$authorListUrl = "https://www.goodreads.com/author/list/$authorId?sort=original_publication_year";
$html = getSimpleHTMLDOMCached($authorListUrl, self::CACHE_TIMEOUT);
foreach ($html->find('tr[itemtype="http://schema.org/Book"]') as $row) {
$dateSpan = $row->find('.uitext', 0)->plaintext;
$date = null;
// If book is not yet published, ignore for now
if (preg_match('/published\s+(\d{4})/', $dateSpan, $matches) === 1) {
// Goodreads doesn't give us exact publication date here, only a year
// We are skipping future dates anyway, so this is def published
// but we can't pick a dynamic date either to keep clients from getting
// confused. So we pick a guaranteed date of 1st-Jan instead.
$date = $matches[1] . '-01-01';
} elseif ($this->getInput('published_only') !== 'checked') {
// We can return unpublished books as well
$date = date('Y-01-01');
} else {
continue;
}
$row = defaultLinkTo($row, $this->getURI());
$item['title'] = $row->find('.bookTitle', 0)->plaintext;
$item['uri'] = $row->find('.bookTitle', 0)->getAttribute('href');
$item['author'] = $row->find('.authorName', 0)->plaintext;
$item['content'] = '<a href="'
. $row->find('.bookTitle', 0)->getAttribute('href')
. '"><img src="'
. $row->find('.bookCover', 0)->getAttribute('src')
. '"></a>';
$item['timestamp'] = $date;
$item['enclosures'] = [
$row->find('.bookCover', 0)->getAttribute('src')
];
$this->items[] = $item; // Add item to the list
}
}
public function collectData()
{
switch ($this->queriedContext) {
case self::CONTEXT_AUTHOR_BOOKS:
$this->collectAuthorBooks($this->getInput('author_url'));
break;
default:
throw new Exception('Invalid context', 1);
break;
}
}
}