|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) Andreas Heigl<[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Andreas Heigl<[email protected]> |
|
25
|
|
|
* @copyright Andreas Heigl |
|
26
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
|
27
|
|
|
* @since 29.07.2016 |
|
28
|
|
|
* @link http://github.com/heiglandreas/callingallpapers |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
namespace Callingallpapers\Parser; |
|
32
|
|
|
|
|
33
|
|
|
use Callingallpapers\Parser\PapercallIo\EventParser; |
|
34
|
|
|
use Callingallpapers\Service\TimezoneService; |
|
35
|
|
|
use Callingallpapers\Writer\WriterInterface; |
|
36
|
|
|
use DOMDocument; |
|
37
|
|
|
use DOMXPath; |
|
38
|
|
|
|
|
39
|
|
|
class PapercallIoParser implements ParserInterface |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
protected $tzService; |
|
43
|
|
|
|
|
44
|
|
|
private $uri = 'https://papercall.io/cfps?page=%1$s'; |
|
45
|
|
|
|
|
46
|
|
|
private $parser; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct(TimezoneService $tzService, EventParser $parser) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->tzService = $tzService; |
|
51
|
|
|
$this->parser = $parser; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function setStartUrl(string $uri) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->uri = $uri; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function parse(WriterInterface $writer) |
|
60
|
|
|
{ |
|
61
|
|
|
$i = 0; |
|
62
|
|
|
|
|
63
|
|
|
$pages = null; |
|
64
|
|
|
|
|
65
|
|
|
do { |
|
66
|
|
|
$dom = new DOMDocument('1.0', 'UTF-8'); |
|
67
|
|
|
libxml_use_internal_errors(true); |
|
68
|
|
|
$uri = sprintf($this->uri, $i+1); |
|
69
|
|
|
var_dump($uri); |
|
|
|
|
|
|
70
|
|
|
$dom->loadHTMLFile($uri); |
|
71
|
|
|
libxml_use_internal_errors(false); |
|
72
|
|
|
$dom->preserveWhiteSpace = false; |
|
73
|
|
|
$xpath = new DOMXPath($dom); |
|
74
|
|
|
|
|
75
|
|
|
if (null === $pages) { |
|
76
|
|
|
$p = $xpath->query("//ul[contains(@class,'pagination')]/li"); |
|
77
|
|
|
$pages = $p->length - 2; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$nodes = $xpath->query("//div[contains(@class,'main')]/div[@class='container'][2]//div[@class='box']"); |
|
81
|
|
|
if ($nodes->length < 1) { |
|
82
|
|
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** @var DOMNode $node */ |
|
86
|
|
|
foreach ($nodes as $node) { |
|
87
|
|
|
try { |
|
88
|
|
|
$writer->write($this->parser->parseEvent($node)); |
|
|
|
|
|
|
89
|
|
|
} catch (\Exception $e) { |
|
90
|
|
|
error_log($e->getMEssage()); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} while (++$i < $pages); |
|
94
|
|
|
|
|
95
|
|
|
return true; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|