Spaces:
Runtime error
Runtime error
Increase volume of comments
Browse files
app.py
CHANGED
|
@@ -4,29 +4,44 @@ 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 |
-
|
| 13 |
-
|
|
|
|
| 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 |
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
import re
|
| 6 |
|
| 7 |
+
pd.set_option("display.max_colwidth", -1)
|
| 8 |
+
def extract_all_comments(video_id,page_token='',comments_list =[]):
|
| 9 |
+
|
| 10 |
+
request = youtube.commentThreads().list(part = ['id','snippet'],
|
| 11 |
+
maxResults = 100,videoId = video_id ,pageToken= page_token)
|
| 12 |
+
response = request.execute()
|
| 13 |
+
for comment_details in response['items']:
|
| 14 |
+
text_dsiplay = comment_details.get('snippet').get('topLevelComment').get('snippet').get('textDisplay')
|
| 15 |
+
text_original = comment_details.get('snippet').get('topLevelComment').get('snippet').get('textOriginal')
|
| 16 |
+
likes = comment_details.get('snippet').get('topLevelComment').get('snippet').get('likeCount')
|
| 17 |
+
published_at = comment_details.get('snippet').get('topLevelComment').get('snippet').get('publishedAt')
|
| 18 |
+
updated_at = comment_details.get('snippet').get('topLevelComment').get('snippet').get('updatedAt')
|
| 19 |
+
reply_count = comment_details.get('snippet').get('totalReplyCount')
|
| 20 |
+
comments_list.append({'text_dsiplay':text_dsiplay,'text_original':text_original,
|
| 21 |
+
'likes':likes,'published_at':published_at,'updated_at':updated_at,
|
| 22 |
+
'reply_count':reply_count})
|
| 23 |
+
|
| 24 |
+
if 'nextPageToken' in response.keys():
|
| 25 |
+
if len(comments_list) < 500:
|
| 26 |
+
|
| 27 |
+
extract_all_comments(video_id = video_id,page_token= response['nextPageToken'],comments_list= comments_list)
|
| 28 |
+
else:
|
| 29 |
+
print("Limiting results for speed up")
|
| 30 |
+
|
| 31 |
+
return comments_list
|
| 32 |
+
|
| 33 |
def extract_comments_from_video(video_id,youtube_api_key):
|
| 34 |
try:
|
| 35 |
youtube = googleapiclient.discovery.build(
|
| 36 |
api_service_name, api_version, developerKey = youtube_api_key)
|
| 37 |
+
|
| 38 |
+
found_comments = extract_all_comments(video_id,page_token='')
|
| 39 |
+
|
| 40 |
+
comments_df = pd.DataFrame(found_comments)
|
| 41 |
except:
|
| 42 |
print("An exception occurred")
|
| 43 |
return pd.DataFrame()
|
|
|
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
return comments_df
|
| 46 |
|
| 47 |
|