Commit
•
24f1253
1
Parent(s):
99ce67b
Delete db_operations
Browse files- db_operations/__init__.py +0 -1
- db_operations/db_operations.py +0 -52
db_operations/__init__.py
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
from db_operations.db_operations import *
|
|
|
|
db_operations/db_operations.py
DELETED
@@ -1,52 +0,0 @@
|
|
1 |
-
import pymongo
|
2 |
-
import os
|
3 |
-
|
4 |
-
|
5 |
-
class DBOperations:
|
6 |
-
"""
|
7 |
-
Inserts processed news into MongoDB
|
8 |
-
"""
|
9 |
-
def __init__(self):
|
10 |
-
self.url = os.getenv('DB_URL')
|
11 |
-
self.database = "rss_news_db"
|
12 |
-
self.collection = "rss_news"
|
13 |
-
self.__client = None
|
14 |
-
self.__error = 0
|
15 |
-
|
16 |
-
def __connect(self):
|
17 |
-
try:
|
18 |
-
self.__client = pymongo.MongoClient(self.url)
|
19 |
-
_ = self.__client.list_database_names()
|
20 |
-
except Exception as conn_exception:
|
21 |
-
self.__error = 1
|
22 |
-
self.__close_connection()
|
23 |
-
self.__client = None
|
24 |
-
raise
|
25 |
-
|
26 |
-
def __insert(self, documents):
|
27 |
-
try:
|
28 |
-
|
29 |
-
db = self.__client[self.database]
|
30 |
-
coll = db[self.collection]
|
31 |
-
coll.drop()
|
32 |
-
coll.insert_many(documents=documents)
|
33 |
-
except Exception as insert_err:
|
34 |
-
self.__error = 1
|
35 |
-
self.__close_connection()
|
36 |
-
raise
|
37 |
-
|
38 |
-
def __close_connection(self):
|
39 |
-
if self.__client is not None:
|
40 |
-
self.__client.close()
|
41 |
-
self.__client = None
|
42 |
-
|
43 |
-
def insert_news_into_db(self, documents: list):
|
44 |
-
if self.url is not None:
|
45 |
-
if self.__error == 0:
|
46 |
-
self.__connect()
|
47 |
-
if self.__error == 0:
|
48 |
-
self.__insert(documents=documents)
|
49 |
-
if self.__error == 0:
|
50 |
-
print("Insertion Successful")
|
51 |
-
if self.__client is not None:
|
52 |
-
self.__close_connection()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|