-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuelScraping.php
53 lines (50 loc) · 1.47 KB
/
fuelScraping.php
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
#!/usr/bin/php
<?php
function getPrice($type, $page) {
$regex = "/{$type}.*?(\d.\d\d)/s";
preg_match($regex, $page, $match);
if (!isset($match[1])) return false;
return $match[1];
}
function query($query) {
$db = new mysqli("localhost", "username", "password", "db")
or die("Unable to connect to database");
$result = $db->query($query);
if ($result instanceof mysqli_result) {
$results_array = array();
while ($row = $result->fetch_assoc()) {
$results_array[] = $row;
}
$result->close();
$result = $results_array;
}
$db->close();
return $result;
}
$url = "http://www.gasbuddy.com/Station/124875";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
curl_close($ch);
$prices = $existing_prices = $counts = array();
$raw = "";
$prices['Regular'] = getPrice("Regular", $page);
$prices['Premium'] = getPrice("Premium", $page);
$prices['Diesel'] = getPrice("Diesel", $page);
$data = query("SELECT * FROM prices");
foreach ($data as $d) {
$existing_prices[$d['type']] = $d;
}
foreach($prices as $type => $price) {
if ($price === false) continue;
if (!isset($existing_prices[$type])) {
$counts[] = query("INSERT INTO prices (type, price) VALUES ('{$type}', {$price})");
} elseif ($existing_prices[$type]['price'] != $price) {
$counts[] = query("UPDATE prices SET price={$price} WHERE type='{$type}'");
}
$raw .= "{$type}\t\${$price}\n";
}
if (empty($counts)) exit();
exec("~/sendSlackMessage.sh fuel web '{$raw}'");
print_r($raw);
?>