2025-06-06 22:18:53 +02:00
import requests
import os
import json
from pathvalidate import validate_filename
from config import *
headers = { }
if TANDOOR_API_TOKEN :
headers [ " Authorization " ] = f " Bearer { TANDOOR_API_TOKEN } "
def fetch_keyword_id ( keyword ) :
2026-04-08 23:13:31 +02:00
endpoint = " /api/keyword/ "
2025-06-06 22:18:53 +02:00
page = 1
while True :
params = { " query " : keyword , " page " : page }
response = requests . get (
f " { TANDOOR_URL } { endpoint } " , params = params , headers = headers
)
if response . status_code != 200 :
print ( f " Error: Received status code { response . status_code } " )
return None
2026-04-08 23:13:31 +02:00
try :
data = response . json ( )
except requests . exceptions . JSONDecodeError :
print ( f " Error: No JSON returned " )
return None
2025-06-06 22:18:53 +02:00
results = data . get ( " results " , [ ] )
for item in results :
if item . get ( " name " ) == keyword :
return item . get ( " id " )
if not data . get ( " next " ) :
break
page + = 1
raise RuntimeError ( f " Keyword ' { keyword } ' not found. " )
def fetch_recipes ( keyword_id ) :
recipes = [ ]
2026-04-08 23:13:31 +02:00
endpoint = " /api/recipe/ "
2025-06-06 22:18:53 +02:00
page = 1
while True :
params = { " keywords " : keyword_id , " page " : page }
response = requests . get (
f " { TANDOOR_URL } { endpoint } " , params = params , headers = headers
)
if response . status_code != 200 :
print ( f " Error: Received status code { response . status_code } " )
return None
2026-04-08 23:13:31 +02:00
try :
data = response . json ( )
except requests . exceptions . JSONDecodeError :
print ( f " Error: No JSON returned " )
return None
2025-06-06 22:18:53 +02:00
results = data . get ( " results " , [ ] )
for item in results :
recipe = fetch_recipe ( item . get ( " id " ) )
recipes . append ( recipe )
if not data . get ( " next " ) :
break
page + = 1
return recipes
2025-06-08 21:17:47 +02:00
2025-06-06 22:18:53 +02:00
def fetch_recipe ( recipe_id ) :
2026-04-08 23:13:31 +02:00
endpoint = " /api/recipe/ "
2025-06-06 22:18:53 +02:00
2026-04-08 23:13:31 +02:00
params = { }
response = requests . get (
2026-04-08 23:51:21 +02:00
f " { TANDOOR_URL } { endpoint } { recipe_id } / " , params = params , headers = headers
2026-04-08 23:13:31 +02:00
)
2025-06-06 22:18:53 +02:00
if response . status_code != 200 :
print ( f " Error: Received status code { response . status_code } " )
return None
2026-04-08 23:13:31 +02:00
try :
data = response . json ( )
except requests . exceptions . JSONDecodeError :
print ( f " Error: No JSON returned " )
return None
recipe = data
2025-06-06 22:18:53 +02:00
return recipe
2025-06-08 21:17:47 +02:00
2025-06-06 22:18:53 +02:00
def main ( ) :
keyword_id = fetch_keyword_id ( TANDOOR_KEYWORD )
recipes = fetch_recipes ( keyword_id )
for recipe in recipes :
json_fn = f " { recipe . get ( ' name ' , ' Untitled Recipe ' ) } .json "
validate_filename ( json_fn )
os . makedirs ( OUTDIR_JSON , exist_ok = True )
with open ( os . path . join ( OUTDIR_JSON , json_fn ) , " w " ) as f :
json . dump ( recipe , f , sort_keys = True , indent = 2 )
if __name__ == " __main__ " :
main ( )