Skip to content

Commit

Permalink
added pagination and removed empty inits
Browse files Browse the repository at this point in the history
  • Loading branch information
LLkaia committed Dec 20, 2023
1 parent 4daeeb1 commit 3d32a5e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
Empty file removed server/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions server/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from fastapi import FastAPI
from fastapi_pagination import add_pagination

from server.routes.search_result import router as SearchResultRouter


app = FastAPI()
add_pagination(app)
app.include_router(SearchResultRouter, tags=["Search"], prefix="/news/search")


Expand Down
7 changes: 4 additions & 3 deletions server/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def search_results_helper(search_result):
async def add_search_results(results: list[dict]):
"""Add articles to database
Check if each article does not exist in database. If it exists,
Check if each article does not exist in database. If it does,
add search words to article's 'tags' field. Else, article will
be added to a database.
:param results: List of new articles
Expand Down Expand Up @@ -61,18 +61,19 @@ async def retrieve_search_results_by_tags(tags: list[str]):
"""Find articles by tags
Take search words and check if database contain articles,
which have more than half of words in 'tags' fields matches
which have more than :percentage: of words in 'tags' fields matches
with words in search query. If database have them, return
this articles.
:param tags: List of search words
:return: List of articles
"""
percentage = 0.75
matched_result = []
results = search_results_collection.find()
search_tags = set(tags)
async for result in results:
common = search_tags.intersection(result["tags"])
if len(common) > len(search_tags) / 2:
if len(common) > len(search_tags) * percentage:
matched_result.append(search_results_helper(result))
return matched_result

Expand Down
Empty file removed server/models/__init__.py
Empty file.
Empty file removed server/routes/__init__.py
Empty file.
32 changes: 23 additions & 9 deletions server/routes/search_result.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fastapi import APIRouter, status, HTTPException
from fastapi import APIRouter, status, HTTPException, Query
from fastapi_pagination import Page, paginate

from server.scraper import scrap_from_search, scrap_content
from server.models.search_result import ArticleModel, ExtendArticleModel
Expand All @@ -12,25 +13,38 @@


router = APIRouter()
Page = Page.with_custom_options(
size=Query(5, ge=1, le=10),
)


@router.get("/", status_code=status.HTTP_200_OK, response_model=list[ArticleModel])
async def get_search_results(find: str | None = None) -> list[ArticleModel]:
@router.get("/", status_code=status.HTTP_200_OK, response_model=Page[ArticleModel])
async def get_search_results(find: str | None = None) -> Page[ArticleModel]:
"""Find articles by search query
Get list of articles which match with search query from database.
If count of articles is less than 20, scrap new articles and add
them to a database. If 'find' param is empty, return 20 newest
If count of articles is less than 10, scrap new articles and add
them to a database. If 'find' param is empty, return newest
articles.
"""
if find:
results = await retrieve_search_results_by_tags(find.split())
if len(results) < 20:
if len(results) < 10:
new_results = scrap_from_search(find)
new_results = await add_search_results(new_results)
results.extend(new_results)
return results[:20]
return await retrieve_newest_search_results()

# check for adding only unic
for new_one in new_results:
repeats = False
for old_one in results:
if new_one['id'] == old_one['id']:
repeats = True
break
if not repeats:
results.append(new_one)

return paginate(results)
return paginate(await retrieve_newest_search_results())


@router.get("/{id}", status_code=status.HTTP_200_OK, response_model=ExtendArticleModel)
Expand Down

0 comments on commit 3d32a5e

Please sign in to comment.