89 lines
2.2 KiB
Python
89 lines
2.2 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
|