... | ... |
@@ -1,10 +1,138 @@ |
1 | 1 |
<?php |
2 | 2 |
|
3 | 3 |
/** |
4 |
+ * Class representing restaurant |
|
5 |
+ */ |
|
6 |
+class Restaurant |
|
7 |
+{ |
|
8 |
+ |
|
9 |
+ private $url; |
|
10 |
+ private $name; |
|
11 |
+ private $xpaths = array(); |
|
12 |
+ |
|
13 |
+ public function getName() |
|
14 |
+ { |
|
15 |
+ return $this->name; |
|
16 |
+ } |
|
17 |
+ |
|
18 |
+ public function getUrl() |
|
19 |
+ { |
|
20 |
+ return $this->url; |
|
21 |
+ } |
|
22 |
+ |
|
23 |
+ public function getXpaths() |
|
24 |
+ { |
|
25 |
+ return $this->xpaths; |
|
26 |
+ } |
|
27 |
+ |
|
28 |
+ /** |
|
29 |
+ * @param $name |
|
30 |
+ * @param $url |
|
31 |
+ * @param $xpaths |
|
32 |
+ */ |
|
33 |
+ function __construct($name, $url, $xpaths) |
|
34 |
+ { |
|
35 |
+ |
|
36 |
+ if (empty($url) || empty($xpaths)) { |
|
37 |
+ throw new InvalidArgumentException("All argument are required"); |
|
38 |
+ } |
|
39 |
+ |
|
40 |
+ $this->name = $name; |
|
41 |
+ $this->url = $url; |
|
42 |
+ |
|
43 |
+ if (is_string($xpaths)) { |
|
44 |
+ $this->xpaths[] = $xpaths; |
|
45 |
+ } else if (is_array($xpaths)) { |
|
46 |
+ $this->xpaths = $xpaths; |
|
47 |
+ } else { |
|
48 |
+ throw new InvalidArgumentException("Xpaths must be string or array"); |
|
49 |
+ } |
|
50 |
+ } |
|
51 |
+ |
|
52 |
+} |
|
53 |
+ |
|
54 |
+ |
|
55 |
+class Parser { |
|
56 |
+ |
|
57 |
+ private $restaurants = array(); |
|
58 |
+ |
|
59 |
+ function __construct($restaurants) |
|
60 |
+ { |
|
61 |
+ |
|
62 |
+ if (empty($restaurants) || !is_array($restaurants)) { |
|
63 |
+ throw new InvalidArgumentException("Array argument required"); |
|
64 |
+ } |
|
65 |
+ |
|
66 |
+ $this->restaurants = $restaurants; |
|
67 |
+ } |
|
68 |
+ |
|
69 |
+ public function parse() |
|
70 |
+ { |
|
71 |
+ foreach ($this->restaurants as $restaurant) { |
|
72 |
+ |
|
73 |
+ $source = file_get_contents($restaurant->getUrl()); |
|
74 |
+ |
|
75 |
+ if ($source === false) { |
|
76 |
+ throw new RuntimeException("Can't read source address: " . $restaurant->getUrl()); |
|
77 |
+ } |
|
78 |
+ |
|
79 |
+ $dom = new DOMDocument(); |
|
80 |
+ @$dom->loadHTML($source); |
|
81 |
+ $xpath_obj = new DOMXPath($dom); |
|
82 |
+ |
|
83 |
+ echo("Restaurant: <a href='{$restaurant->getUrl()}'>" . $restaurant->getName() . "</a><br>\n"); |
|
84 |
+ |
|
85 |
+ foreach ($restaurant->getXpaths() as $name => $xpath) { |
|
86 |
+ |
|
87 |
+ $found = $xpath_obj->query($xpath); |
|
88 |
+ |
|
89 |
+ if ($found === false || $found->length === 0) { |
|
90 |
+ continue; |
|
91 |
+ } |
|
92 |
+ |
|
93 |
+ echo "<b>$name</b>"; |
|
94 |
+ $exists = false; |
|
95 |
+ foreach ($found->item(0)->childNodes as $elem) { |
|
96 |
+ |
|
97 |
+ $meal = trim($elem->nodeValue); |
|
98 |
+ if (!empty($meal)) { |
|
99 |
+ echo("\t<br>$meal\n"); |
|
100 |
+ $exists = true; |
|
101 |
+ } |
|
102 |
+ } |
|
103 |
+ if (!$exists) { |
|
104 |
+ echo "<br>{}"; |
|
105 |
+ } |
|
106 |
+ echo "<br>"; |
|
107 |
+ } |
|
108 |
+ echo "<br>"; |
|
109 |
+ } |
|
110 |
+ } |
|
111 |
+ |
|
112 |
+ |
|
113 |
+} |
|
114 |
+ |
|
115 |
+$twenties = new Restaurant("Twenties", "http://www.twenties.sk/", array("Polievky" => '//*[@id="article"]/div[2]/p[1]')); |
|
116 |
+$pulitzer = new Restaurant("Pulitzer", "http://www.pulitzer.sk", |
|
117 |
+ array("Polievky" => '//*[@id="soups"]', |
|
118 |
+ "Hlavne jedla" => '//*[@id="meals"]')); |
|
119 |
+$slovak = new Restaurant("Slovak pub", "http://www.arcaderestaurant.sk/articles/public_menu/show-modules/id/24", |
|
120 |
+ array("Salat" => '//*[@id="table2"]/tbody/tr[10]', |
|
121 |
+ "Polievka" => '//*[@id="table2"]/tbody/tr[2]')); |
|
122 |
+ |
|
123 |
+ |
|
124 |
+$parser = new Parser(array($twenties, $pulitzer, $slovak)); |
|
125 |
+$parser->parse(); |
|
126 |
+ |
|
127 |
+ |
|
128 |
+ |
|
129 |
+/** |
|
4 | 130 |
* Lebo bez masa nie je den dnom. Maso je sucast jedla, preto chodim jest do |
5 | 131 |
* blizkych malych restauracii. |
6 | 132 |
*/ |
7 | 133 |
|
134 |
+/* |
|
135 |
+ |
|
8 | 136 |
$sites = [ |
9 | 137 |
'Pulitzer' => 'http://pulitzer.sk/', |
10 | 138 |
'Twenties' => 'http://www.twenties.sk/' |
... | ... |
@@ -44,7 +172,7 @@ foreach ($sites as $restaurant => $site) |
44 | 44 |
} |
45 | 45 |
|
46 | 46 |
$found_meal = FALSE; |
47 |
- foreach ($oh_my_god_here_are_saved_meals->item(0)->childNodes as $elem) |
|
47 |
+ foreach ($oh_my_god_here_are_saved_meals->item(0)->childNodes as $elem) |
|
48 | 48 |
{ |
49 | 49 |
$meal_meal_meal = trim($elem->nodeValue); |
50 | 50 |
if (! empty($meal_meal_meal)) |
... | ... |
@@ -58,7 +186,7 @@ foreach ($sites as $restaurant => $site) |
58 | 58 |
{ |
59 | 59 |
append_result("\tNevaria!!! Daj mi niekto gulomet, nech ich zabijem!\n"); |
60 | 60 |
} |
61 |
- else |
|
61 |
+ else |
|
62 | 62 |
{ |
63 | 63 |
append_result("\n"); |
64 | 64 |
} |
... | ... |
@@ -72,3 +200,5 @@ function append_result($text) |
72 | 72 |
global $output_file; |
73 | 73 |
file_put_contents($output_file, $text, FILE_APPEND | FILE_TEXT); |
74 | 74 |
} |
75 |
+ |
|
76 |
+*/ |