Spaces:
Sleeping
Sleeping
Update pages/lecture_finder.py
Browse files- pages/lecture_finder.py +123 -122
pages/lecture_finder.py
CHANGED
@@ -1,123 +1,124 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import googleapiclient.discovery
|
3 |
-
|
4 |
-
from
|
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 |
-
hours =
|
51 |
-
|
52 |
-
|
53 |
-
minutes =
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
st.
|
70 |
-
st.sidebar.
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
st.markdown(f"
|
114 |
-
st.markdown(
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
|
|
123 |
main()
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import googleapiclient.discovery
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from datetime import timedelta
|
6 |
+
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Set up YouTube API client
|
11 |
+
api_service_name = "youtube"
|
12 |
+
api_version = "v3"
|
13 |
+
DEVELOPER_KEY = os.getenv('DEVELOPER_KEY')
|
14 |
+
youtube = googleapiclient.discovery.build(api_service_name, api_version, developerKey=DEVELOPER_KEY)
|
15 |
+
|
16 |
+
def search_youtube(query, max_results=50):
|
17 |
+
try:
|
18 |
+
request = youtube.search().list(
|
19 |
+
q=query,
|
20 |
+
type="video",
|
21 |
+
part="id,snippet",
|
22 |
+
maxResults=max_results,
|
23 |
+
fields="items(id(videoId),snippet(title,description,thumbnails))"
|
24 |
+
)
|
25 |
+
response = request.execute()
|
26 |
+
return response.get('items', [])
|
27 |
+
except googleapiclient.errors.HttpError as e:
|
28 |
+
st.error(f"An error occurred: {e}")
|
29 |
+
return []
|
30 |
+
|
31 |
+
def get_video_details(video_id):
|
32 |
+
try:
|
33 |
+
request = youtube.videos().list(
|
34 |
+
part="contentDetails,statistics",
|
35 |
+
id=video_id,
|
36 |
+
fields="items(contentDetails(duration),statistics(viewCount))"
|
37 |
+
)
|
38 |
+
response = request.execute()
|
39 |
+
return response['items'][0] if response['items'] else None
|
40 |
+
except googleapiclient.errors.HttpError as e:
|
41 |
+
st.error(f"An error occurred while fetching video details: {e}")
|
42 |
+
return None
|
43 |
+
|
44 |
+
def format_duration(duration):
|
45 |
+
duration = duration.replace('PT', '')
|
46 |
+
hours = 0
|
47 |
+
minutes = 0
|
48 |
+
seconds = 0
|
49 |
+
if 'H' in duration:
|
50 |
+
hours, duration = duration.split('H')
|
51 |
+
hours = int(hours)
|
52 |
+
if 'M' in duration:
|
53 |
+
minutes, duration = duration.split('M')
|
54 |
+
minutes = int(minutes)
|
55 |
+
if 'S' in duration:
|
56 |
+
seconds = int(duration.replace('S', ''))
|
57 |
+
return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
|
58 |
+
|
59 |
+
def parse_duration(duration_str):
|
60 |
+
parts = duration_str.split(':')
|
61 |
+
if len(parts) == 3:
|
62 |
+
return timedelta(hours=int(parts[0]), minutes=int(parts[1]), seconds=int(parts[2]))
|
63 |
+
elif len(parts) == 2:
|
64 |
+
return timedelta(minutes=int(parts[0]), seconds=int(parts[1]))
|
65 |
+
else:
|
66 |
+
return timedelta(seconds=int(parts[0]))
|
67 |
+
|
68 |
+
def main():
|
69 |
+
st.set_page_config(page_title="S.H.E.R.L.O.C.K. Learning Assistant", page_icon="🕵️", layout="wide")
|
70 |
+
st.sidebar.title("S.H.E.R.L.O.C.K.")
|
71 |
+
st.sidebar.markdown("""
|
72 |
+
**S**ystematic **H**olistic **E**ducational **R**esource for **L**earning and **O**ptimizing **C**ognitive **K**nowledge
|
73 |
+
|
74 |
+
Enhance your cognitive abilities, memory techniques, and subject-specific knowledge with AI-powered personalized learning.
|
75 |
+
""")
|
76 |
+
|
77 |
+
query = st.sidebar.text_input("What would you like to learn about?", "")
|
78 |
+
|
79 |
+
min_duration = st.sidebar.selectbox(
|
80 |
+
"Minimum video duration",
|
81 |
+
["Any", "5:00", "10:00", "15:00", "30:00", "45:00", "1:00:00"],
|
82 |
+
index=0
|
83 |
+
)
|
84 |
+
|
85 |
+
search_button = st.sidebar.button("Search for Learning Resources")
|
86 |
+
|
87 |
+
st.title("Learning Resources")
|
88 |
+
|
89 |
+
if search_button and query:
|
90 |
+
with st.spinner("Searching for the best learning resources..."):
|
91 |
+
results = search_youtube(query)
|
92 |
+
|
93 |
+
if results:
|
94 |
+
filtered_results = []
|
95 |
+
for item in results:
|
96 |
+
video_id = item['id']['videoId']
|
97 |
+
video_details = get_video_details(video_id)
|
98 |
+
|
99 |
+
if video_details:
|
100 |
+
duration = video_details['contentDetails']['duration']
|
101 |
+
formatted_duration = format_duration(duration)
|
102 |
+
views = int(video_details['statistics']['viewCount'])
|
103 |
+
|
104 |
+
if min_duration == "Any" or parse_duration(formatted_duration) >= parse_duration(min_duration):
|
105 |
+
filtered_results.append((item, formatted_duration, views))
|
106 |
+
|
107 |
+
if filtered_results:
|
108 |
+
for item, duration, views in filtered_results:
|
109 |
+
col1, col2 = st.columns([1, 3])
|
110 |
+
with col1:
|
111 |
+
st.image(item['snippet']['thumbnails']['medium']['url'], use_column_width=True)
|
112 |
+
with col2:
|
113 |
+
st.markdown(f"### [{item['snippet']['title']}](https://www.youtube.com/watch?v={item['id']['videoId']})")
|
114 |
+
st.markdown(f"**Duration:** {duration} | **Views:** {views:,}")
|
115 |
+
st.markdown(item['snippet']['description'])
|
116 |
+
|
117 |
+
st.markdown("---")
|
118 |
+
else:
|
119 |
+
st.warning("No results found matching your duration criteria. Try adjusting the minimum duration or search query.")
|
120 |
+
else:
|
121 |
+
st.warning("No results found. Please try a different search query.")
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
main()
|