|
| 1 | +<?php |
| 2 | +header('Content-Type: image/svg+xml'); |
| 3 | + |
| 4 | +function get_version($str) { |
| 5 | + preg_match('/(\d+\.\d+\.\d+)<\/a>\s+\(latest\)/', $str, $matches); |
| 6 | + return $matches[1]; |
| 7 | +} |
| 8 | + |
| 9 | +function get_name($str) { |
| 10 | + preg_match('/<h1>(.*?)<\/h1>/', $str, $matches); |
| 11 | + return $matches[1]; |
| 12 | +} |
| 13 | + |
| 14 | +if (!isset($_GET["lib"])) { |
| 15 | + die; |
| 16 | +} |
| 17 | + |
| 18 | +$lib = $_GET["lib"]; |
| 19 | +$cache_file = '/tmp/arduino_lib_' . md5($lib) . '.cache'; |
| 20 | + |
| 21 | +if (file_exists($cache_file) && (time() - filemtime($cache_file) < 86000)) { |
| 22 | + // Use cached version if it's less than 1 day |
| 23 | + $data = json_decode(file_get_contents($cache_file), true); |
| 24 | + $name = $data['name']; |
| 25 | + $version = $data['version']; |
| 26 | +} else { |
| 27 | + $url = "https://www.arduino.cc/reference/en/libraries/" . strtolower(str_replace(" ", "-", $lib)) . "/"; |
| 28 | + $website = @file_get_contents($url); |
| 29 | + |
| 30 | + if ($website === false) { |
| 31 | + $badge_url = "https://img.shields.io/badge/Library%20Manager-" . urlencode($lib) . "-red?logo=arduino"; |
| 32 | + echo file_get_contents($badge_url); |
| 33 | + die; |
| 34 | + } |
| 35 | + |
| 36 | + $version = get_version($website); |
| 37 | + $name = get_name($website); |
| 38 | + |
| 39 | + if ($name && $version) { |
| 40 | + // Cache the results |
| 41 | + $data = ['name' => $name, 'version' => $version]; |
| 42 | + file_put_contents($cache_file, json_encode($data)); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +if (!$name || !$version) { |
| 47 | + $badge_url = "https://img.shields.io/badge/Library%20Manager-" . urlencode($lib) . "-red?logo=arduino"; |
| 48 | +} else { |
| 49 | + $badge_url = "https://img.shields.io/badge/Library%20Manager-" . $name . "%20" . $version . "-green?logo=arduino&color=%233C1"; |
| 50 | +} |
| 51 | + |
| 52 | +echo file_get_contents($badge_url); |
0 commit comments