search.forkos.org/updateIndex/default.py

58 lines
2.3 KiB
Python
Raw Normal View History

2024-07-25 22:52:31 +00:00
import meilisearch
import argparse
import json
import time
import os
def meiliTaskStatus(task_uid: int, verbose: bool):
taskInfo = client.get_task(task_uid)
while taskInfo.status != "succeeded" and taskInfo.status != "canceled" and taskInfo.status != "failed":
# no need to hammer the api
time.sleep(3)
taskInfo = client.get_task(task_uid)
if verbose:
print(taskInfo)
else:
if taskInfo.status == "succeeded":
print(f"job {taskInfo.type} for {taskInfo.index_uid} ({taskInfo.uid}) succeeded")
elif taskInfo.status == "canceled":
print(f"job {taskInfo.type} for {taskInfo.index_uid} ({taskInfo.uid}) was canceled")
elif taskInfo.status == "failed":
print(f"job {taskInfo.type} for {taskInfo.index_uid} ({taskInfo.uid}) failed")
else:
raise ValueError(f"unexpected job task type for: {taskInfo}")
# todo: error and figure out a sensible default via envrc and maybe a local instance with something like devenv
meiliInstance = os.getenv("MEILI_INSTANCE")
if meiliInstance is None:
meiliInstance = "https://meilisearch.aq0.de/"
parser = argparse.ArgumentParser(
prog='updateIndex',
description='Updates meilisearch indexs, inteded to be be used for search.forkos.org'
)
parser.add_argument('documentsJsonFile')
parser.add_argument('-k', '--kind', choices=['packages', 'options'], default='packages')
parser.add_argument('-r', '--release', choices=['rolling', 'stable'], default='rolling')
parser.add_argument('-v', '--verbose', action='store_true')
args = parser.parse_args()
client = meilisearch.Client(meiliInstance)
indexName = f"{args.kind}-{args.release}"
with open(args.documentsJsonFile) as json_file:
documents = json.load(json_file)
if args.kind == "packages":
documentResponse = client.index(indexName).add_documents(documents)
indexResponse = client.index(indexName).update_filterable_attributes(['meta'])
meiliTaskStatus(documentResponse.task_uid, args.verbose)
meiliTaskStatus(indexResponse.task_uid, args.verbose)
elif args.kind == "options":
documentResponse = client.index(indexName).add_documents(documents)
meiliTaskStatus(documentResponse.task_uid, args.verbose)
else:
raise ValueError("invalid value for type kind")