api.py 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. from multiprocessing import Queue
  2. from pathlib import Path
  3. from ninja import NinjaAPI
  4. from app import settings
  5. import mwmbl.crawler.app as crawler
  6. from mwmbl.indexer.batch_cache import BatchCache
  7. from mwmbl.indexer.paths import INDEX_NAME, BATCH_DIR_NAME
  8. from mwmbl.tinysearchengine import search
  9. from mwmbl.tinysearchengine.completer import Completer
  10. from mwmbl.tinysearchengine.indexer import TinyIndex, Document
  11. from mwmbl.tinysearchengine.rank import HeuristicRanker
  12. api = NinjaAPI(version="1.0.0")
  13. index_path = Path(settings.DATA_PATH) / INDEX_NAME
  14. tiny_index = TinyIndex(item_factory=Document, index_path=index_path)
  15. tiny_index.__enter__()
  16. completer = Completer()
  17. ranker = HeuristicRanker(tiny_index, completer)
  18. search_router = search.create_router(ranker)
  19. api.add_router("/search/", search_router)
  20. batch_cache = BatchCache(Path(settings.DATA_PATH) / BATCH_DIR_NAME)
  21. queued_batches = Queue()
  22. crawler_router = crawler.create_router(batch_cache=batch_cache, queued_batches=queued_batches)
  23. api.add_router("/crawler/", crawler_router)