From 9dbba182ae34152af43a554b5e2e22b6cebae038 Mon Sep 17 00:00:00 2001 From: askuric Date: Thu, 11 Jul 2024 10:25:00 +0200 Subject: [PATCH] renamed index --- index.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 index.php diff --git a/index.php b/index.php new file mode 100644 index 0000000..cb42a12 --- /dev/null +++ b/index.php @@ -0,0 +1,52 @@ +\s+\(latest\)/', $str, $matches); + return $matches[1]; +} + +function get_name($str) { + preg_match('/

(.*?)<\/h1>/', $str, $matches); + return $matches[1]; +} + +if (!isset($_GET["lib"])) { + die; +} + +$lib = $_GET["lib"]; +$cache_file = '/tmp/arduino_lib_' . md5($lib) . '.cache'; + +if (file_exists($cache_file) && (time() - filemtime($cache_file) < 86000)) { + // 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);