diff --git a/scripts/common.php b/scripts/common.php index 5798442..50c52d4 100644 --- a/scripts/common.php +++ b/scripts/common.php @@ -89,6 +89,20 @@ function debug_log($message) { error_log($message . "\n", 3, $_SERVER['DOCUMENT_ROOT'] . "/debug_log.log"); } +function get_com_en_name($sci_name) { + if (!isset($_labels_flickr)) { + $_labels_flickr = file(get_home() . "/BirdNET-Pi/model/labels_flickr.txt"); + } + $engname = null; + foreach ($_labels_flickr as $label) { + if (strpos($label, $sci_name) !== false) { + $engname = trim(explode("_", $label)[1]); + break; + } + } + return $engname; +} + define('DB', './scripts/flickr.db'); class Flickr { @@ -186,7 +200,7 @@ class Flickr { } private function get_from_flickr($sci_name) { - $engname = $this->get_com_en_name($sci_name); + $engname = get_com_en_name($sci_name); $flickrjson = json_decode(file_get_contents("https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=" . $this->flickr_api_key . "&text=" . str_replace(" ", "%20", $engname) . $this->comnameprefix . "&sort=relevance" . $this->args . "&per_page=5&media=photos&format=json&nojsoncallback=1"), true)["photos"]["photo"]; // could be null!! @@ -245,18 +259,4 @@ class Flickr { $this->set_uid_in_db($uid); } - private function get_com_en_name($sci_name) { - if ($this->labels_flickr === null) { - $this->labels_flickr = file(get_home() . "/BirdNET-Pi/model/labels_flickr.txt"); - } - $engname = null; - foreach ($this->labels_flickr as $label) { - if (strpos($label, $sci_name) !== false) { - $engname = trim(explode("_", $label)[1]); - break; - } - } - return $engname; - } - } diff --git a/scripts/stats.php b/scripts/stats.php index f4c74a5..2eeea57 100644 --- a/scripts/stats.php +++ b/scripts/stats.php @@ -183,17 +183,7 @@ while($results=$result3->fetchArray(SQLITE3_ASSOC)){ $config = get_config(); if (! empty($config["FLICKR_API_KEY"])) { - // only open the file once per script execution - if(!isset($lines)) { - $lines = file($home."/BirdNET-Pi/model/labels_flickr.txt"); - } - // convert sci name to English name - foreach($lines as $line){ - if(strpos($line, $results['Sci_Name']) !== false){ - $engname = trim(explode("_", $line)[1]); - break; - } - } + $engname = get_com_en_name($results['Sci_Name']); $flickrjson = json_decode(file_get_contents("https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=".$config["FLICKR_API_KEY"]."&text=\"".str_replace(' ', '%20', $engname)."\"&license=2%2C3%2C4%2C5%2C6%2C9&sort=relevance&per_page=15&format=json&nojsoncallback=1"), true)["photos"]["photo"];