Spaces:
Runtime error
Runtime error
First run demo
Browse files
app.py
CHANGED
|
@@ -1,4 +1,62 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import googleapiclient.discovery
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
+
def extract_comments_from_video(video_id,youtube_api_key):
|
| 8 |
+
try:
|
| 9 |
+
youtube = googleapiclient.discovery.build(
|
| 10 |
+
api_service_name, api_version, developerKey = youtube_api_key)
|
| 11 |
+
|
| 12 |
+
request = youtube.commentThreads().list(part = ['id','snippet'],maxResults = 100,videoId = video_id)
|
| 13 |
+
response = request.execute()
|
| 14 |
+
except:
|
| 15 |
+
print("An exception occurred")
|
| 16 |
+
return pd.DataFrame()
|
| 17 |
+
comments_df = pd.json_normalize(response['items'],sep='_')
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
comments_df=comments_df[['snippet_topLevelComment_snippet_textDisplay',
|
| 21 |
+
'snippet_topLevelComment_snippet_textOriginal',
|
| 22 |
+
'snippet_topLevelComment_snippet_viewerRating',
|
| 23 |
+
'snippet_topLevelComment_snippet_likeCount',
|
| 24 |
+
'snippet_topLevelComment_snippet_publishedAt',
|
| 25 |
+
'snippet_topLevelComment_snippet_updatedAt',
|
| 26 |
+
'snippet_totalReplyCount']]
|
| 27 |
+
except:
|
| 28 |
+
print("An exception occurred Key error")
|
| 29 |
+
return pd.DataFrame()
|
| 30 |
+
return comments_df
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
#os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
|
| 34 |
+
api_service_name = "youtube"
|
| 35 |
+
api_version = "v3"
|
| 36 |
+
|
| 37 |
+
def find_video_id(youtube_video_id):
|
| 38 |
+
if 'v=' in youtube_video_id:
|
| 39 |
+
video_filter = youtube_video_id.split('v=')[1]
|
| 40 |
+
video_filter = str(video_filter.split('&')[0])
|
| 41 |
+
return video_filter
|
| 42 |
+
return youtube_video_id
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
with st.form("my_form"):
|
| 46 |
+
st.write('Enter Youtube API key( Will not be stored )')
|
| 47 |
+
youtube_api_key = st.text_input('',label_visibility= "hidden")
|
| 48 |
+
|
| 49 |
+
st.write('Enter Youtube Video ID/ Video Link')
|
| 50 |
+
youtube_video_id = st.text_input(' ', label_visibility= "hidden")
|
| 51 |
+
|
| 52 |
+
submitted = st.form_submit_button("Submit")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
if submitted:
|
| 57 |
+
youtube_video_id = find_video_id(youtube_video_id)
|
| 58 |
+
df = extract_comments_from_video(youtube_video_id,youtube_api_key)
|
| 59 |
+
if len(df) > 0:
|
| 60 |
+
st.dataframe(df,height=101)
|
| 61 |
+
else:
|
| 62 |
+
st.info('This video comments are not found', icon="ℹ️")
|