Skip to content

Commit 690a8dc

Browse files
committed
Add SQLite and MongoDB implementations for YouTube video management with CRUD operations
1 parent 8763270 commit 690a8dc

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

youtube_manager/youtube_videos.db

8 KB
Binary file not shown.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from pymongo import MongoClient
2+
from bson import ObjectId
3+
4+
client = MongoClient("mongodb+srv://CHAI:CHAI@cluster0.lxl3fsq.mongodb.net/", 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()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import sqlite3
2+
3+
conn = sqlite3.connect('youtube_videos.db')
4+
5+
cursor = conn.cursor()
6+
7+
cursor.execute('''
8+
CREATE TABLE IF NOT EXISTS videos (
9+
id INTEGER PRIMARY KEY,
10+
name TEXT NOT NULL,
11+
time TEXT NOT NULL
12+
)
13+
''')
14+
15+
def list_videos():
16+
cursor.execute("SELECT * FROM videos")
17+
for row in cursor.fetchall():
18+
print(row)
19+
20+
def add_video(name, time):
21+
cursor.execute("INSERT INTO videos (name, time) VALUES (?, ?)", (name, time))
22+
conn.commit()
23+
24+
def update_video(video_id, new_name, new_time):
25+
cursor.execute("UPDATE videos SET name = ?, time = ? WHERE id = ?", (new_name, new_time, video_id))
26+
conn.commit()
27+
28+
def delete_video(video_id):
29+
cursor.execute("DELETE FROM videos where id = ?", (video_id,))
30+
conn.commit()
31+
32+
def main():
33+
while True:
34+
print("\n Youtube manager app with DB")
35+
print("1. List Videos")
36+
print("2. Add Videos")
37+
print("3. Update Videos")
38+
print("4. Delete Videos")
39+
print("5. exit app")
40+
choice = input("Enter your choice: ")
41+
42+
if choice == '1':
43+
list_videos()
44+
elif choice == '2':
45+
name = input("Enter the video name: ")
46+
time = input("Enter the video time: ")
47+
add_video(name, time)
48+
elif choice == '3':
49+
video_id = input("Enter video ID to update: ")
50+
name = input("Enter the video name: ")
51+
time = input("Enter the video time: ")
52+
update_video(video_id, name, time)
53+
elif choice == '4':
54+
video_id = input("Enter video ID to delete: ")
55+
delete_video(video_id)
56+
elif choice == '5':
57+
break
58+
else:
59+
print("Invalid Choice ")
60+
61+
conn.close()
62+
63+
if __name__ == "__main__":
64+
main()

0 commit comments

Comments
 (0)