-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
106 lines (89 loc) · 2.88 KB
/
main.php
File metadata and controls
106 lines (89 loc) · 2.88 KB
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
<?php
use League\Csv\Exception as CSVException;
use League\Csv\Reader;
use League\Csv\Writer;
use Hotel\RoomType;
require_once "vendor/autoload.php";
$rakuten = new Hotel\RakutenSearch();
$rakuten->loadConfig('config.yaml');
$io = new IOManager();
$hotelNos = $io->getHotelsFromHotelNoCSV('hotel.csv');
$searchDate = $io->getSearchDateFromDateCSV('date.csv');
$csvData = [];
$roomType = [[RoomType::NON],[RoomType::DOUBLE, RoomType::SEMI_DOUBLE],[RoomType::TWIN]];
$avoidWord = ['シニア','バースデ', '誕生日', 'レイト', 'レディース', '18時', '19時'];
foreach ($hotelNos as $no) {
$hotel = $rakuten->getHotel($no);
$time = 0;
$csvRow = [];
$csvRow[] = $hotel->getHotelName();
foreach ($searchDate as $date) {
$plan = $rakuten->getBestPricePlan($hotel, $date, $avoidWord, 1, $roomType[0]);
$price = isset($plan) ? $plan->getTotalCharge() : 0;
$csvRow[] = $price;
if ($time % 2 == 0) sleep(1);
$time++;
}
$csvData[] = $csvRow;
$csvRow = [];
$csvRow[] = $hotel->getHotelName();
foreach ($searchDate as $date) {
$plan = $rakuten->getBestPricePlan($hotel, $date, $avoidWord, 2, $roomType[1]);
$price = isset($plan) ? $plan->getTotalCharge() : 0;
$csvRow[] = $price;
if ($time % 2 == 0) sleep(1);
$time++;
}
$csvData[] = $csvRow;
$csvRow = [];
$csvRow[] = $hotel->getHotelName();
foreach ($searchDate as $date) {
$plan = $rakuten->getBestPricePlan($hotel, $date, $avoidWord, 2, $roomType[2]);
$price = isset($plan) ? $plan->getTotalCharge() : 0;
$csvRow[] = $price;
if ($time % 2 == 0) sleep(1);
$time++;
}
$csvData[] = $csvRow;
}
$file = new SplTempFileObject();
$writer = Writer::createFromFileObject($file);
$writer->insertAll($csvData);
$fileName = 'price.csv';
$csvStr = $writer->getContent();
file_put_contents($fileName, $csvStr);
class IOManager
{
public function getHotelsFromHotelNoCSV(string $filePath): array
{
$hotelNos = [];
try {
$reader = Reader::createFromPath($filePath, 'r');
$records = $reader->getRecords();
foreach ($records as $idx => $row) {
if ($idx === 0) {
continue;
}
$hotelNos[] = $row[0];
}
} catch (CSVException $e) {
}
return $hotelNos;
}
public function getSearchDateFromDateCSV(string $filePath): array
{
$date = [];
try {
$reader = Reader::createFromPath($filePath, 'r');
$records = $reader->getRecords();
foreach ($records as $idx => $row) {
try {
$date[] = new DateTime($row[0]);
} catch (\Exception $e) {
}
}
} catch (CSVException $e) {
}
return $date;
}
}