forked from RSS-Bridge/rss-bridge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLWNprevBridge.php
145 lines (128 loc) · 3.63 KB
/
LWNprevBridge.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
<?php
class LWNprevBridge extends BridgeAbstract{
const MAINTAINER = 'Pierre Mazière';
const NAME = 'LWN Free Weekly Edition';
const URI = 'https://lwn.net/';
const CACHE_TIMEOUT = 604800; // 1 week
const DESCRIPTION = 'LWN Free Weekly Edition available one week late';
function getURI(){
return self::URI . 'free/bigpage';
}
private function jumpToNextTag(&$node){
while($node && $node->nodeType === XML_TEXT_NODE){
$nextNode = $node->nextSibling;
if(!$nextNode){
break;
}
$node = $nextNode;
}
}
private function jumpToPreviousTag(&$node){
while($node && $node->nodeType === XML_TEXT_NODE){
$previousNode = $node->previousSibling;
if(!$previousNode){
break;
}
$node = $previousNode;
}
}
public function collectData(){
// Because the LWN page is written in loose HTML and not XHTML,
// Simple HTML Dom is not accurate enough for the job
$content = getContents($this->getURI())
or returnServerError('No results for LWNprev');
libxml_use_internal_errors(true);
$html = new DOMDocument();
$html->loadHTML($content);
libxml_clear_errors();
$cat1 = '';
$cat2 = '';
foreach($html->getElementsByTagName('a') as $a){
if($a->textContent === 'Multi-page format'){
break;
}
}
$realURI = self::URI . $a->getAttribute('href');
$URICounter = 0;
$edition = $html->getElementsByTagName('h1')->item(0)->textContent;
$editionTimeStamp = strtotime(
substr($edition, strpos($edition, 'for ') + strlen('for '))
);
foreach($html->getElementsByTagName('h2') as $h2){
if($h2->getAttribute('class') !== 'SummaryHL'){
continue;
}
$item = array();
$h2NextSibling = $h2->nextSibling;
$this->jumpToNextTag($h2NextSibling);
switch($h2NextSibling->getAttribute('class')){
case 'FeatureByline':
$item['author'] = $h2NextSibling->getElementsByTagName('b')->item(0)->textContent;
break;
case 'GAByline':
$text = $h2NextSibling->textContent;
$item['author'] = substr($text, strpos($text, 'by '));
break;
default:
$item['author'] = 'LWN';
break;
};
$h2FirstChild = $h2->firstChild;
$this->jumpToNextTag($h2FirstChild);
if($h2FirstChild->nodeName === 'a'){
$item['uri'] = self::URI . $h2FirstChild->getAttribute('href');
}else{
$item['uri'] = $realURI . '#' . $URICounter;
}
$URICounter++;
$item['timestamp'] = $editionTimeStamp + $URICounter;
$h2PrevSibling = $h2->previousSibling;
$this->jumpToPreviousTag($h2PrevSibling);
switch($h2PrevSibling->getAttribute('class')){
case 'Cat2HL':
$cat2 = $h2PrevSibling->textContent;
$h2PrevSibling = $h2PrevSibling->previousSibling;
$this->jumpToPreviousTag($h2PrevSibling);
if($h2PrevSibling->getAttribute('class') !== 'Cat1HL'){
break;
}
$cat1 = $h2PrevSibling->textContent;
break;
case 'Cat1HL':
$cat1 = $h2PrevSibling->textContent;
$cat2 = '';
break;
default:
break;
}
$h2PrevSibling = null;
$item['title'] = '';
if(!empty($cat1)){
$item['title'] .= '[' . $cat1 . ($cat2 ? '/' . $cat2 : '') . '] ';
}
$item['title'] .= $h2->textContent;
$node = $h2;
$content = '';
$contentEnd = false;
while(!$contentEnd){
$node = $node->nextSibling;
if(!$node || (
$node->nodeType !== XML_TEXT_NODE && (
$node->nodeName === 'h2' || (
!is_null($node->attributes) &&
!is_null($class = $node->attributes->getNamedItem('class')) &&
in_array($class->nodeValue, array('Cat1HL', 'Cat2HL'))
)
)
)
){
$contentEnd = true;
}else{
$content .= $node->C14N();
}
}
$item['content'] = $content;
$this->items[] = $item;
}
}
}