From b1332dede9eb5ffa56e1c49e91de20cffad3958a Mon Sep 17 00:00:00 2001 From: Chris Giacofei Date: Thu, 13 Jun 2024 16:15:35 -0400 Subject: [PATCH] Implement basic call to brewfather to pull data. --- beer/admin.py | 6 +--- beer/extras.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ beer/models.py | 10 +++---- beer/views.py | 32 ++++++++++++++++++-- 4 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 beer/extras.py diff --git a/beer/admin.py b/beer/admin.py index a98a270..488088e 100644 --- a/beer/admin.py +++ b/beer/admin.py @@ -3,7 +3,7 @@ from django.urls import reverse from django.utils.html import format_html from django.apps import apps -from beer.models import Batch, Recipe, BatchRecipe +from beer.models import Batch, BatchRecipe, BatchRecipe from yeast.models import Yeast from config.extras import BREWFATHER_APP_ROOT @@ -12,10 +12,6 @@ class SampleInline(admin.TabularInline): model = Yeast extra = 0 -@admin.register(Recipe) -class RecipeAdmin(admin.ModelAdmin): - list_display = ['name'] - @admin.register(BatchRecipe) class BatchRecipeAdmin(admin.ModelAdmin): list_display = ['name'] diff --git a/beer/extras.py b/beer/extras.py new file mode 100644 index 0000000..ab8f7d3 --- /dev/null +++ b/beer/extras.py @@ -0,0 +1,81 @@ +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