· 6 years ago · Dec 17, 2019, 08:56 AM
1 public function parserSinglePage($url, $parse_only_phone = false)
2 {
3 $dom = $this->getDom($url);
4
5 if($parse_only_phone){
6 return str_replace(" ", "", $this->text($dom, '.agent .number'));
7 }
8
9 $url_info = parse_url($url);
10 parse_str($url_info['query'], $query);
11 $home_id_myhome = $query['id'];
12
13 $home_myhome = HomeParsed::where('external_id', $home_id_myhome)->where('site', 'myhome.ge')->first();
14 if (!empty($home_myhome)) {
15 return "home $home_id_myhome SKIPPED already parsed.";
16 }
17
18 $home_and_params_data = [];
19 $home_data = [];
20 $user_data = [];
21
22 $home_data['duration'] = 30;
23
24 //TITLE START
25 $title = $this->text($dom, '.statement-title .mb-0');
26
27 if (empty($title) || strlen($title) < 5) {
28 return "home $home_id_myhome SKIPPED NO DATA.";
29 }
30
31 $json_arr = ['ka' => $title, 'en' => $title, 'ru' => $title];
32 $home_data['title'] = json_encode($json_arr, JSON_UNESCAPED_UNICODE);
33 //TITLE END
34
35 //COMMENT START
36 $comment = $this->text($dom, ".shortened p");
37 $json_arr_comment = ['ka' => $comment, 'en' => $comment, 'ru' => $comment];
38 $home_data['comment'] = json_encode($json_arr_comment, JSON_UNESCAPED_UNICODE);
39 //COMMENT END
40
41 //ADDRESSES START
42 $address_arr = explode(",", $this->text($dom, '.address'));
43 $address_arr = array_map('trim', $address_arr);
44
45 $index_city = count($address_arr)-1;
46 $start_index_address = 0;
47
48 //$home_data['address'] = $address_arr[0];
49
50
51
52 if (!empty($address_arr[$index_city])) {
53 $id_region_3 = HomeParams::where('name', $address_arr[$index_city])->first();
54
55 if (empty($id_region_3)) {
56// $id_region_3 = HomeParams::create([
57// 'name' => $address_arr[3],
58// 'parent_id' => 0,
59// 'type_id' => 5
60// ]
61// );
62 $start_index_address = $index_city;
63 }else {
64
65 $id = $id_region_3['id'];
66
67 $home_and_params_data[] = ['param_id' => $id, 'name' => $address_arr[$index_city]];
68 }
69 }
70
71 if (!empty($address_arr[$index_city-1])) {
72 $id_region_2 = HomeParams::where('name', $address_arr[$index_city-1])->first();
73
74 if (empty($id_region_2)) {
75
76// $parent_id = empty($id_region_3) ? 0 : $id_region_3['id'];
77//
78// $id_region_2 = HomeParams::create([
79// 'name' => $address_arr[2],
80// 'parent_id' => 0,
81// 'type_id' => 5
82// ]
83// );
84 $start_index_address = $index_city-1;
85 }else {
86
87 $id = $id_region_2['id'];
88
89 $home_and_params_data[] = ['param_id' => $id, 'name' => $address_arr[$index_city-1]];
90 }
91 }
92
93 if (!empty($address_arr[$index_city-2])) {
94
95 $id_region_1 = HomeParams::where('name', $address_arr[$index_city-2])->first();
96
97 if (empty($id_region_1)) {
98
99// $id_region_1 = HomeParams::create([
100// 'name' => $address_arr[1],
101// 'parent_id' => $parent_id,
102// 'type_id' => 5
103// ]
104// );
105 $start_index_address = $index_city-2;
106 }else {
107 $id = $id_region_1['id'];
108 $home_and_params_data[] = ['param_id' => $id, 'name' => $address_arr[$index_city-2]];
109 }
110
111 }
112
113
114 $address = "";
115 for ($i=0; $i<=$start_index_address; $i++){
116 if($i!=$start_index_address) {
117 $address .= $address_arr[$i] . ",";
118 }else{
119 $address .= $address_arr[$i];
120 }
121 }
122
123 $address = str_replace("თბილისი,","",$address);
124 $home_data['address'] = $address;
125
126 //ADDRESSES END
127
128 //TYPE START
129 $title = trim($dom->find('.statement-title .mb-0')[0]->innertext);
130 if (strpos($title, 'ქირავდება დღიურად') !== false) {
131 $home_and_params_data[] = ['param_id' => $this->TYPE_QIRAVDEBA_DGIURAD, 'name' => 'ქირავდება დღიურად'];
132 $home_data['deal_type'] = $this->TYPE_QIRAVDEBA_DGIURAD;
133 } elseif (strpos($title, 'ქირავდება') !== false) {
134 $home_and_params_data[] = ['param_id' => $this->TYPE_QIRAVDEBA, 'name' => 'ქირავდება'];
135 $home_data['deal_type'] = $this->TYPE_QIRAVDEBA;
136 } elseif (strpos($title, 'იყიდება') !== false) {
137 $home_and_params_data[] = ['param_id' => $this->TYPE_IYIDEBA, 'name' => 'იყიდება'];
138 $home_data['deal_type'] = $this->TYPE_IYIDEBA;
139 } elseif (strpos($title, 'გირავდება') !== false) {
140 $home_and_params_data[] = ['param_id' => $this->TYPE_GIRAVDEBA, 'name' => 'გირავდება'];
141 $home_data['deal_type'] = $this->TYPE_GIRAVDEBA;
142 }
143
144 //TYPE END
145
146 //PRICE START
147 $price_gel = trim($dom->find('.price .d-block')[0]->{'data-price-gel'});
148 $home_data['price'] = str_replace(",", "", $price_gel);
149 $home_data['price_type'] = $this->PRICE_TYPE_FULL;
150 $home_data['price_currency'] = $this->CURRENCY_ID_GEL;
151 //PRICE END
152
153 //AREA START
154 $area = trim(strip_tags($dom->find('.space span')[0]->innertext));
155 $area = str_replace("ფართი:", "", $area);
156 $area = trim(str_replace("მ²", "", $area));
157 $home_data['total_square'] = $area;
158 //AREA END
159
160 //USER START
161 $digits = 5;
162 $rand_numb = rand(pow(10, $digits - 1), pow(10, $digits) - 1);
163 $username = $this->text($dom, '.agent .name');
164 $phone = str_replace(" ", "", $this->text($dom, '.agent .number'));
165
166 $home_data['mobile'] = $phone;
167
168 $user_data['name'] = $username;
169 $user_data['phone'] = $phone;
170 $user_data['email'] = $username . "_" . $rand_numb . "@gmail.com";
171 $user_data['password'] = sha1("pp9dss");
172
173 $user = \App\User::where('phone', $phone)->first();
174 if (empty($user)) {
175 $user = \App\User::create($user_data);
176 }
177
178 $home_data['user_id'] = $user->id;
179
180 //USER END
181
182 //PERSONS START
183 $persons = $this->text($dom, '.persons span');
184 if (!empty($persons)) {
185 $persons = preg_replace('/[^0-9]/', '', $persons);
186 if(!empty($persons) && is_numeric($persons)) {
187 $home_data['people_num'] = intval($persons);
188 }
189 }
190 //PERSONS END
191
192 //HOME TYPE START
193 $title = $this->text($dom, '.statement-title .mb-0');
194 $home_type = -1;
195 if (strpos($title, 'ბინა') !== false) {
196 $home_type = 985;
197 } elseif (strpos($title, 'სახლი') !== false || strpos($title, 'აგარაკი') !== false) {
198 $home_type = 986;
199 } elseif (strpos($title, 'კომერციული') !== false) {
200 $home_type = 987;
201 } elseif (strpos($title, 'ნაკვეთი') !== false) {
202 $home_type = 988;
203 } elseif (strpos($title, 'სასტუმრო') !== false) {
204 $home_type = 989;
205 }
206 $home_data['home_type'] = $home_type;
207 //HOME TYPE END
208
209
210 if ($home_type != 988) {
211 //MAIN FEATURES START
212 $main_features = [];
213 foreach ($dom->find('.main-features div div span') as $ftr) {
214 $main_features[] = trim(($ftr->innertext));
215 }
216
217
218 if ($home_type == 986) {
219
220 }
221 //BUILT TYPE START
222 if ($home_type == 985) { //BINA
223 $built_type = $main_features[0];
224 $built_type_id = $this->TYPE_NEW_MSHENEBARE;
225 if (strpos($built_type, 'ახალი') !== false) {
226 $built_type_id = $this->TYPE_NEW_BUILT;
227 }
228 if (strpos($built_type, 'ძველი') !== false) {
229 $built_type_id = $this->TYPE_OLD_BUILT;
230 }
231 $home_and_params_data[] = ['param_id' => $built_type_id, 'name' => $built_type];
232 }
233 //BUILT TYPE END
234
235 //ROOMS START
236
237 $rooms_obj = $dom->find('.main-features div')[2];
238 $rooms_obj_2 = $rooms_obj->find('span')[1];
239
240 $rooms = explode(" ", $rooms_obj_2->innertext)[0];
241 $home_data['rooms'] = $rooms;
242
243 //ROOMS END
244
245 //FLOORS START
246 $floors_index = count($main_features) - 2;
247
248 $floors = explode("/", $main_features[$floors_index]);
249 if (count($floors) > 1) {
250 $floors_current = $floors[0];
251 $floors_total = $floors[1];
252 } else {
253 $floors_current = $floors[0];
254 $floors_total = $floors[0];
255 }
256 $home_data['floor_current'] = $floors_current;
257 $home_data['floor_total'] = $floors_total;
258 //FLOORS END
259
260 //MAIN FEATURES END
261
262 }
263
264 //dd($main_features);
265
266 //AMENITIES START
267
268 $count = 0;
269 foreach ($dom->find('.amenities span.d-block') as $span) {
270 $count++;
271 $className = $span->getAttribute('class');
272 $span_content = trim($span->innertext);
273
274 $external = $span->next_sibling();
275
276 foreach ($this->amenities_numbered as $val) {
277 if (strpos($span_content, $val) !== false) {
278 $span_content = trim(strip_tags($span_content));
279 $splitvar = explode(" ", $span_content);
280 if (trim($splitvar[0]) == 'ვერანდა') {
281 if (!empty($splitvar[1])) {
282 $home_data['veranda_area'] = floatval(trim($splitvar[1]));
283 }
284 }
285 if (trim($splitvar[0]) == 'აივანი') {
286 if (!empty($splitvar[2])) {
287 $home_data['yard_area'] = floatval(trim($splitvar[2]));
288 }
289 }
290 if (trim($splitvar[0]) == 'ეზოს') {
291 if (!empty($splitvar[2])) {
292 $home_data['yard_area'] = floatval(trim($splitvar[2]));
293 }
294 }
295 if (trim($splitvar[0]) == 'ჭერის') {
296 if (!empty($splitvar[2])) {
297 $home_data['ceiling_height'] = floatval(trim($splitvar[2]));
298 }
299 }
300 if (trim($splitvar[0]) == 'საძინებელი') {
301 if (!empty($splitvar[1])) {
302 $home_data['bedrooms'] = intval(trim($splitvar[1]));
303 }
304 }
305 if (trim($splitvar[0]) == 'სველი') {
306 if (!empty($splitvar[2])) {
307 $home_data['toilet'] = intval(trim($splitvar[2]));
308 }
309 }
310 //echo $span_content . "<br>";
311 continue 2;
312 }
313
314 }
315
316 //dd($home_and_params_data);
317
318 if (strpos($className, 'no') == false) {
319 $available[] = trim($span_content);
320 if (!empty($external)) {
321 $available[] = trim(strip_tags($external->innertext));
322 }
323 } else {
324 $not_available[] = trim($span_content);
325 }
326 }
327
328 //dd($home_data);
329
330 //AMENITIES END
331 //dd($available);
332
333
334 if (empty($available))
335 return "home $home_id_myhome SKIPPED very low info.";
336
337 //PROCESS AMENITIES START
338 foreach ($available as $value) {
339
340 $home_and_params_data[] = ['name' => $value];
341
342// $value = $value == 'სამგზავრო ლიფტი' ? 'სამგზავრო' : $value;
343// $value = $value == 'სატვირთო ლიფტი' ? 'სატვირთო' : $value;
344// if ($value == 'ცენტრალური გათბობის სისტემა') {
345// $param_id = 1128;
346// $param_name = 'ცენტრალური გათბობა (წყალი)';
347// } else {
348// $param = HomeParams::where('name', $value)->first();
349//
350// if (empty($param)) {
351// if ((strpos($value, 'ადაპტირებული') !== false)) {
352// $home_data['is_addaptived'] = 1;
353// } elseif ((strpos($value, 'მანსარდა') !== false)) {
354// $home_data['is_mansard'] = 1;
355// } else {
356// echo "$url $value Not Found \n";
357// }
358//
359//// CREATE NEW PARAM IF NOT FOUND
360//
361//// $param = HomeParams::create([
362//// 'name' => $value,
363//// 'parent_id' => 0,
364//// 'type_id' => 7
365//// ]
366//// );
367// }
368//
369// $param_id = $param['id'];
370// $param_name = $value;
371// }
372//
373// $home_and_params_data[] = ['param_id' => $param_id, 'name' => $param_name];
374 }
375 //PROCESS AMENITIES END
376
377 //CREATE HOME AND HOME PARAMS
378
379 $home_data['status'] = 1;
380
381 var_dump($home_data);
382 var_dump($home_and_params_data);
383 //$home = Home::create($home_data);
384
385 die();
386
387
388
389 //END
390
391 //IMAGES START
392
393
394 $images = [];
395 if(empty($dom->find('.images.large'))){
396 //$home->delete();
397 return "HOME $home_id_myhome SKIPPED NO IMAGES";
398 }
399 foreach ($dom->find('.images.large') as $div) {
400 $image_large = $div->{'data-background'};
401 $file_current = pathinfo($image_large);
402
403 if (@getimagesize($image_large) && @getimagesize(str_replace("large", "thumbs", $image_large))){
404 if(strpos($image_large,"youtube")===false) {
405
406 $images["large"][] = [
407 "url" => $image_large,
408 "filename" => str_limit(md5($file_current["basename"])) . '.jpg'
409 ];
410
411 $images["thumb"][] = [
412 "url" => str_replace("large", "thumbs", $image_large),
413 "filename" => str_limit(md5($file_current["basename"])) . '.jpg'
414 ];
415
416 }else{
417 // $home->delete();
418 return "HOME $home_id_myhome SKIPPED VIDEO INSTEAD OF IMAGES";
419 }
420 }else{
421 //$home->delete();
422 return "HOME $home_id_myhome SKIPPED NO IMAGES";
423 }
424 }
425
426 foreach ($home_and_params_data as $home_param) {
427// HomeAndParam::create([
428// 'home_id' => $home['id'],
429// 'param_id' => $home_param['param_id']
430// ]
431// );
432 }
433
434 if(!isset($images["large"]))
435 return "Something wrong no images found!.".PHP_EOL;
436
437
438 $this->downloadImages($images["large"],$home['id']);
439 $this->downloadImages($images["thumb"],$home['id'].'/'.'thumbnail');
440
441 foreach($images["large"] as $k=>$l){
442 $file = public_path("assets/uploads/homes/" . $home['id'] . "/" . $l["filename"]);
443
444 $img_large = Image::make($file);
445 $img_large->crop($img_large->width(), intval($img_large->height() * 0.90), 0, 0)
446 ->save();
447
448 HomeImages::create([
449 'home_id' => $home['id'],
450 'filename' => $l["filename"],
451 "order" => $k
452 ]);
453 }
454
455 //IMAGES END
456
457 HomeParsed::create([
458 'home_id' => $home['id'],
459 'external_id' => $home_id_myhome,
460 'site' => 'myhome.ge'
461 ]);
462
463
464 Artisan::call('cache:clear');
465
466 return "DONE! successfully parsed ".$home['id']." home $home_id_myhome and ".count($images["large"])." images.";
467
468 }