forked from GX4FM-Base-X/SemanticDataLink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
69 lines (57 loc) · 1.94 KB
/
Copy pathhelpers.py
File metadata and controls
69 lines (57 loc) · 1.94 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
import hashlib
from pymongo import MongoClient
from urllib.parse import quote_plus
import json
def generate_said(data):
data_string = str(data).encode()
hash_object = hashlib.sha256()
hash_object.update(data_string)
return hash_object.hexdigest()
# Load Data into MongoDB
username = "admin"
password = "admin"
uri = f"mongodb://{quote_plus(username)}:{quote_plus(password)}@localhost:27017/mydatabase?authSource=admin"
client = MongoClient(uri)
db = client['SemanticDataLink']
collection = db['SemanticDataLink']
def insert_capture_base(data, capture_base_hash):
# Insert Data into MongoDBs
# Check if a document with the same capture_base exists
try:
existing_document = collection.find_one(
{"ref_capture_base": capture_base_hash})
if existing_document is None:
# If no existing document, insert the new data
data['ref_capture_base'] = capture_base_hash
# print(data)
collection.insert_one(data)
print("capture_base inserted.")
return 0
else:
print("capture_base already exists.")
return 1
except:
return -1
def insert_overlay(data, overlay_hash):
# Insert Data into MongoDBs
# Check if a document with the same capture_base exists
try:
existing_document = collection.find_one(
{"overlay_hash": overlay_hash})
if existing_document is None:
# If no existing document, insert the new data
data['overlay_hash'] = overlay_hash
collection.insert_one(data)
return 0
else:
return 1
except:
return -1
def validateJSON(jsonData):
try:
json.loads(jsonData)
except ValueError as err:
return False
return True
def count_keys_except(dictionary, key1='capture_base', key2='type', key3='language'):
return sum(1 for key in dictionary if key not in [key1, key2, key3])