forked from NiklasSchmitt/wsc-dl-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_data.php
64 lines (58 loc) · 2.29 KB
/
fetch_data.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
54
55
56
57
58
59
60
61
62
63
64
<?php
include_once("config.inc.php");
// fetch_data.php
// this script, fetch download-stats from https://pluginstore.woltlab.com/file/XXXX and save it into an mysql-database
if ($_SERVER["REQUEST_METHOD"] == "GET" && !empty(validate_form_input($_GET['id'])) ) {
$id = $_GET['id'];
if ($id == "all") {
$stmt = mysqli_prepare($mysqli, "SELECT count(*) FROM `plugins` ");
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt, $count); mysqli_stmt_fetch($stmt);
for($i=1; $i <= $count; $i++) {
echo "<p>Fetching Data for Plugin-ID $i: <br>";
fetch_plugin($mysqli, $i);
echo "</p>";
}
} else {
fetch_plugin($mysqli, $id);
}
}
echo "<p><a href='index.php'>Back</a></p>";
function fetch_plugin($mysqli, $plugin) {
$stmt = mysqli_prepare($mysqli, "SELECT `pluginstore` FROM `plugins` WHERE `id` = ? LIMIT 1");
mysqli_stmt_bind_param($stmt, "i", $plugin); mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt); // use this, if you just want to use num_rows
if ($stmt->num_rows == 1) {
mysqli_stmt_bind_result($stmt, $pluginstore_id); mysqli_stmt_fetch($stmt); // use this, if you want to select one row and get the result
$page = get_url("https://pluginstore.woltlab.com/file/".$pluginstore_id);
if (preg_match ('/(\d*)\sDownloads/', $page, $arr)) {
$downloads = $arr[1];
}
$stmt2 = mysqli_prepare($mysqli, "INSERT INTO `stats` (`id`, `date`, `value`) VALUES (?, CURDATE(), ?)");
mysqli_stmt_bind_param($stmt2, "ii", $plugin, $downloads);
if (mysqli_stmt_execute($stmt2)) {
return $downloads;
} else {
echo "Could not insert stats for plugin-id ".$plugin.": ".mysqli_error($mysqli);
return 0;
}
} else {
echo "No plugin found with id $plugin!";
return 0;
}
}
function get_url($request_url) {
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $request_url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 0);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl_handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
$JsonResponse = curl_exec($curl_handle);
$http_code = curl_getinfo($curl_handle);
return($JsonResponse);
}
?>