-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
52 lines (44 loc) · 1.66 KB
/
index.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
<?php
header('Content-Type: image/svg+xml');
header('Cache-Control: no-cache');
function get_version($str) {
preg_match('/(\d+\.\d+\.\d+)<\/a>\s+\(latest\)/', $str, $matches);
return $matches[1];
}
function get_name($str) {
preg_match('/<h1>(.*?)<\/h1>/', $str, $matches);
return $matches[1];
}
if (!isset($_GET["lib"])) {
$lib = "No lib specified";
}else{
$lib = $_GET["lib"];
}
$cache_file = '/tmp/arduino_lib_' . md5($lib) . '.cache';
if (file_exists($cache_file) && (time() - filemtime($cache_file) < 6*60*60)) { // if 6 hours has passed from the last check
// Use cached version if it's less than 1 day
$data = json_decode(file_get_contents($cache_file), true);
$name = $data['name'];
$version = $data['version'];
} else {
$url = "https://www.arduino.cc/reference/en/libraries/" . strtolower(str_replace(" ", "-", $lib)) . "/";
$website = @file_get_contents($url);
if ($website === false) {
$badge_url = "https://img.shields.io/badge/Library%20Manager-" . urlencode($lib) . "-red?logo=arduino";
echo file_get_contents($badge_url);
die;
}
$version = get_version($website);
$name = get_name($website);
if ($name && $version) {
// Cache the results
$data = ['name' => $name, 'version' => $version];
file_put_contents($cache_file, json_encode($data));
}
}
if (!$name || !$version) {
$badge_url = "https://img.shields.io/badge/Library%20Manager-" . urlencode($lib) . "-red?logo=arduino";
} else {
$badge_url = "https://img.shields.io/badge/Library%20Manager-" . $name . "%20" . $version . "-green?logo=arduino&color=%233C1";
}
echo file_get_contents($badge_url);