Skip to content

Commit

Permalink
(#29)
Browse files Browse the repository at this point in the history
* Separate `get_article` function to `get_article_by_id` and `get_article_by_url`
* Fix `get_many` function in `mongodb_driver`
  • Loading branch information
kggold4 committed Jun 8, 2023
1 parent 2611046 commit 15b2c3a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion api/server_api/api_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def get_similar_articles_data(self, article_url: str) -> List[ArticleApiData]:
"""
self.server_logger.debug(f"Checking similar articles to article -> `{article_url}`")
similar_articles: List[ArticleApiData] = list()
article_object: Article = self._article_utils.get_article(article_url=article_url)
article_object: Article = self._article_utils.get_article_by_url(article_url=article_url)

if not article_object:
desc = f"Didn't find article in db with article url: `{article_url}`"
Expand Down
2 changes: 1 addition & 1 deletion db_driver/mongodb_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def get_many(self, table_name: str, data_filter: dict) -> List[dict]:
if res:
object_id = res.cursor_id
self.logger.info(f"Got data from db: '{self.DB_NAME}', table_name: '{table_name}', id: '{object_id}'")
return list(dict(res))
return list(res)
else:
desc = f"Error find data with filter: {data_filter}, table: '{table_name}', db: '{self.DB_NAME}'"
self.logger.warning(desc)
Expand Down
15 changes: 14 additions & 1 deletion server_utils/db_utils/article_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,19 @@ def get_unclassified_article(self, required_filter_data: dict = None, get_random

return Article(**article)

def get_article(self, article_url: str) -> Union[Article, None]:
def get_article_by_id(self, article_id: str) -> Union[Article, None]:
article = None
data_filter = {"article_id": article_id}
try:
article_data = self._db.get_one(table_name=DBConsts.ARTICLE_TABLE_NAME, data_filter=data_filter)
article_object: Article = get_db_object_from_dict(object_dict=article_data, class_instance=Article)
article = article_object
except DataNotFoundDBException as e:
self.logger.warning(f"Error get article by article id: `{article_id}` - {str(e)}")
self.logger.info(f"Got article from db, article_id: `{article.article_id}`, url: `{article.url}`")
return article

def get_article_by_url(self, article_url: str) -> Union[Article, None]:
article = None
data_filter = {"url": article_url}
try:
Expand All @@ -70,6 +82,7 @@ def get_article(self, article_url: str) -> Union[Article, None]:
return article

def get_articles(self, articles_id: List[str]) -> List[Article]:
# todo: separate this function to: get_articles_by_ids and get_articles_by_urls
articles: List[Article] = []
data_filter = {"article_id": {"$in": articles_id}}
articles_data = self._db.get_many(table_name=DBConsts.ARTICLE_TABLE_NAME, data_filter=data_filter)
Expand Down
2 changes: 1 addition & 1 deletion server_utils/db_utils/cluster_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ def get_cluster(self, cluster_id: str) -> Cluster:
# For debug
if __name__ == '__main__':
article_utils = ArticleUtils()
new_article: Article = article_utils.get_article(article_url="https://www.bbc.com/news/world-europe-65471904")
new_article: Article = article_utils.get_article_by_url(article_url="https://www.bbc.com/news/world-europe-65471904")
cluster_utils = ClusterUtils()
cluster_utils.create_new_cluster(article=new_article, classified_categories=["finance", "bitcoin"])

0 comments on commit 15b2c3a

Please sign in to comment.