Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RubenKelevra/52840b8226239c13087fb65a9d53acee to your computer and use it in GitHub Desktop.
Save RubenKelevra/52840b8226239c13087fb65a9d53acee to your computer and use it in GitHub Desktop.
Add messurements to grocy
<?php
$apiaccess =[
"url" => "https://grocy.yourdomain.tld/api",
"key" => ""
];
$quantity_units = [
"Cup US" => [
"name" => "Cup US",
"description" => "",
"name_plural" => "Cups US"
],
"Gallon US" => [
"name" => "Gallon US",
"description" => "",
"name_plural" => "Gallons US"
],
"Cup UK" => [
"name" => "Cup UK",
"description" => "",
"name_plural" => "Cups UK"
],
"Gallon UK" => [
"name" => "Gallon UK",
"description" => "",
"name_plural" => "Gallons UK"
],
"Gram" => [
"name" => "Gram",
"description" => "",
"name_plural" => "Grams"
],
"Kilogram" => [
"name" => "Kilogram",
"description" => "",
"name_plural" => "Kilograms"
],
"Litre" => [
"name" => "Litre",
"description" => "",
"name_plural" => "Litres"
],
"Millilitre" => [
"name" => "Millilitre",
"description" => "",
"name_plural" => "Millilitres"
],
"Ounce liquid US" => [
"name" => "Ounce liquid US",
"description" => "",
"name_plural" => "Ounces liquid"
],
"Pint US" => [
"name" => "Pint US",
"description" => "",
"name_plural" => "Pints US"
],
"Quart US" => [
"name" => "Quart US",
"description" => "",
"name_plural" => "Quarts US"
],
"Tablespoon US" => [
"name" => "Tablespoon US",
"description" => "",
"name_plural" => "Tablespoons US"
],
"Teaspoon US" => [
"name" => "Teaspoon US",
"description" => "",
"name_plural" => "Teaspoons US"
],
"Ounce" => [
"name" => "Ounce",
"description" => "",
"name_plural" => "Ounces"
],
"Ounce liquid UK" => [
"name" => "Ounce liquid UK",
"description" => "",
"name_plural" => "Ounces liquid"
],
"Pint UK" => [
"name" => "Pint UK",
"description" => "",
"name_plural" => "Pints UK"
],
"Pound" => [
"name" => "Pound",
"description" => "",
"name_plural" => "Pounds"
],
"Quart UK" => [
"name" => "Quart UK",
"description" => "",
"name_plural" => "Quarts UK"
],
"Tablespoon UK" => [
"name" => "Tablespoon UK",
"description" => "",
"name_plural" => "Tablespoons UK"
],
"Teaspoon UK" => [
"name" => "Teaspoon UK",
"description" => "",
"name_plural" => "Teaspoons UK"
],
];
$quantity_unit_conversions = [
"Cup US" => [
"Gallon US" => [
"factor"=> ".062",
],
"Litre" => [
"factor"=> "0.237",
],
"Millilitre" => [
"factor"=> "236.588",
],
"Ounce liquid US" => [
"factor"=> "8",
],
"Pint US" => [
"factor"=> ".5",
],
"Quart US" => [
"factor"=> ".25",
],
"Tablespoon US" => [
"factor"=> "16",
],
"Teaspoon US" => [
"factor"=> "48",
],
],
"Gallon US" => [
"Cup US" => [
"factor"=> "16",
],
"Litre" => [
"factor"=> "3.785",
],
"Millilitre" => [
"factor"=> "3785.412",
],
"Ounce liquid US" => [
"factor"=> "128",
],
"Pint US" => [
"factor"=> "8",
],
"Quart US" => [
"factor"=> "4",
],
"Tablespoon US" => [
"factor"=> "256",
],
"Teaspoon US" => [
"factor"=> "768",
],
],
"Cup UK" => [
"Gallon UK" => [
"factor"=> ".055",
],
"Litre" => [
"factor"=> "0.250",
],
"Millilitre" => [
"factor"=> "250",
],
"Ounce liquid UK" => [
"factor"=> "8.799",
],
"Pint UK" => [
"factor"=> ".440",
],
"Quart UK" => [
"factor"=> ".220",
],
"Tablespoon UK" => [
"factor"=> "17.598",
],
"Teaspoon UK" => [
"factor"=> "70.390",
],
],
"Gallon UK" => [
"Cup UK" => [
"factor"=> "18.184",
],
"Litre" => [
"factor"=> "4.546",
],
"Millilitre" => [
"factor"=> "4546.090",
],
"Ounce liquid UK" => [
"factor"=> "160",
],
"Pint UK" => [
"factor"=> "8",
],
"Quart UK" => [
"factor"=> "4",
],
"Tablespoon UK" => [
"factor"=> "320",
],
"Teaspoon UK" => [
"factor"=> "960",
],
],
"Gram" => [
"Kilogram" => [
"factor"=> "0.0010",
],
"Ounce" => [
"factor"=> "0.035",
],
"Pound" => [
"factor"=> "0.002",
],
],
"Kilogram" => [
"Gram" => [
"factor"=> "1000.0000",
],
"Ounce" => [
"factor"=> "35.274",
],
"Pound" => [
"factor"=> "2.205",
],
],
"Litre" => [
"Millilitre" => [
"factor"=> "0.0010",
],
"Cup US" => [
"factor"=> "4.227",
],
"Gallon US" => [
"factor"=> "0.260",
],
"Ounce liquid US" => [
"factor"=> "33.81",
],
"Pint US" => [
"factor"=> "2.113",
],
"Quart US" => [
"factor"=> "1.0570",
],
"Tablespoon US" => [
"factor"=> "67.628",
],
"Teaspoon US" => [
"factor"=> "202.884",
],
"Cup UK" => [
"factor"=> "4",
],
"Gallon UK" => [
"factor"=> "0.220",
],
"Ounce liquid UK" => [
"factor"=> "35.195",
],
"Pint UK" => [
"factor"=> "1.760",
],
"Quart UK" => [
"factor"=> "0.880",
],
"Tablespoon UK" => [
"factor"=> "70.390",
],
"Teaspoon UK" => [
"factor"=> "281.560",
],
],
"Millilitre" => [
"Litre" => [
"factor"=> "0.1000",
],
"Cup US" => [
"factor"=> "0.004",
],
"Gallon US" => [
"factor"=> "0.000264",
],
"Ounce liquid US" => [
"factor"=> "0.033",
],
"Pint US" => [
"factor"=> "0.002",
],
"Quart US" => [
"factor"=> "0.001",
],
"Tablespoon US" => [
"factor"=> "0.067",
],
"Teaspoon US" => [
"factor"=> "0.203",
],
"Cup UK" => [
"factor"=> "0.004",
],
"Gallon UK" => [
"factor"=> "0.000220",
],
"Ounce liquid UK" => [
"factor"=> "0.035",
],
"Pint UK" => [
"factor"=> "0.002",
],
"Quart UK" => [
"factor"=> "0.001",
],
"Tablespoon UK" => [
"factor"=> "0.070",
],
"Teaspoon UK" => [
"factor"=> "0.282",
],
],
"Ounce" => [
"Gram" => [
"factor"=> "28.35",
],
"Kilogram" => [
"factor"=> "0.028",
],
"Pound" => [
"factor"=> "0.063",
],
],
"Ounce liquid US" => [
"Litre" => [
"factor"=> "0.03",
],
"Millilitre" => [
"factor"=> "29.574",
],
"Cup US" => [
"factor"=> "0.1250",
],
"Gallon US" => [
"factor"=> "0.0078",
],
"Pint US" => [
"factor"=> "0.0625",
],
"Quart US" => [
"factor"=> "0.0313",
],
"Tablespoon US" => [
"factor"=> "2.0000",
],
"Teaspoon US" => [
"factor"=> "6.0000",
],
],
"Pint US" => [
"Litre" => [
"factor"=> "0.473",
],
"Millilitre" => [
"factor"=> "473.177",
],
"Cup US" => [
"factor"=> "2",
],
"Gallon US" => [
"factor"=> "0.1250",
],
"Ounce liquid US" => [
"factor"=> "16.0000",
],
"Quart US" => [
"factor"=> "0.5000",
],
"Tablespoon US" => [
"factor"=> "32.0000",
],
"Teaspoon US" => [
"factor"=> "96.0000",
],
],
"Pound" => [
"Gram" => [
"factor"=> "0.0022",
],
"Kilogram" => [
"factor"=> "0.0000",
],
"Ounce" => [
"factor"=> "16.0000",
],
],
"Quart US" => [
"Litre" => [
"factor"=> "0.9461",
],
"Millilitre" => [
"factor"=> "946.353",
],
"Cup US" => [
"factor"=> "4.0000",
],
"Gallon US" => [
"factor"=> "0.2500",
],
"Ounce liquid US" => [
"factor"=> "32.0000",
],
"Pint US" => [
"factor"=> "2.0000",
],
"Tablespoon US" => [
"factor"=> "64.0000",
],
"Teaspoon US" => [
"factor"=> "192.0000",
],
],
"Tablespoon US" => [
"Litre" => [
"factor"=> "0.015",
],
"Millilitre" => [
"factor"=> "14.8",
],
"Cup US" => [
"factor"=> "0.062",
],
"Gallon US" => [
"factor"=> "0.0039",
],
"Ounce liquid US" => [
"factor"=> "0.5000",
],
"Pint US" => [
"factor"=> "0.0313",
],
"Quart US" => [
"factor"=> "0.0156",
],
"Teaspoon US" => [
"factor"=> "3.0000",
],
],
"Teaspoon US" => [
"Litre" => [
"factor"=> "0.005",
],
"Millilitre" => [
"factor"=> "4.93",
],
"Cup US" => [
"factor"=> "0.0208",
],
"Gallon US" => [
"factor"=> "0.0013",
],
"Ounce liquid US" => [
"factor"=> "0.1667",
],
"Pint US" => [
"factor"=> "0.0104",
],
"Quart US" => [
"factor"=> "0.0052",
],
"Tablespoon US" => [
"factor"=> "0.3333",
],
],
"Ounce liquid UK" => [
"Litre" => [
"factor"=> "0.0284",
],
"Millilitre" => [
"factor"=> "28.413",
],
"Cup UK" => [
"factor"=> "0.114",
],
"Gallon UK" => [
"factor"=> "0.006",
],
"Pint UK" => [
"factor"=> "0.05",
],
"Quart UK" => [
"factor"=> "0.025",
],
"Tablespoon UK" => [
"factor"=> "2.0000",
],
"Teaspoon UK" => [
"factor"=> "8.0000",
],
],
"Pint UK" => [
"Litre" => [
"factor"=> "0.568",
],
"Millilitre" => [
"factor"=> "568.261",
],
"Cup UK" => [
"factor"=> "2.273",
],
"Gallon UK" => [
"factor"=> "0.1250",
],
"Ounce liquid UK" => [
"factor"=> "20",
],
"Quart UK" => [
"factor"=> "0.5000",
],
"Tablespoon UK" => [
"factor"=> "40.0000",
],
"Teaspoon UK" => [
"factor"=> "120.0",
],
],
"Quart UK" => [
"Litre" => [
"factor"=> "1.137",
],
"Millilitre" => [
"factor"=> "1136.523",
],
"Cup UK" => [
"factor"=> "4.546",
],
"Gallon UK" => [
"factor"=> "0.2500",
],
"Ounce liquid UK" => [
"factor"=> "64.0000",
],
"Pint UK" => [
"factor"=> "2.0000",
],
"Tablespoon UK" => [
"factor"=> "128.0000",
],
"Teaspoon UK" => [
"factor"=> "240.0000",
],
],
"Tablespoon UK" => [
"Litre" => [
"factor"=> "0.014",
],
"Millilitre" => [
"factor"=> "14.207",
],
"Cup UK" => [
"factor"=> "0.057",
],
"Gallon UK" => [
"factor"=> "0.003",
],
"Ounce liquid UK" => [
"factor"=> "0.5000",
],
"Pint UK" => [
"factor"=> "0.025",
],
"Quart UK" => [
"factor"=> "0.013",
],
"Teaspoon UK" => [
"factor"=> "3.0000",
],
],
"Teaspoon UK" => [
"Litre" => [
"factor"=> "0.004",
],
"Millilitre" => [
"factor"=> "3.552",
],
"Cup UK" => [
"factor"=> "0.014",
],
"Gallon UK" => [
"factor"=> "0.001",
],
"Ounce liquid UK" => [
"factor"=> "0.125",
],
"Pint UK" => [
"factor"=> "0.008",
],
"Quart UK" => [
"factor"=> "0.004",
],
"Tablespoon UK" => [
"factor"=> "0.3333",
],
],
];
function curl_post($stub,$apiaccess,$data)
{
$options = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $apiaccess["url"].$stub,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => ["GROCY-API-KEY:".$apiaccess["key"],"Accept: application/json","Content-Type: application/json"],
);
$ch = curl_init();
curl_setopt_array($ch, ($options));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return json_decode($result,true);
}
function curl_get($stub,$apiaccess)
{
$options = array(
CURLOPT_URL => $apiaccess["url"].$stub,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 4,
CURLOPT_HTTPHEADER => ["GROCY-API-KEY:".$apiaccess["key"],"Accept: application/json"],
);
$ch = curl_init();
curl_setopt_array($ch, ($options));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return json_decode($result,true);
}
$currentUnits = curl_get("/objects/quantity_units",$apiaccess);
foreach ($currentUnits as $unit) {
if (isset($quantity_units[$unit["name"]])) {
$quantity_units[$unit["name"]]["id"] = $unit["id"];
}
}
foreach ($quantity_units as $unit => $data) {
if (!isset($data["id"])){
$result = curl_post("/objects/quantity_units",$apiaccess,json_encode($data));
if (isset($result["error_message"])) {
die($result["error_message"]);
}
$quantity_units[$unit]["id"] = $result["created_object_id"];
}
}
$currentConversions = curl_get("/objects/quantity_unit_conversions",$apiaccess);
foreach ($quantity_unit_conversions as $base => $baseData) {
$fromId = $quantity_units[$base]["id"];
$existingConversions = array_filter($currentConversions, function ($var) use ($fromId) {
return ($var["from_qu_id"] == $fromId);
});
$existingConversions = array_column($existingConversions,"to_qu_id");
foreach ($baseData as $to => $toData) {
if(!in_array($quantity_units[$to]["id"],$existingConversions)){
$result = curl_post("/objects/quantity_unit_conversions",$apiaccess,json_encode([
"from_qu_id" => $fromId,
"to_qu_id" => $quantity_units[$to]["id"],
"factor" => $toData["factor"],
]));
if (isset($result["error_message"])) {
die($result["error_message"]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment