from fastapi import FastAPI from pydantic import BaseModel from missing_kids import MissingKidsFetcher from search import SearchFetcher from logger import get_logger logger = get_logger() app = FastAPI() @app.get("/get_missing_kids/") def get_missing_kids(pages: int = -1): try: logger.info("Get missing kids, #pages={}".format(pages)) res = {"list_urls": MissingKidsFetcher().get_missing_kids_urls(first_n_pages=pages)} except Exception as e: logger.warning("Exception: {}".format(str(e)), exc_info=True) res = {} return res class BodyVerifyMissingKid(BaseModel): url: str @app.post("/verify_missing_kid/") def get_missing_kids(data: BodyVerifyMissingKid): try: logger.info("Verify missing kid, URL={}".format(data.url)) res = MissingKidsFetcher().verify_missing_kid_url(data.url) except Exception as e: logger.warning("Exception: {}".format(str(e)), exc_info=True) res = {} return res class BodyFetchSearch(BaseModel): search: str @app.post("/fetch_search/") def fetch_search(data: BodyFetchSearch): try: # Initialize search_fetcher, results = SearchFetcher(), {} # Iterate for source in search_fetcher.get_available_sources(): logger.info("Fetch based search source={} search={}".format(source, data.search)) # Fetch results[source] = SearchFetcher().search(source, data.search) # Empty? if (len(results[source]) == 0): results.pop(source) except Exception as e: logger.warning("Exception: {}".format(str(e)), exc_info=True) results = {} return results