Skip to content

Commit 8763270

Browse files
committed
Add YouTube manager functionality with video CRUD operations and JSON data handling
1 parent 2edc284 commit 8763270

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

Error_handling/error_handle.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
file = open('youtube.txt', 'w')
2+
3+
try:
4+
file.write('chai aur code')
5+
finally:
6+
file.close()
7+
8+
with open('youtube.txt', 'w') as file:
9+
file.write('chai aur python')

youtube_manager/youtube.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"name": "Waseem", "time": "15"}]

youtube_manager/youtube_manager.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy