-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathytmanager_with_mongodb.py
More file actions
59 lines (48 loc) · 1.91 KB
/
ytmanager_with_mongodb.py
File metadata and controls
59 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
from pymongo import MongoClient
from bson import ObjectId
client = MongoClient("mongodb+srv://CHAI:CHAI@cluster0.lxl3fsq.mongodb.net/", tlsAllowInvalidCertificates=True)
# Not a good idea to include id and password in code files
# tlsAllowInvalidCertificates=True - Not a good way to handle ssl
print(client)
db = client["ytmanager"]
video_collection = db["videos"]
# print(video_collection)
def add_video(name, time):
video_collection.insert_one({"name": name, "time": time})
def list_videos():
for video in video_collection.find():
print(f"ID: {video['_id']}, Name: {video['name']} and Time: {video['time']}")
def update_video(video_id, new_name, new_time):
video_collection.update_one({'_id': ObjectId(video_id)}, {"$set": {"name": new_name, "time": new_time}})
def delete_video(video_id):
video_collection.delete_one({"_id": video_id})
# TODO: debug this video_id problem
def main():
while True:
print("\n Youtube manager App")
print("1. List all videos")
print("2. Add a new videos")
print("3. Update a videos")
print("4. Delete a videos")
print("5. Exit the app")
choice = input("Enter your choice: ")
if choice == '1':
list_videos()
elif choice == '2':
name = input("Enter the video name: ")
time = input("Enter the video time: ")
add_video(name, time)
elif choice == '3':
video_id = input("Enter the video id to update: ")
name = input("Enter the updated video name: ")
time = input("Enter the updated video time: ")
update_video(video_id, name, time)
elif choice == '4':
video_id = input("Enter the video id to update: ")
delete_video(video_id, name, time)
elif choice == '5':
break
else:
print("Invalid choice")
if __name__ == "__main__":
main()