Răsfoiți Sursa

Parser configuration parameters, some small fixes and better output.

Iñigo Valentin 5 ani în urmă
părinte
comite
1939949e06
1 a modificat fișierele cu 211 adăugiri și 117 ștergeri
  1. 211 117
      parser/swarfarm_parser

+ 211 - 117
parser/swarfarm_parser

@@ -16,6 +16,13 @@
      */
     CONST PATH = __DIR__ . "/../";
 
+    $args = array(
+        "verbosity" => 3,
+        "override_images" => false,
+        "skip_downloads" => false,
+        "only_downloads" => false,
+        "only_new" => false
+    );
     /**
      * Script verbosity
      *
@@ -28,22 +35,59 @@
      */
     $verbosity = 4;
 
+    $override_images = false;
+
+    $skip_downloads = false;
+
     /**
      * Echoes an output message to stdout, depending on the verbosity level.
      *
      * @param int $level Message priority.
      * @param string $message content.
      */
-    function output($level, $message){
-        global $verbosity;
-        if ($level <= $verbosity){
-            if (substr("testers", -1) != PHP_EOL){
+    function output($level, $message, $keep_format = false){
+        global $args;
+        if ($level <= $args["verbosity"]){
+            if ($keep_format == false){
+                $message = str_replace("\n", " ", $message);
+                while (strpos($message, "  ") !== false){
+                    $message = str_replace("  ", " ", $message);
+                }
+            }
+            if (substr($message, -1) != PHP_EOL && $keep_format == false){
                 $message .= PHP_EOL;
             }
             echo $message;
         }
     }
 
+    /**
+     * Downloads a file.
+     *
+     * @param string $url The file to download.
+     * @param string $target File destination.
+     * @return boolean true on success, false on error.
+     */
+    function download($url, $target){
+        global $args;
+        if ($args["skip_downloads"] == true){
+            return true;
+        }
+        if (!file_exists($target) || $args["override_images"] == true){
+            output(4, "Downloading file '$url' to: '$target'");
+            if (!file_put_contents($target, file_get_contents($url))) { 
+                output(1, "Error downloading file '$url' to: '$target'");
+                return false;
+            }
+            else{
+                return true;
+            }
+        }
+        else{
+            return true;
+        }
+    }
+
     /**
      * Gets an entity com2us_id from it's Swarfarm id.
      *
@@ -127,10 +171,39 @@
     }
 
     /**
-     * Gets an archetype ID from it's name
+     * Prints the help message.
+     */
+    function show_help(){
+        echo "\nSwarfarm parser\n\n";
+        echo "Refreshes data using the Swarfarm API.\n\n";
+        echo "Usage:\n\n";
+        echo "    swarfarm_parser <options...>\n\n";
+        echo "Options:\n\n";
+        echo " +--------+------------------+---------------------------------------------------+\n";
+        echo " | -s     | --skip-downloads |  Don't download images.                           |\n";
+        echo " +--------+------------------+---------------------------------------------------+\n";
+        echo " | -o     | --only-downloads |  Download images, but don't make changes to       |\n";
+        echo " |        |                  |  the database.                                    |\n";
+        echo " +--------+------------------+---------------------------------------------------+\n";
+        echo " | -n     | --only-new       |  Look for new units or skills, but don't update   |\n";
+        echo " |        |                  |  existing ones.                                   |\n";
+        echo " +--------+------------------+---------------------------------------------------+\n";
+        echo " | -v [n] | --verbosity [n]  |  Controls the script output to the console.       |\n";
+        echo " |        |                  |  Values for [n]:                                  |\n";
+        echo " |        |                  |    0: Only show critical errors.                  |\n";
+        echo " |        |                  |    1: Only error messages.                        |\n";
+        echo " |        |                  |    2: Only error and warning messages.            |\n";
+        echo " |        |                  |    3: Standard output.                            |\n";
+        echo " |        |                  |    4: Include debug messages.                     |\n";
+        echo " |        |                  |    5: Print traces (lots of lines!).              |\n";
+        echo " +--------+------------------+---------------------------------------------------+\n";
+    }
+
+    /**
+     * Establishes a connection with the database.
      *
      * @param string $name Archetype name (case insensitive).
-     * @return int Archetype ID, -1 on unknown archetype.
+     * @return resource Database connection
      */
     function connect_to_database(){
         // Connect to the database
@@ -141,10 +214,91 @@
         }
         // Disable integrity
         $db->query("PRAGMA foreign_keys=OFF;");
+        output(4, "Connected to databse: " . PATH . "application/sw.sqlite");
         return $db;
     }
 
-    // TODO: Parse arguments
+    // Parse arguments
+    $accepted = array(
+      "--verbosity", "-v",
+      "--skip-downloads", "-s",
+      "--only-downloads", "-d",
+      "--only-new", "-n",
+      "--override-images", "-o",
+      "--help", "-h"
+    );
+    for ($i = 1; $i < count($argv); $i++){
+        if ($argv[$i] == "--help" || $argv[$i] == "-h"){
+            show_help();
+            return 0;
+        }
+        elseif (!in_array($argv[$i], $accepted)){
+            echo "Unrecognized option " . $argv[$i] . "\n";
+            return -1;
+        }
+        elseif ($argv[$i] == "--verbosity" || $argv[$i] == "-v"){
+            if (count($argv) >= $i + 2 && is_int($argv[$i + 1]) && intval($argv[$i + 1]) >= 0 && intval($argv[$i + 1]) <= 5){
+                $args["verbosity"] = intval($argv[$i + 1]);
+                $i ++;
+            }
+            else{
+                echo "Unrecognized value for parameter " . $argv[$i] . ": " . $argv[$i + 1] . "\n";
+                echo "Accepted values are:\n";
+                echo "  0 (critical)\n";
+                echo "  1 (error)\n";
+                echo "  2 (warning)\n";
+                echo "  3 (info)\n";
+                echo "  4 (debug)\n";
+                echo "  5 (trace)\n";
+            }
+        }
+        elseif ($argv[$i] == "--skip-downloads" || $argv[$i] == "-s"){
+            if (in_array("-d", $argv)){
+                echo "Conflicting arguments: '-d' and '" . $argv[$i] . "'\n";
+                return -1;
+            }
+            elseif (in_array("--only-downloads", $argv)){
+                echo "Conflicting arguments: '--only-downloads' and '" . $argv[$i] . "'\n";
+                return -1;
+            }
+            else{
+                $args["skip_downloads"] = true;
+            }
+        }
+        elseif ($argv[$i] == "--only-downloads" || $argv[$i] == "-s"){
+            if (in_array("-s", $argv)){
+                echo "Conflicting arguments: '-s' and '" . $argv[$i] . "'\n";
+                return -1;
+            }
+            elseif (in_array("--skip-downloads", $argv)){
+                echo "Conflicting arguments: '--skip-downloads' and '" . $argv[$i] . "'\n";
+                return -1;
+            }
+            else{
+                $args["only_downloads"] = true;
+            }
+        }
+        elseif ($argv[$i] == "--only-new" || $argv[$i] == "-n"){
+            $args["only_new"] = true;
+        }
+        elseif ($argv[$i] == "--override-images" || $argv[$i] == "-o"){
+            if (in_array("-s", $argv)){
+                echo "Conflicting arguments: '-s' and '" . $argv[$i] . "'\n";
+                return -1;
+            }
+            elseif (in_array("--skip-downloads", $argv)){
+                echo "Conflicting arguments: '--skip-downloads' and '" . $argv[$i] . "'\n";
+                return -1;
+            }
+            else{
+                $args["override_images"] = true;
+            }
+        }
+    }
+    output(4, "Resolved arguments:");
+    foreach($args as $key => $value) {
+        output(4, "  $key => " . var_export($value, true));
+    }
 
     // Connect to the database
     $db = connect_to_database();
@@ -202,15 +356,11 @@
                 $statement->bindValue(":aoe", $skill->aoe, SQLITE3_INTEGER);
                 $statement->bindValue(":max_level", $skill->max_level, SQLITE3_INTEGER);
                 $statement->bindValue(":multiplier_formula", $skill->multiplier_formula, SQLITE3_INTEGER);
-                output(4, "Execute query: ");
-                output(4, $statement->getSQL(true));
+                output(4, "QUERY: " . $statement->getSQL(true));
                 $statement->execute();
                 $url = 'https://swarfarm.com/static/herders/images/skills/' . $skill->icon_filename;
                 $out = PATH . "public/img/skill/" . sprintf('%08d', $skill->com2us_id) . ".png";
-                output(4, "Downloading file '$url' to: '$out'");
-                if (!file_put_contents($out, file_get_contents($url))) { 
-                    output(1, "Error downloading file '$url' to: '$out'");
-                }
+                download($url, $out);
                 $level = 1;
                 $statement = $db->prepare("INSERT INTO k_skill_level (skill, level, description) VALUES (:skill, :level, :description);");
                 $statement->bindValue(":skill", $skill->com2us_id, SQLITE3_INTEGER);
@@ -221,8 +371,7 @@
                     $statement->bindValue(":skill", $skill->com2us_id, SQLITE3_INTEGER);
                     $statement->bindValue(":level", $level, SQLITE3_INTEGER);
                     $statement->bindValue(":description", str_replace("{0}", $upgrade->amount, $upgrade->effect), SQLITE3_TEXT);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 // TODO: Get data for other tables: 
@@ -234,40 +383,35 @@
                     $statement = $db->prepare("UPDATE k_skill SET name = :name WHERE id = :id;");
                     $statement->bindValue(":name", $skill->name, SQLITE3_TEXT);
                     $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["description"] != $skill->description){
                     $statement = $db->prepare("UPDATE k_skill SET description = :description WHERE id = :id;");
                     $statement->bindValue(":description", $skill->description, SQLITE3_TEXT);
                     $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["slot"] != $skill->slot){
                     $statement = $db->prepare("UPDATE k_skill SET slot = :slot WHERE id = :id;");
                     $statement->bindValue(":slot", $skill->slot, SQLITE3_TEXT);
                     $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["cooltime"] != $skill->cooltime){
                     $statement = $db->prepare("UPDATE k_skill SET cooltime = :cooltime WHERE id = :id;");
                     $statement->bindValue(":cooltime", $skill->cooltime, SQLITE3_TEXT);
                     $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["hits"] != $skill->hits){
                     $statement = $db->prepare("UPDATE k_skill SET hits = :hits WHERE id = :id;");
                     $statement->bindValue(":hits", $skill->hits, SQLITE3_TEXT);
                     $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 $passive = ($skill->passive == 1) ? (1) : (0);
@@ -275,8 +419,7 @@
                     $statement = $db->prepare("UPDATE k_skill SET passive = :passive WHERE id = :id;");
                     $statement->bindValue(":passive", $passive, SQLITE3_TEXT);
                     $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 $aoe = ($skill->aoe == 1) ? (1) : (0);
@@ -284,36 +427,28 @@
                     $statement = $db->prepare("UPDATE k_skill SET aoe = :aoe WHERE id = :id;");
                     $statement->bindValue(":aoe", $aoe, SQLITE3_TEXT);
                     $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["max_level"] != $skill->max_level){
                     $statement = $db->prepare("UPDATE k_skill SET max_level = :max_level WHERE id = :id;");
                     $statement->bindValue(":max_level", $skill->max_level, SQLITE3_TEXT);
                     $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["multiplier_formula"] != $skill->multiplier_formula){
                     $statement = $db->prepare("UPDATE k_skill SET multiplier_formula = :multiplier_formula WHERE id = :id;");
                     $statement->bindValue(":multiplier_formula", $skill->multiplier_formula, SQLITE3_TEXT);
                     $statement->bindValue(":id", $skill->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
 
                 // Image download
                 $out = PATH . "public/img/skill/" . sprintf('%08d', $skill->com2us_id) . ".png";
-                if (!file_exists($out)){
-                    $url = 'https://swarfarm.com/static/herders/images/skills/' . $skill->icon_filename;   
-                    output(4, "Downloading file '$url' to: '$out'");
-                    if (!file_put_contents($out, file_get_contents($url))) { 
-                        output(1, "Error downloading file '$url' to: '$out'");
-                    } 
-                }
+                $url = 'https://swarfarm.com/static/herders/images/skills/' . $skill->icon_filename;   
+                download($url, $out);
             }
         }
         //echo "NEXTPAGE: " . $next;
@@ -430,28 +565,23 @@
                 $statement->bindValue(":max_lvl_hp", $unit->max_lvl_hp, SQLITE3_INTEGER);
                 $statement->bindValue(":max_lvl_attack", $unit->max_lvl_attack, SQLITE3_INTEGER);
                 $statement->bindValue(":max_lvl_defense", $unit->max_lvl_defense, SQLITE3_INTEGER);
-                $statement->bindValue(":awakens_from", get_com2us_id($unit->awakens_from), SQLITE3_INTEGER);
+                $statement->bindValue(":awakens_from", get_com2us_id($unit->awakens_from), SQLITE3_TEXT);
                 $statement->bindValue(":awakens_to", get_com2us_id($unit->awakens_to), SQLITE3_TEXT);
                 $statement->bindValue(":fusion_food", $unit->fusion_food, SQLITE3_INTEGER);
                 $statement->bindValue(":homunculus", $unit->homunculus, SQLITE3_INTEGER);
                 $statement->bindValue(":craft_cost", 0, SQLITE3_INTEGER);
-                output(4, "Execute query: ");
-                output(4, $statement->getSQL(true));
+                output(4, "QUERY: " . $statement->getSQL(true));
                 $statement->execute();
 
                 $url = 'https://swarfarm.com/static/herders/images/monsters/' . $unit->image_filename;   
                 $out = PATH . "public/img/unit/" . sprintf('%08d', $unit->com2us_id) . ".png";
-                output(4, "Downloading file '$url' to: '$out'");
-                if (!file_put_contents($out, file_get_contents($url))) { 
-                    output(1, "Error downloading file '$url' to: '$out'");
-                }
+                download($url, $out);
 
                 foreach ($unit->skills as $skill){
                     $statement = $db->prepare("INSERT INTO k_unit_skill (unit, skill) VALUES (:unit, :skill);");
                     $statement->bindValue(":unit", $unit->com2us_id, SQLITE3_INTEGER);
                     $statement->bindValue(":skill", $skill, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 // TODO: Get data for other tables: 
@@ -464,16 +594,14 @@
                     $statement = $db->prepare("UPDATE k_unit SET family = :family WHERE id = :id;");
                     $statement->bindValue(":family", $unit->family_id, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["name"] != $unit->name){
                     $statement = $db->prepare("UPDATE k_unit SET name = :name WHERE id = :id;");
                     $statement->bindValue(":name", $unit->name, SQLITE3_TEXT);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 $element = get_element_id($unit->element);
@@ -481,8 +609,7 @@
                     $statement = $db->prepare("UPDATE k_unit SET element = :element WHERE id = :id;");
                     $statement->bindValue(":element", $element, SQLITE3_TEXT);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 $archetype = get_archetype_id($unit->archetype);
@@ -490,168 +617,147 @@
                     $statement = $db->prepare("UPDATE k_unit SET archetype = :archetype WHERE id = :id;");
                     $statement->bindValue(":archetype", $archetype, SQLITE3_TEXT);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["base_stars"] != $unit->base_stars){
                     $statement = $db->prepare("UPDATE k_unit SET base_stars = :base_stars WHERE id = :id;");
                     $statement->bindValue(":base_stars", $unit->base_stars, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["natural_stars"] != $unit->natural_stars){
                     $statement = $db->prepare("UPDATE k_unit SET natural_stars = :natural_stars WHERE id = :id;");
                     $statement->bindValue(":natural_stars", $unit->natural_stars, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["obtainable"] != $unit->obtainable){
                     $statement = $db->prepare("UPDATE k_unit SET obtainable = :obtainable WHERE id = :id;");
                     $statement->bindValue(":obtainable", $unit->obtainable, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["can_awaken"] != $unit->can_awaken){
                     $statement = $db->prepare("UPDATE k_unit SET can_awaken = :can_awaken WHERE id = :id;");
                     $statement->bindValue(":can_awaken", $unit->can_awaken, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["awaken_bonus"] != $unit->awaken_bonus && $unit->awaken_bonus != 0){
                     $statement = $db->prepare("UPDATE k_unit SET awaken_bonus = :awaken_bonus WHERE id = :id;");
                     $statement->bindValue(":awaken_bonus", $unit->awaken_bonus, SQLITE3_TEXT);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_TEXT);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["skill_ups_to_max"] != $unit->skill_ups_to_max){
                     $statement = $db->prepare("UPDATE k_unit SET skill_ups_to_max = :skill_ups_to_max WHERE id = :id;");
                     $statement->bindValue(":skill_ups_to_max", $unit->skill_ups_to_max, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["hp"] != $unit->base_hp){
                     $statement = $db->prepare("UPDATE k_unit SET hp = :hp WHERE id = :id;");
                     $statement->bindValue(":hp", $unit->base_hp, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["attack"] != $unit->base_attack){
                     $statement = $db->prepare("UPDATE k_unit SET attack = :attack WHERE id = :id;");
                     $statement->bindValue(":attack", $unit->base_attack, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["defense"] != $unit->base_defense){
                     $statement = $db->prepare("UPDATE k_unit SET defense = :defense WHERE id = :id;");
                     $statement->bindValue(":defense", $unit->base_defense, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["speed"] != $unit->speed){
                     $statement = $db->prepare("UPDATE k_unit SET speed = :speed WHERE id = :id;");
                     $statement->bindValue(":speed", $unit->speed, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["crit_rate"] != $unit->crit_rate){
                     $statement = $db->prepare("UPDATE k_unit SET crit_rate = :crit_rate WHERE id = :id;");
                     $statement->bindValue(":crit_rate", $unit->crit_rate, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["crit_damage"] != $unit->crit_damage){
                     $statement = $db->prepare("UPDATE k_unit SET crit_damage = :crit_damage WHERE id = :id;");
                     $statement->bindValue(":crit_damage", $unit->crit_damage, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["resistance"] != $unit->resistance){
                     $statement = $db->prepare("UPDATE k_unit SET resistance = :resistance WHERE id = :id;");
                     $statement->bindValue(":resistance", $unit->resistance, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["accuracy"] != $unit->accuracy){
                     $statement = $db->prepare("UPDATE k_unit SET accuracy = :accuracy WHERE id = :id;");
                     $statement->bindValue(":accuracy", $unit->accuracy, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["raw_hp"] != $unit->raw_hp){
                     $statement = $db->prepare("UPDATE k_unit SET raw_hp = :raw_hp WHERE id = :id;");
                     $statement->bindValue(":raw_hp", $unit->raw_hp, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["raw_attack"] != $unit->raw_attack){
                     $statement = $db->prepare("UPDATE k_unit SET raw_attack = :raw_attack WHERE id = :id;");
                     $statement->bindValue(":raw_attack", $unit->raw_attack, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["raw_defense"] != $unit->raw_defense){
                     $statement = $db->prepare("UPDATE k_unit SET raw_defense = :raw_defense WHERE id = :id;");
                     $statement->bindValue(":raw_defense", $unit->raw_defense, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["max_lvl_hp"] != $unit->max_lvl_hp){
                     $statement = $db->prepare("UPDATE k_unit SET max_lvl_hp = :max_lvl_hp WHERE id = :id;");
                     $statement->bindValue(":max_lvl_hp", $unit->max_lvl_hp, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["max_lvl_attack"] != $unit->max_lvl_attack){
                     $statement = $db->prepare("UPDATE k_unit SET max_lvl_attack = :max_lvl_attack WHERE id = :id;");
                     $statement->bindValue(":max_lvl_attack", $unit->max_lvl_attack, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["max_lvl_defense"] != $unit->max_lvl_defense){
                     $statement = $db->prepare("UPDATE k_unit SET max_lvl_defense = :max_lvl_defense WHERE id = :id;");
                     $statement->bindValue(":max_lvl_defense", $unit->max_lvl_defense, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 $com2us_id = get_com2us_id($unit->awakens_from);
@@ -659,8 +765,7 @@
                     $statement = $db->prepare("UPDATE k_unit SET awakens_from = :awakens_from WHERE id = :id;");
                     $statement->bindValue(":awakens_from", $com2us_id, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 $com2us_id = get_com2us_id($unit->awakens_to);
@@ -668,24 +773,21 @@
                     $statement = $db->prepare("UPDATE k_unit SET awakens_to = :awakens_to WHERE id = :id;");
                     $statement->bindValue(":awakens_to", $com2us_id, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["fusion_food"] != $unit->fusion_food){
                     $statement = $db->prepare("UPDATE k_unit SET fusion_food = :fusion_food WHERE id = :id;");
                     $statement->bindValue(":fusion_food", $unit->fusion_food, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
                 if ($r["homunculus"] != $unit->homunculus){
                     $statement = $db->prepare("UPDATE k_unit SET homunculus = :homunculus WHERE id = :id;");
                     $statement->bindValue(":homunculus", $unit->homunculus, SQLITE3_INTEGER);
                     $statement->bindValue(":id", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                 }
 
@@ -697,29 +799,21 @@
                 if ($r["c"] != sizeof($unit->skills)){
                     $statement = $db->prepare("DELETE FROM k_unit_skill WHERE unit = :unit;");
                     $statement->bindValue(":unit", $unit->com2us_id, SQLITE3_INTEGER);
-                    output(4, "Execute query: ");
-                    output(4, $statement->getSQL(true));
+                    output(4, "QUERY: " . $statement->getSQL(true));
                     $statement->execute();
                     foreach ($unit->skills as $skill){
                         $statement = $db->prepare("INSERT INTO k_unit_skill (unit, skill) VALUES (:unit, :skill);");
                         $statement->bindValue(":unit", $unit->com2us_id, SQLITE3_INTEGER);
                         $statement->bindValue(":skill", $skill, SQLITE3_INTEGER);
-                        output(4, "Execute query: ");
-                        output(4, $statement->getSQL(true));
+                        output(4, "QUERY: " . $statement->getSQL(true));
                         $statement->execute();
                     }
                 }
 
                 // Image download
                 $out = PATH . "public/img/unit/" . sprintf('%08d', $unit->com2us_id) . ".png";
-                if (!file_exists($out)){
-                    //echo("  Downloading image " . $unit->image_filename . " to " .  sprintf('%08d', $unit->com2us_id) . ".png");
-                    $url = 'https://swarfarm.com/static/herders/images/monsters/' . $unit->image_filename;   
-                    output(4, "Downloading file '$url' to: '$out'");
-                    if (!file_put_contents($out, file_get_contents($url))) { 
-                        output(1, "Error downloading file '$url' to: '$out'");
-                    } 
-                }
+                $url = 'https://swarfarm.com/static/herders/images/monsters/' . $unit->image_filename;   
+                download($url, $out);
             }
         }
         //echo "NEXTPAGE: " . $next;