Skip to content

Commit

Permalink
added docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
LLkaia committed Jan 11, 2024
1 parent 01b9848 commit 1b73e92
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 13 deletions.
5 changes: 4 additions & 1 deletion fill_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@


async def fill():
"""Dump news corresponding to laptops"""
with open('laptop_models.json') as f:
laptops = json.load(f)
print('[INFO] loaded laptops')
print('[INFO] loaded laptops')

for i, laptop in enumerate(laptops):
name = (laptop.get('producer') + ' ' + laptop.get('model')).lower()
print(f'[INFO] process {i} laptop')
await update_search_results(name)


if __name__ == '__main__':
import asyncio
asyncio.run(fill())
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""start app"""
import uvicorn


Expand Down
5 changes: 0 additions & 5 deletions server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,3 @@

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


@app.get('/', tags=['Root'])
async def read_root():
return {'message': 'Welcome!'}
3 changes: 1 addition & 2 deletions server/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
MONGO_DETAILS = 'mongodb://mongodb:27017'
client = motor.motor_asyncio.AsyncIOMotorClient(MONGO_DETAILS)
db = client.lappy

search_results_collection = db.get_collection('search_results')


def search_results_helper(search_result):
"""Take each article and convert it to JSONable format"""
"""Convert each article to JSONable format"""
return {
"id": str(search_result["_id"]),
"link": search_result["link"],
Expand Down
1 change: 0 additions & 1 deletion server/models/search_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class ArticleModel(BaseModel):
image: HttpUrl | None = None
date: datetime | None = None
description: str = ""
tags: set[str] = set()


class SearchResponseModel(BaseModel):
Expand Down
8 changes: 4 additions & 4 deletions server/routes/search_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ async def get_search_results(find: Annotated[str | None, Query(description='Writ
return {'count': count, 'results': results}


@router.get("/{id}", status_code=status.HTTP_200_OK, response_model=ExtendArticleModel)
async def get_article(id: str):
@router.get("/{id_}", status_code=status.HTTP_200_OK, response_model=ExtendArticleModel)
async def get_article(id_: str):
"""Get concrete article with content
Find article by ID in database and if it exists, check if it
has content in 'content' field. If it is, return it, else scrap
this content. If article is not exist in db, return 404.
"""
result = await retrieve_search_result_by_id(id)
result = await retrieve_search_result_by_id(id_)
if result:
if not result['content']:
content = scrap_content(result['link'])
result = await update_content_of_article(id, content)
result = await update_content_of_article(id_, content)
return result
else:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Item not found")

0 comments on commit 1b73e92

Please sign in to comment.