Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -43,18 +43,9 @@ def scrape_comments(video_id):
|
|
43 |
return comments_df
|
44 |
|
45 |
|
46 |
-
# Function to
|
47 |
-
def
|
48 |
-
|
49 |
-
if match:
|
50 |
-
return match.group(0)
|
51 |
-
else:
|
52 |
-
st.error("Invalid YouTube video URL")
|
53 |
-
|
54 |
-
# Function to fetch YouTube comments for a video ID
|
55 |
-
def fetch_comments(video_id):
|
56 |
-
# Example using youtube-comment-scraper-python library
|
57 |
-
comments = scrape_comments(video_id)
|
58 |
|
59 |
request = youtube.videos().list(
|
60 |
part="snippet",
|
@@ -65,12 +56,27 @@ def fetch_comments(video_id):
|
|
65 |
if response['items']:
|
66 |
video_info = response['items'][0]['snippet']
|
67 |
channel_name = video_info['channelTitle']
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
else:
|
70 |
-
|
71 |
-
video_description = "No description available"
|
72 |
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
# Function to analyze sentiment for a single comment
|
76 |
def analyze_sentiment(comment):
|
@@ -153,8 +159,10 @@ def main():
|
|
153 |
comments_df['sentiment'] = comments_df['comment'].apply(lambda x: analyze_sentiment(x[:512]))
|
154 |
sentiment_counts = comments_df['sentiment'].value_counts()
|
155 |
|
|
|
|
|
156 |
st.write(f"**Channel Name:** {channel_name}")
|
157 |
-
st.write(f"**Video Description:** {
|
158 |
|
159 |
st.write("Based on top :100: comments from this video")
|
160 |
# Create pie chart
|
|
|
43 |
return comments_df
|
44 |
|
45 |
|
46 |
+
# Function to fetch video metadata using YouTube API
|
47 |
+
def fetch_video_info(video_id):
|
48 |
+
video_id = extract_video_id(video_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
request = youtube.videos().list(
|
51 |
part="snippet",
|
|
|
56 |
if response['items']:
|
57 |
video_info = response['items'][0]['snippet']
|
58 |
channel_name = video_info['channelTitle']
|
59 |
+
video_title = video_info['title']
|
60 |
+
return channel_name, video_title
|
61 |
+
|
62 |
+
else:
|
63 |
+
raise ValueError("Video not found")
|
64 |
+
|
65 |
+
|
66 |
+
# Function to extract video ID from YouTube URL
|
67 |
+
def extract_video_id(video_url):
|
68 |
+
match = re.search(r'(?<=v=)[\w-]+', video_url)
|
69 |
+
if match:
|
70 |
+
return match.group(0)
|
71 |
else:
|
72 |
+
st.error("Invalid YouTube video URL")
|
|
|
73 |
|
74 |
+
# Function to fetch YouTube comments for a video ID
|
75 |
+
def fetch_comments(video_id):
|
76 |
+
# Example using youtube-comment-scraper-python library
|
77 |
+
comments_df = scrape_comments(video_id)
|
78 |
+
|
79 |
+
return comments_df
|
80 |
|
81 |
# Function to analyze sentiment for a single comment
|
82 |
def analyze_sentiment(comment):
|
|
|
159 |
comments_df['sentiment'] = comments_df['comment'].apply(lambda x: analyze_sentiment(x[:512]))
|
160 |
sentiment_counts = comments_df['sentiment'].value_counts()
|
161 |
|
162 |
+
channel_name, video_title = fetch_video_info(video_id)
|
163 |
+
|
164 |
st.write(f"**Channel Name:** {channel_name}")
|
165 |
+
st.write(f"**Video Description:** {video_title}")
|
166 |
|
167 |
st.write("Based on top :100: comments from this video")
|
168 |
# Create pie chart
|