brewery-website/beer/extras.py

81 lines
2.7 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
def get_batches(api_user, api_key, batch=''):
auth_string = api_user + ':' + api_key
auth64 = base64.b64encode(auth_string.encode("utf-8"))
batch_array = []
if batch != '':
lastbatch = '&start_after=' + batch
else:
lastbatch = ''
query = '{batch_url}?limit={pull_limit}&complete=True&include=recipe,recipe.batchSize&status=Planning{last_batch}'.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 pull_recipes(api_user, api_key):
batch_list = get_batches(api_user, api_key);
for batch in batch_list:
batch_id = batch['_id']
brewDate = batch['brewDate']
recipe = batch['recipe']
batch_name = '{} {}'.format(batch['name'], batch['batchNo'])
name = recipe['name']
batchSize = recipe['batchSize'] * 0.264172
strike_water = recipe['data']['mashWaterAmount'] * 0.264172
sparge_water = recipe['data']['spargeWaterAmount'] * 0.264172
fermentables = recipe['data']['mashFermentables']
fermentable_text = ''
for num, ferm in enumerate(fermentables):
logger.critical(ferm)
# var this_ferm = recipe.data.mashFermentables[key];
# console.log(this_ferm);
# var malt_string = this_ferm.name + '@@@' + this_ferm.grainCategory + '@@@' + this_ferm.amount * 2.20462 + '@@@ @@@' + this_ferm.color;
# fermentable_text = fermentable_text + "%%%" + malt_string;
# }
# /*for (j=0;j<fermentables.length;j++){
# console.log(fermentables[j]);
# var malt_string = fermentables[j].name + '@@@' + fermentables[j].grainCategory + '@@@' + fermentables[j].amount * 2.20462 + '@@@ @@@' + fermentables[j].color;
# fermentable_text = fermentable_text + "%%%" + malt_string;
# }*/
# row_data.push([name, batch_name, fermentable_text, id, strike_water, sparge_water, batchSize, brewDate]);
# }
# sheet = SpreadsheetApp.getActive().getSheetByName('Recipes');
# clearrange = sheet.getRange("A2:H");
# clearrange.clear();
# range = sheet.getRange(1, 1, row_data.length, row_data[0].length);
# range.setValues(row_data);
# clearrange.sort(8);
# }