|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Vendors\StackOverflow; |
|
4
|
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Watcher\Handler\WebPageHandler; |
|
6
|
|
|
use DOMElement; |
|
7
|
|
|
use DOMXPath; |
|
8
|
|
|
|
|
9
|
|
|
class StackOverflowWebPageHandler extends WebPageHandler |
|
10
|
|
|
{ |
|
11
|
|
|
public function __construct ( string $url ) |
|
12
|
|
|
{ |
|
13
|
|
|
parent::__construct( $url ); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
private array $results; |
|
17
|
|
|
|
|
18
|
|
|
public function getResults () : array |
|
19
|
|
|
{ |
|
20
|
|
|
return $this->results; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function read () : void |
|
24
|
|
|
{ |
|
25
|
|
|
parent::read(); |
|
26
|
|
|
|
|
27
|
|
|
$this->results = array(); |
|
28
|
|
|
|
|
29
|
|
|
$finder = new DomXPath( $this->dom ); |
|
30
|
|
|
$classname = "listResults"; |
|
31
|
|
|
$listResultsDom = $finder->query( "//*[contains(@class, '$classname')]" ); // DOMNodeList |
|
32
|
|
|
|
|
33
|
|
|
for ( $i = 0; $i < $listResultsDom->count(); $i++ ) { |
|
34
|
|
|
$currentDomNode = $listResultsDom->item( $i ); // DOMElement |
|
35
|
|
|
|
|
36
|
|
|
if ( $currentDomNode->hasChildNodes() ) { |
|
37
|
|
|
$children = $currentDomNode->childNodes; // DOMNodeList |
|
38
|
|
|
|
|
39
|
|
|
for ( $j = 0; $j < $children->count(); $j++ ) { |
|
40
|
|
|
$currentJob = new StackOverflowJob(); |
|
41
|
|
|
|
|
42
|
|
|
$currentChildrenNode = $children->item( $j ); // DOMElement or DOMText |
|
43
|
|
|
$attributes = $currentChildrenNode->attributes; // DOMNamedNodeMap |
|
44
|
|
|
|
|
45
|
|
|
if ( $attributes != null ) { |
|
46
|
|
|
foreach ( $attributes as $currentAttribute ) { |
|
47
|
|
|
if ( $currentAttribute->name != null ) { |
|
48
|
|
|
if ( $currentAttribute->name == "data-jobid" ) { |
|
49
|
|
|
$currentJob->setJobId( $currentAttribute->value ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if ( $currentAttribute->name == "data-result-id" ) { |
|
53
|
|
|
$currentJob->setResultId( $currentAttribute->value ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ( $currentAttribute->name == "data-preview-url" ) { |
|
57
|
|
|
$currentJob->setPreviewUrl( $currentAttribute->value ); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ( $currentChildrenNode->hasChildNodes() ) { |
|
64
|
|
|
$extraChildNodes = $currentChildrenNode->childNodes; // DOMNodeList |
|
65
|
|
|
|
|
66
|
|
|
for ( $k = 0; $k < $extraChildNodes->count(); $k++ ) { |
|
67
|
|
|
$currentExtraChildNode = $extraChildNodes->item( $k ); // DOMElement or DOMText |
|
68
|
|
|
|
|
69
|
|
|
if ( $currentExtraChildNode instanceof DOMElement ) { |
|
70
|
|
|
if ( $currentExtraChildNode->hasChildNodes() ) { |
|
71
|
|
|
$currentExtraChildNodeChildren = $currentExtraChildNode->childNodes; // DOMNodeList |
|
72
|
|
|
|
|
73
|
|
|
if ( $currentExtraChildNodeChildren->count() == 6 ) { |
|
74
|
|
|
for ( $l = 0; $l < $currentExtraChildNodeChildren->count(); $l++ ) { |
|
75
|
|
|
$currentDeepNode = $currentExtraChildNodeChildren->item( $l ); // DOMElement or DOMText |
|
76
|
|
|
|
|
77
|
|
|
if ( $currentDeepNode instanceof DOMElement ) { |
|
78
|
|
|
$currentDeepNodeAttributes = $currentDeepNode->attributes; // DOMNamedNodeMap |
|
79
|
|
|
|
|
80
|
|
|
if ( $currentDeepNode->tagName == "img" ) { |
|
81
|
|
|
if ( $currentDeepNodeAttributes != null && sizeof( $currentDeepNodeAttributes ) == 2 ) { |
|
82
|
|
|
$attr1 = $currentDeepNodeAttributes[0]; // DOMAttr |
|
83
|
|
|
$attr2 = $currentDeepNodeAttributes[1]; // DOMAttr |
|
84
|
|
|
|
|
85
|
|
|
if ( $attr1->name == "class" && $attr1->value == "grid--cell fl-shrink0 w48 h48 bar-sm mr12" && $attr2->name == "src" ) { |
|
86
|
|
|
$currentJob->setLogo( $attr2->value ); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ( $currentDeepNode->tagName == "div" ) { |
|
92
|
|
|
if ( $currentDeepNode->hasChildNodes() ) { |
|
93
|
|
|
$currentDeepNodeChildren = $currentDeepNode->childNodes; // DOMNodeList |
|
94
|
|
|
|
|
95
|
|
|
for ( $m = 0; $m < $currentDeepNodeChildren->count(); $m++ ) { |
|
96
|
|
|
$currentDeepNodeChildrenElement = $currentDeepNodeChildren->item( $m ); // DOMElement or DOMText |
|
97
|
|
|
|
|
98
|
|
|
if ( $currentDeepNodeChildrenElement instanceof DOMElement ) { |
|
99
|
|
|
if ( $currentDeepNodeChildrenElement->tagName == "h2" ) { |
|
100
|
|
|
$currentJob->setTitle( trim( $currentDeepNodeChildrenElement->nodeValue ) ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if ( $currentDeepNodeChildrenElement->tagName == "h3" ) { |
|
104
|
|
|
if ( $currentDeepNodeChildrenElement->hasChildNodes() ) { |
|
105
|
|
|
$companyAndLocationDomNodeList = $currentDeepNodeChildrenElement->childNodes; // DOMNodeList |
|
106
|
|
|
|
|
107
|
|
|
for ( $n = 0; $n < $companyAndLocationDomNodeList->count(); $n++ ) { |
|
108
|
|
|
$currentCompanyAndLocationElement = $companyAndLocationDomNodeList->item( $n ); // DOMElement or DOMText |
|
109
|
|
|
|
|
110
|
|
|
if ( $currentCompanyAndLocationElement instanceof DOMElement ) { |
|
111
|
|
|
if ( $currentCompanyAndLocationElement->nodeName == "span" ) { |
|
112
|
|
|
if ( $currentCompanyAndLocationElement->attributes->count() == 0 ) { |
|
113
|
|
|
$companyName = trim( $currentCompanyAndLocationElement->nodeValue ); |
|
114
|
|
|
|
|
115
|
|
|
if ( strpos( $companyName, "\r\n" ) !== false ) { |
|
116
|
|
|
$companyNameParts = explode( "\r\n", $companyName ); |
|
117
|
|
|
|
|
118
|
|
|
foreach ( $companyNameParts as $index => $currentCompanyNamePart ) { |
|
119
|
|
|
$companyNameParts[$index] = trim( $currentCompanyNamePart ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$currentJob->setCompany( implode( " ", $companyNameParts ) ); |
|
123
|
|
|
} else { |
|
124
|
|
|
$currentJob->setCompany( $companyName ); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if ( $currentCompanyAndLocationElement->attributes->count() == 1 ) { |
|
129
|
|
|
if ( $currentCompanyAndLocationElement->getAttribute( "class" ) == "fc-black-500" ) { |
|
130
|
|
|
$currentJob->setLocation( trim( $currentCompanyAndLocationElement->nodeValue ) ); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if ( $currentJob->allAttributesDefined() ) { |
|
151
|
|
|
array_push( $this->results, $currentJob ); |
|
152
|
|
|
} else { |
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|