Cocktail Database using CocktailDB, Templater and Python

Hi,

I want to show forth how I made and manage a cocktail database using CocktailDB, Templater and Python.


The way I set it up does not allow for use of it without python, it could have been used using QuickAdd and JS, but I did not know how to manage that.

Shout out to

So how do you achieve this?

First,

You need Python and Templater plugin. In python you need to have the following packages installed.

  • requests
  • json
  • pyperclip
  • sys

You start with this simple python code and the reason why I did this in Python, for the request function.

# imports
import requests
import json
import pyperclip
import sys

# Argument tree
if sys.argv[1].startswith('1'):
    r = requests.get("https://www.thecocktaildb.com/api/json/v1/1/random.php")
else:
    r = requests.get("https://www.thecocktaildb.com/api/json/v1/1/search.php?s=" + sys.argv[1])


r_json = r.json()["drinks"]

# Some of the other key-value pairs from the TheCocktailDB query I don't want.
# Just comment, uncomment or and more if you want.
remove = ["strDrinkAlternate", 
          'strTags', 
          'strVideo', 
        #   'strCategory', 
          'strIBA', 
          'strInstructionsES', 
          'strInstructionsDE',
          'strInstructionsFR',
          'strInstructionsIT',
          'strInstructionsZH-HANT',
          'strInstructionsZH-HANS',
          'strImageSource',
          'strCreativeCommonsConfirmed',
          'dateModified',
          'strImageAttribution']

for drink in r_json:
    i = 1

    # Removes the key-value pairs
    drink["strDrink"] = drink["strDrink"].replace("'", "")
    drink["strInstructions"] = drink["strInstructions"].replace("'", "")

    for rem in remove:
        del drink[rem]

    # Removes the extra ingredient key-value pairs
    while i <= 15:
        if drink[f"strIngredient{i}"] == None:
            del drink[f"strIngredient{i}"]
            del drink[f"strMeasure{i}"]

        i += 1
        
# Converts to json text
json_text = json.dumps(r_json)

# Puts the json into your clipboard
pyperclip.copy(json_text)

Since the infomation has been put into the clipboard, we can continue onward into Templater, but before you need to setup some settings in Templater to make this work.

You need to add a User Function in Templater, in the settings tab.


You will need to replace the "40 - Obsidian/Scripts/cocktail.py" with the path to the code, and replace py to your devices, python startup. Go in the terminal and type py, py3 or others and check if python pops up.

Second,

We need to set up the Templater script.

The code is real messy

<%*
const drinks = await tp.system.suggester(["Search:", "Random"], [0,1], false, "What do you choose");
if (drinks === null) {
  // User cancelled the suggestion dialog without making a selection. Handle appropriately.
  return;
} else if (drinks === 1) {
  await tp.user.cocktail({rand_drink: drinks});
} else {
  const search = await tp.system.prompt("What cocktail you want to search for?", null, false, false);
  await tp.user.cocktail({rand_drink: search});
}
let text = await tp.system.clipboard();

const obj = JSON.parse(text); 
var arr_name = [];
let ingredients = "";
let ing = "";
let mes = "";
for (let i = 0; i < obj.length; i++) {
  arr_name.push(obj[i].strDrink);
}
const selectedKey = await tp.system.suggester(arr_name, obj, false, "Select a cocktail: ");
if (selectedKey === null) {
  // User cancelled the suggestion dialog without making a selection. Handle appropriately.
  return;
} else {
  if (await tp.file.exists("/60 - Food & Drinks/Cocktails/" + selectedKey.strDrink) === true) {
    return;
  } else {
    await tp.file.move("/60 - Food & Drinks/Cocktails/" + selectedKey.strDrink);
    for (let i = 1; i <= 15; i++) { 
      // assuming a maximum of 15 ingredients/measures as seen in similar APIs 
      const ingredient = selectedKey[`strIngredient${i}`]; 
      const measure = selectedKey[`strMeasure${i}`]; 
      if (ingredient) { 
        ingredients += `- [x] ${ingredient}, ${measure || ''}\n`; 
        ing += `- "[[${ingredient}]]"\n`
        mes += `- "${measure}"\n`
      } 
    }
  }
}

-%>---
tags:  drink/cocktail 
image:  <% selectedKey.strDrinkThumb %> 
ingredients: 
<% await ing %>
amount: 
<% await mes %>
source: TheCocktailDB
howTo: <% selectedKey.strInstructions %>

---

# `=this.file.name`
![](<% selectedKey.strDrinkThumb %>)

Rating:: 
Method::


 > [!tip] Tips
 > Placeholder

---
## Ingredients
- Glass
	-  <% selectedKey.strGlass %> 

#ingredients 
<% await ingredients %>

---

## Steps:
<% selectedKey.strInstructions %>

This should be easy if you have used Templater before. Add this into your template folder and use the Templater: Create new note from template function to make a new note.

Third,

Here is the note and code for the dataview for the beginning images, note that you need to have the correct css, to make it look the same.

---
tags:
  - drink/homepage
cssclasses:
  - cards
banner: "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fdistiller-blog-prod%2Fwp-content%2Fuploads%2F2020%2F04%2F08174837%2Fstir.jpg&f=1&nofb=1&ipt=72875d368a77af2f13b20f335f57a8a6253a850c14c438931e03da7c7d97f672&ipo=images"
banner_y: 0.45
---
# Cocktails

```dataviewjs
let pages = dv.pages('#drink/cocktail AND -"40 - Obsidian"');
dv.table(["Image", "Title", "Rating", "Method", "Ingredients", "HowTo"], pages
		.sort(p => p.file.name, 'asc')
		.map(p => 
			["![](" + p.image + ")", 
			p.file.link,
			"Rated as ⭐ " + p.Rating,
			p.Method,
			p.ingredients.map((item, index) => `${item}, ${p.amount[index]}`), 
			p.howTo])
		
)
``` 

Final words

This is a very messy tutorial and setup, but I just wanted to post it so people could use it or follow its example. If you have any good ideas or tips to how I could either improve upon this concept or just about the post in general, leave a comment.

Otherwise, good night.
art_is_0

1 Like