Repository navigation
Expand file tree
/
Copy pathmp_db.py
More file actions
61 lines (48 loc) · 1.91 KB
/
Copy pathmp_db.py
File metadata and controls
61 lines (48 loc) · 1.91 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
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from urllib.parse import quote_plus
from scrape import get_route, get_area, get_id, get_directory
username = quote_plus('EvilMonkey')
password = quote_plus('&a@JREztYS5@EyPL')
cluster = '<clusterName>'
authSource = '<authSource>'
authMechanism = '<authMechanism>'
uri = 'mongodb+srv://' + username + ':' + password + '@cluster0.rhyjndk.mongodb.net/?retryWrites=true&w=majority'
# Create a new client and connect to the server
client = MongoClient(uri, server_api=ServerApi('1'))
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
db = client["mountain_project"]
def populate_routes_in(areas, routes, area_id):
area = get_area(area_id)
for child in area['children']:
child_id = str(child['id'])
if child['type'] == 'Route':
# Check if route exists
id_str = str(child['id'])
new_route = {
"_id": ObjectId(id_str),
}
route_exists = routes.find_one({"_id": id_str})
if route_exists is None:
# Object doesn't exist, so add it to the collection
result = routes.insert_one(new_route)
print("New route added with _id:", result.inserted_id)
else:
# Object with the same name already exists; you can update it or take other action
print("Route {id_str} already exists.")
else:
populate_routes_in(areas, routes, child_id)
def populate_routes(db):
areas = db['areas']
routes = db['routes']
directory = get_directory()
del directory['International']
for state in directory:
area_id = get_id(directory[state])
populate_routes_in(area_id, areas, routes)
break