NEW! jamesgoca Weather Station (Open 8am to 8pm)

Skip to main content

Airtable Coffee Scraper

Code Snippet

Year Active:

View this project

A script to scrape data from Airtable.

import jinja2
import requests

import shutil

headers = {
	"Authorization": "Bearer AIRTABLE_API_KEY"
}

endpoint = "AIRTABLE_API_URL"

get_coffees = requests.get(endpoint, headers=headers)

records = get_coffees.json()["records"]

# Download images from Airtable

# for r in records:
# 	if "Image" in r["fields"]:
# 		image = r["fields"]["Image"][0]
# 		get_image = requests.get(image["url"], stream=True)
# 		with open("files/{}".format(image["filename"]), "wb") as file:
# 			shutil.copyfileobj(get_image.raw, file)
# 		del get_image

with open("template.txt") as file:
	template = jinja2.Template(file.read())

# Define codenames for coffee shops

new_shops = []
new_shops_mapped = ['Alpha', 'Beta', 'Gamma', 'Delta', 'Episolon', 'Zeta', 'Eta', 'Theta']

for i in range(0, len(records)):
	shop_index = new_shops.index(records[i]["fields"]["Coffee Shop"])
	shop = new_shops_mapped[shop_index]
	if "Image" in records[i]["fields"]:
		if "HEIC" in records[i]["fields"]["Image"][0]["filename"]:
			image_name = records[i]["fields"]["Image"][0]["filename"].replace("HEIC", "jpg")
		else:
			image_name = records[i]["fields"]["Image"][0]["filename"]
	else:
		image_name = None

	final = template.render(values=records[i]["fields"], shop=shop, image_name=image_name)

	with open("coffees/{}.md".format(i+1), "w+") as file:
		file.write(final)

Go Back to the Top