-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo_handler.py
More file actions
136 lines (119 loc) · 4.1 KB
/
Copy pathmongo_handler.py
File metadata and controls
136 lines (119 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from datetime import datetime
from pymongo import MongoClient
from pymongo.collection import Collection
class MongoHandler:
collection: Collection
SCRAPED_BUT_NO_TEXT_PIPELINE_EXTENSION = {
'$match': {
'scraped_selenium': {
'$exists': True
},
'text_selenium': {
'$exists': False
}
}
}
def __init__(self, url: str = '127.0.0.1', db_name: str = 'nvd', collection_name: str = 'nvd_all'):
self.collection = self.__get_mongo_colection(url, db_name, collection_name)
def get_reference_list_for_url(self, url: str, only_scraped: bool = False, only_not_scraped: bool = False,
only_scraped_but_no_text: bool = False) -> []:
pipeline: [] = self.__get_pipeline_for_url(url)
if only_scraped:
pipeline.append(MongoHandler.SCRAPED_PIPELINE_EXTENSION)
elif only_not_scraped:
pipeline.append(MongoHandler.NOT_SCRAPED_PIPELINE_EXTENSION)
elif only_scraped_but_no_text:
pipeline.append(MongoHandler.SCRAPED_BUT_NO_TEXT_PIPELINE_EXTENSION)
return list(self.collection.aggregate(pipeline))
def __get_mongo_colection(self, url: str, db_name: str, collection_name: str) -> Collection:
mongo_client = MongoClient(url)
mongo_db = mongo_client[db_name]
mongo_collection = mongo_db[collection_name]
return mongo_collection
SCRAPED_PIPELINE_EXTENSION = {
'$match': {
'scraped_selenium': {
'$exists': True
}
}
}
NOT_SCRAPED_PIPELINE_EXTENSION = {
'$match': {
'scraped_selenium': {
'$exists': False
}
}
}
def __get_pipeline_for_url(self, url: str) -> []:
pipeline = [
{
'$unwind': {
'path': '$reference_data'
}
}, {
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'_id': '$_id'
}, '$reference_data'
]
}
}
}, {
'$match': {
'url': {
'$regex': f'.*{url}*'
}
}
}, {
'$project': {
'url': True,
'text_selenium': True,
'scraped_selenium': True
}
}
]
return pipeline
def get_scraped_without_text(self) -> []:
pipeline = [
{
'$unwind': {
'path': '$reference_data'
}
}, {
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
'$$ROOT', '$reference_data'
]
}
}
}, {
'$project': {
'reference_data': 0,
'cpe': 0,
'cvssv2': 0,
'cwe': 0,
'references': 0
}
}, {
'$match': {
'scraped_selenium': {
'$exists': True
},
'text_selenium': {
'$exists': False
}
}
}
]
return list(self.collection.aggregate(pipeline))
def insert_text_in_mongo(self, id: str, url: str, text: str) -> None:
full_record = self.collection.find_one({"_id": id})
for i, e in enumerate(full_record['reference_data']):
if e['url'] == url:
full_record['reference_data'][i]['scraped_selenium'] = datetime.now()
if text and len(text):
full_record['reference_data'][i]['text_selenium'] = text
self.collection.update_one({'_id': id}, {'$set': full_record})