1
+ import json
2
+
3
+ def load_data ():
4
+ try :
5
+ with open ('youtube.txt' , 'r' ) as file :
6
+ test = json .load (file )
7
+ # print(type(test))
8
+ return test
9
+ except FileNotFoundError :
10
+ return []
11
+
12
+ def save_data_helper (videos ):
13
+ with open ('youtube.txt' , 'w' ) as file :
14
+ json .dump (videos , file )
15
+
16
+ def list_all_videos (videos ):
17
+ print ("\n " )
18
+ print ("*" * 70 )
19
+ for index , video in enumerate (videos , start = 1 ):
20
+ print (f"{ index } . { video ['name' ]} , Duration: { video ['time' ]} " )
21
+ print ("\n " )
22
+ print ("*" * 70 )
23
+
24
+ def add_video (videos ):
25
+ name = input ("Enter video name: " )
26
+ time = input ("Enter video time: " )
27
+ videos .append ({'name' : name , 'time' : time })
28
+ save_data_helper (videos )
29
+
30
+ def update_video (videos ):
31
+ list_all_videos (videos )
32
+ index = int (input ("Enter the video number to update" ))
33
+ if 1 <= index <= len (videos ):
34
+ name = input ("Enter the new video name" )
35
+ time = input ("Enter the new video time" )
36
+ videos [index - 1 ] = {'name' :name , 'time' : time }
37
+ save_data_helper (videos )
38
+ else :
39
+ print ("Invalid index selected" )
40
+
41
+
42
+ def delete_video (videos ):
43
+ list_all_videos (videos )
44
+ index = int (input ("Enter the video number to be deleted" ))
45
+
46
+ if 1 <= index <= len (videos ):
47
+ del videos [index - 1 ]
48
+ save_data_helper (videos )
49
+ else :
50
+ print ("Invalid video index selected" )
51
+
52
+
53
+ def main ():
54
+ videos = load_data ()
55
+ while True :
56
+ print ("\n Youtube Manager | choose an option " )
57
+ print ("1. List all youtube videos " )
58
+ print ("2. Add a youtube video " )
59
+ print ("3. Update a youtube video details " )
60
+ print ("4. Delete a youtube video " )
61
+ print ("5. Exit the app " )
62
+ choice = input ("Enter your choice: " )
63
+ # print(videos)
64
+
65
+ match choice :
66
+ case '1' :
67
+ list_all_videos (videos )
68
+ case '2' :
69
+ add_video (videos )
70
+ case '3' :
71
+ update_video (videos )
72
+ case '4' :
73
+ delete_video (videos )
74
+ case '5' :
75
+ break
76
+ case _:
77
+ print ("Invalid Choice" )
78
+
79
+ if __name__ == "__main__" :
80
+ main ()
0 commit comments