-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutils.py
86 lines (65 loc) · 2.52 KB
/
utils.py
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
import ngrok
from datetime import datetime
import firebase_admin
import os
import certifi
from pymongo import MongoClient
from dotenv import load_dotenv
load_dotenv()
BASE_CURR_FILE_PATH = os.path.dirname(os.path.abspath(__file__))
class mongo_db_connection():
def __init__(self, db_name):
self.uri = os.getenv("MONGO_URI")
self.ca = certifi.where()
self.client = MongoClient(self.uri, tlsCAFile=self.ca)
try:
self.client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
self.db = self.client[db_name]
def insert_one(self, collection_name, item):
collection = self.db[collection_name]
mongo_id = collection.insert_one(item)
return mongo_id.inserted_id
def get_all(self, collection_name):
collection = self.db[collection_name]
all_items = collection.find()
return all_items
def delete_item(self, collection_name, item):
collection = self.db[collection_name]
collection.delete_one(item)
def find_one_by_uiqu_id(self, collection_name, id):
all_items = self.get_all(collection_name)
for item in all_items:
if id in item:
return item
return None
def is_uiqu_id_exist(self, collection_name, id):
all_items = self.get_all(collection_name)
for item in all_items:
if id in item:
return True
return False
def update_by_mongo_id(self, collection_name, mongo_id, new_json):
collection = self.db[collection_name]
collection.update_one({"_id": mongo_id}, {"$set": new_json})
def update_remote_ip_ngrok_mongo(db_name, collection, port, currStatus=0):
"""
currStatus: 1 to start server
0 to stop server
"""
conn = mongo_db_connection(db_name)
##getting all data
for i in conn.get_all(collection):
mongo_id = i['_id']
if currStatus == 1:
listner = ngrok.forward(addr="localhost:"+str(port),authtoken=os.getenv("NGROK_AUTH_TOKEN"))
conn.update_by_mongo_id(collection, mongo_id, {"ip_ep": listner.url(), "time": datetime.now(), "type": "server", "status": 1})
print(listner.url())
else:
conn = mongo_db_connection(db_name)
##getting all data
for i in conn.get_all(collection):
mongo_id = i['_id']
conn.update_by_mongo_id(collection, mongo_id, {"status": 0})