|
import pymongo |
|
import os |
|
import pandas as pd |
|
from config import PRODUCTION_URL, PRODUCTION_DATABASE, PRODUCTION_COLLECTION |
|
from logger import get_logger |
|
|
|
|
|
logger = get_logger() |
|
|
|
class DBRead: |
|
""" |
|
Reads news from MongoDB |
|
""" |
|
def __init__(self): |
|
self.url = PRODUCTION_URL |
|
self.database = PRODUCTION_DATABASE |
|
self.collection = PRODUCTION_COLLECTION |
|
self.__client = None |
|
self.__error = 0 |
|
|
|
def __connect(self): |
|
try: |
|
self.__client = pymongo.MongoClient(self.url) |
|
_ = self.__client.list_database_names() |
|
except Exception as conn_exception: |
|
self.__error = 1 |
|
self.__client = None |
|
|
|
def __read(self): |
|
try: |
|
db = self.__client[self.database] |
|
coll = db[self.collection] |
|
docs = [] |
|
for doc in coll.find(): |
|
docs.append(doc) |
|
rss_df = pd.DataFrame(docs) |
|
except Exception as insert_err: |
|
self.__error = 1 |
|
rss_df = pd.DataFrame() |
|
return rss_df |
|
|
|
def __close_connection(self): |
|
if self.__client is not None: |
|
self.__client.close() |
|
self.__client = None |
|
|
|
def read_news_from_db(self): |
|
logger.warning('Entering read_news_from_db() to read old news') |
|
rss_df = pd.DataFrame() |
|
if self.url is not None: |
|
if self.__error == 0: |
|
self.__connect() |
|
if self.__error == 0: |
|
rss_df = self.__read() |
|
if self.__error == 0: |
|
logger.warning(f"Read Successful. Read {len(rss_df)} documents.") |
|
if self.__client is not None: |
|
self.__close_connection() |
|
logger.warning(f'Exiting read_news_from_db()') |
|
if len(rss_df) == 0: |
|
rss_df = None |
|
return rss_df |