1
+ from pymongo import MongoClient
2
+ from bson import ObjectId
3
+
4
+ client = MongoClient (
"mongodb+srv://CHAI:[email protected] /" ,
tlsAllowInvalidCertificates = True )
5
+ # Not a good idea to include id and password in code files
6
+ # tlsAllowInvalidCertificates=True - Not a good way to handle ssl
7
+
8
+ print (client )
9
+ db = client ["ytmanager" ]
10
+ video_collection = db ["videos" ]
11
+
12
+ # print(video_collection)
13
+
14
+ def add_video (name , time ):
15
+ video_collection .insert_one ({"name" : name , "time" : time })
16
+
17
+ def list_videos ():
18
+ for video in video_collection .find ():
19
+ print (f"ID: { video ['_id' ]} , Name: { video ['name' ]} and Time: { video ['time' ]} " )
20
+
21
+ def update_video (video_id , new_name , new_time ):
22
+ video_collection .update_one ({'_id' : ObjectId (video_id )}, {"$set" : {"name" : new_name , "time" : new_time }})
23
+
24
+ def delete_video (video_id ):
25
+ video_collection .delete_one ({"_id" : video_id })
26
+ # TODO: debug this video_id problem
27
+
28
+
29
+ def main ():
30
+ while True :
31
+ print ("\n Youtube manager App" )
32
+ print ("1. List all videos" )
33
+ print ("2. Add a new videos" )
34
+ print ("3. Update a videos" )
35
+ print ("4. Delete a videos" )
36
+ print ("5. Exit the app" )
37
+ choice = input ("Enter your choice: " )
38
+
39
+ if choice == '1' :
40
+ list_videos ()
41
+ elif choice == '2' :
42
+ name = input ("Enter the video name: " )
43
+ time = input ("Enter the video time: " )
44
+ add_video (name , time )
45
+ elif choice == '3' :
46
+ video_id = input ("Enter the video id to update: " )
47
+ name = input ("Enter the updated video name: " )
48
+ time = input ("Enter the updated video time: " )
49
+ update_video (video_id , name , time )
50
+ elif choice == '4' :
51
+ video_id = input ("Enter the video id to update: " )
52
+ delete_video (video_id , name , time )
53
+ elif choice == '5' :
54
+ break
55
+ else :
56
+ print ("Invalid choice" )
57
+
58
+ if __name__ == "__main__" :
59
+ main ()
0 commit comments