The Cocktail Database

TheCocktailDB has a very impressive cocktail selection you can use for fun or commercial purposes. They offer abundance of information regarding different types of cocktails and you can filter cocktails based on, ingredient, name, type, glass type, alcoholic vs non-alcoholic, etc.

Their cocktail photos are also a delight to the eye. With the help of webbrowser library we can then open these links right from the python terminal.

Here is a simply code that uses requests, json and webbrowser libraries:

import requests
import json
import webbrowser
def cocktails():
    f = r"https://www.thecocktaildb.com/api/json/v1/1/search.php?s=margarita"
    data = requests.get(f)
    tt = json.loads(data.text)
    #webbrowser.open(tt["url"])
    
    for i in (tt["drinks"]):
        print(i, "\n")
    print(len(tt["drinks"]))    
cocktails()

Source: The Cocktail DB

Code below will print a bunch of other information regarding cocktails such as: Instructions and Ingredients and it will also open the cocktail image in a browser.

import requests
import json
import webbrowser
def cocktails():
    f = r"https://www.thecocktaildb.com/api/json/v1/1/search.php?s=margarita"
    data = requests.get(f)
    tt = json.loads(data.text)
    
    
    for i in (tt["drinks"]):
        print(i["strDrink"], "\n")
        print(i["strInstructions"], "\n")
        print(i["strInstructionsDE"], "\n")
        
        print(i["strIngredient1"])
        print(i["strIngredient2"])
        print(i["strIngredient3"])
        print(i["strIngredient4"])
        url = i["strDrinkThumb"]
        webbrowser.open(url)
cocktails()

Source: The Cocktail DB

Code below will open all the cocktail images in your browser. Be careful as there might be too many images to open at once.

import requests
import json
import webbrowser
def cocktails():
    f = r"https://www.thecocktaildb.com/api/json/v1/1/filter.php?i=Gin"
    data = requests.get(f)
    tt = json.loads(data.text)
    print(tt)
    
    for i in (tt["drinks"]):
        try:
            url = i["strDrinkThumb"]
            webbrowser.open(url)
            print(i, "\n")
            print(i["strDrink"], "\n")
        except Exception:
            pass
cocktails()

TheCocktailDB is generous to allow access to their impressive cocktail database for educational and small scale personal use (via experimental API key: 1). For anything futher, you can support them through their Patreon Page here and get a legit API key.

For instance, if you’d like to create an app based on their Cocktail API, they got different options depending on the scale of your usage intention of their API.

Source of API: TheCocktailDB

If you need a refresher on user defined functions lesson in Python here is a link to our Defining Functions Lesson.

Recommended Posts