-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
134 lines (109 loc) · 4.55 KB
/
Copy pathanalysis.py
File metadata and controls
134 lines (109 loc) · 4.55 KB
1
2
3
4
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'''
script that performs analysis (keyword, sentiment, engagement, top comments) on videos
and throws the results into MongoDB
'''
#%%
import pandas as pd
import os
from glob import glob
import matplotlib.pyplot as plt
import json
from comment_analysis import CommentAnalyzer
from pymongo import MongoClient
from dotenv import load_dotenv
from datetime import datetime
load_dotenv()
MONGO_URI = os.getenv('MONGO_URI')
client = MongoClient(MONGO_URI)
# TODO: generalize
# database of current domain or niche
db = client['yt_comments']
channel_to_state = pd.read_csv('master_channel_to_states.csv')
videos = pd.read_csv('data/master_videos.csv')
def load_comments(video_id, channel_id):
file_path = f'data/{channel_id}/{video_id}/comments.csv'
if os.path.exists(file_path):
df = pd.read_csv(file_path)
df['video_id'] = video_id
return df
return None
all_comments = []
for _, row in videos.iterrows():
comments = load_comments(row['video_id'], row['channel_id'])
if comments is not None:
all_comments.append(comments)
print(len(all_comments))
print('Comments loaded')
comments = pd.concat(all_comments, ignore_index=True)
#%%
videos_with_state = pd.merge(videos, channel_to_state, on='channel_id', how='left')
comments_with_video = pd.merge(comments, videos_with_state[['video_id', 'state', 'video_title','video_url','views']], on='video_id', how='left')
#%%
#we need to convert like_count to int and handle periods, commas, and other non-numeric characters (k, M)
def convert_to_int(value):
if pd.isna(value):
return None
if str(value).strip() == '':
return None
value = str(value)
value = value.replace(',', '').replace('.', '').replace('k', '000').replace('M', '000000')
return int(value)
comments_with_video['like_count'] = comments_with_video['like_count'].apply(convert_to_int)
#%%
top_comments_by_state = comments_with_video.sort_values('like_count', ascending=False).groupby('state').first()
print(top_comments_by_state[['0', 'like_count']])
avg_likes_by_state = comments_with_video.groupby('state')['like_count'].mean().sort_values(ascending=False)
print("\nAverage likes per comment by state:")
print(avg_likes_by_state)
comment_count_by_state = comments_with_video['state'].value_counts()
print("\nComment count by state:")
print(comment_count_by_state)
# %%
#most liked comment in each state
most_liked_comment_by_state = comments_with_video.sort_values('like_count', ascending=False).groupby('state').first()
print(most_liked_comment_by_state[['0', 'like_count']])
# %%
comments_with_video.to_csv('data/comments_with_video.csv', index=False)
for _, row in comments_with_video.iterrows():
comment_data = row.to_dict()
db.comments_with_video.update_one(
{'comment_id': comment_data['comment_id']},
{'$set': comment_data},
upsert=True
)
# TODO: generalize
analyzer = CommentAnalyzer(comments_with_video)
keyword_analysis = analyzer.analyze_keywords_by_state()
sentiment_analysis = pd.DataFrame(list(analyzer.get_sentiment_by_state()))
engagement_metrics = analyzer.get_engagement_metrics()
top_comments_by_state = analyzer.get_top_comments_by_state()
top_comments_by_video = analyzer.get_top_comments_by_video()
state_summary = {}
for state in comments_with_video['state'].unique():
if pd.isna(state):
continue
state_summary[state] = {
'keywords': keyword_analysis.get(state, {}),
'sentiment': sentiment_analysis[sentiment_analysis['state'] == state].to_dict('records')[0],
'engagement': engagement_metrics.loc[state].to_dict(),
'top_comments': [comment for comment in top_comments_by_state if comment['state'] == state]
}
video_summary = {}
for video_id in comments_with_video['video_id'].unique():
video_summary[video_id] = {
'top_comments': [comment for comment in top_comments_by_video if comment['video_id'] == video_id]
}
db.state_analysis.delete_many({})
db.state_analysis.insert_many([{'state': k, **v} for k, v in state_summary.items()])
db.video_analysis.delete_many({})
db.video_analysis.insert_many([{'video_id': k, **v} for k, v in video_summary.items()])
# %%
# visualize top comments by state
for state in comments_with_video['state'].unique():
top_comments = [comment for comment in top_comments_by_state if comment['state'] == state]
print(f"Top comments for {state}:")
for comment in top_comments:
print(f" - {comment['0']} (Likes: {comment['like_count']})")
#print current time in human readable format
print(f"Current time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
# %%