164 lines
4.4 KiB
Python
164 lines
4.4 KiB
Python
import base64
|
|
import json
|
|
from urllib.request import Request, urlopen
|
|
|
|
import logging
|
|
logger = logging.getLogger('django')
|
|
|
|
RECIPE_URL = 'https://api.brewfather.app/v2/recipes'
|
|
BATCH_URL = 'https://api.brewfather.app/v2/batches'
|
|
PULL_LIMIT = 50
|
|
|
|
|
|
BREWFATHER_CONVERT_LOOKUP = { # local_name: brewfather_name
|
|
'all': {
|
|
'name': 'name',
|
|
'unit_cost': 'costPerAmount',
|
|
'supplier': 'supplier',
|
|
'notes': 'notes',
|
|
'user_notes': 'userNotes',
|
|
},
|
|
'fermentable': {
|
|
'grain_category': 'grainCategory',
|
|
'fermentable_type': 'type',
|
|
'diastatic_power': 'diastaticPower',
|
|
'potential': 'potential',
|
|
'protein': 'protein',
|
|
'attenuation': 'attenuation',
|
|
'lovibond': 'lovibond',
|
|
'max_in_batch': 'maxInBatch',
|
|
'moisture': 'moisture',
|
|
'non_fermentable': 'notFermentable',
|
|
'ibu_per_unit': 'ibuPerAmount',
|
|
},
|
|
'hop': {
|
|
'ibu': 'ibu',
|
|
'use': 'use',
|
|
'hop_type': 'type',
|
|
'alpha': 'alpha',
|
|
|
|
},
|
|
'misc': {
|
|
'use': 'use',
|
|
'misc_type': 'type',
|
|
'water_adjustment': 'waterAdjustment',
|
|
|
|
}
|
|
}
|
|
|
|
|
|
def bf_from_local(category, name):
|
|
return BREWFATHER_CONVERT_LOOKUP[category][name]
|
|
|
|
|
|
def local_from_bf(category, name):
|
|
local_keys = list(BREWFATHER_CONVERT_LOOKUP[category].keys())
|
|
bf_keys = list(BREWFATHER_CONVERT_LOOKUP[category].values())
|
|
return local_keys[bf_keys.index(name)]
|
|
|
|
|
|
def get_batches(api_user, api_key, batch=''):
|
|
auth_string = api_user + ':' + api_key
|
|
|
|
auth64 = base64.b64encode(auth_string.encode("utf-8"))
|
|
|
|
if batch != '':
|
|
lastbatch = '&start_after=' + batch
|
|
else:
|
|
lastbatch = ''
|
|
|
|
qry_str = ('{batch_url}?limit={pull_limit}'
|
|
'&complete=True&include=recipe,recipe.batchSize'
|
|
'&status=Planning{last_batch}')
|
|
query = qry_str.format(
|
|
batch_url=BATCH_URL,
|
|
pull_limit=PULL_LIMIT,
|
|
last_batch=lastbatch
|
|
)
|
|
|
|
req = Request(query)
|
|
req.add_header('authorization', 'Basic ' + auth64.decode())
|
|
content = urlopen(req)
|
|
|
|
data = json.load(content)
|
|
|
|
if len(data) == PULL_LIMIT:
|
|
last_id = data[-1]['_id']
|
|
data = data + get_batches(batch=last_id)
|
|
|
|
return data
|
|
|
|
|
|
def sg_plato(sg):
|
|
"""Convert specific gravtiy to °P."""
|
|
sg = float(sg)
|
|
return (-1*616.868) + (1111.14*sg) - (630.272*sg**2) + (135.997*sg**3)
|
|
|
|
|
|
def plato_sg(plato):
|
|
"""Convert °P to specific gravtiy."""
|
|
return 1 + (plato / (258.6 - (plato/258.2) * 227.1))
|
|
|
|
|
|
def kg_extract(v, sg):
|
|
"""Calculate kg of extract based on volume and SG."""
|
|
return float(v) * float(sg) * sg_plato(sg)/100
|
|
|
|
|
|
def convert(value, start_unit, end_unit):
|
|
"""Convert units."""
|
|
|
|
family_dict = unit_family(start_unit)
|
|
|
|
if family_dict:
|
|
start = [val for key, val in family_dict.items()
|
|
if start_unit in key.split(',')][0]
|
|
|
|
end = [val for key, val in family_dict.items()
|
|
if end_unit in key.split(',')][0]
|
|
|
|
return float(value) * end / start
|
|
|
|
|
|
def unit_family(unit_name):
|
|
"""Find unit family base on unit name."""
|
|
|
|
unit_lookup = [
|
|
{'name': 'length',
|
|
'units': {
|
|
'm,meter,meters': 1.0,
|
|
'cm,centimeter,centimeters': 100,
|
|
'mm,millimeter,millimeters': 1000,
|
|
'in,inch,inches': 39.3701,
|
|
'yd,yard,yards': 1.09361,
|
|
'ft,foot,feet': 3.28084,
|
|
'mi,mile,miles': 0.000621371,
|
|
'km,killometer,killometers': .001,
|
|
}},
|
|
{'name': 'mass',
|
|
'units': {
|
|
'kg,kilogram,kilograms': 1.0,
|
|
'g,gram,grams': 1000,
|
|
'lb,pound,pounds': 2.20462,
|
|
'oz,ounce,ounces': 35.274,
|
|
'st,stone': 0.157473,
|
|
}},
|
|
{'name': 'volume',
|
|
'units': {
|
|
'l,liter,liters': 1.0,
|
|
'ml,milliliter,milliliters': 1000,
|
|
'floz,fluid ounce,fluid ounces': 33.814,
|
|
'cup,cups': 4.22675,
|
|
'qt,quart,quarts': 1.05669,
|
|
'gal,gallon,gallons': 0.264172,
|
|
'ft^3,cubic foot,cubic feet': 0.0353147,
|
|
'pt,pint,pints': 4.22675/2,
|
|
'tsp,teaspoon,teaspoons': 202.884,
|
|
'tbsp,tablespoon,tablespoons': 202.884/3,
|
|
}},
|
|
]
|
|
|
|
for family in unit_lookup:
|
|
if [key for key in family['units'] if unit_name in key.split(',')]:
|
|
return family['units']
|