forked from StefanoDPCarraro/YTLiveComments
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstats.py
More file actions
50 lines (35 loc) · 1.43 KB
/
stats.py
File metadata and controls
50 lines (35 loc) · 1.43 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
import json
from matplotlib import pyplot as plt
from matplotlib import dates as mdates
import pandas as pd
from collections import Counter
def get_author_comments(author, comments_json, interval=30):
with open(comments_json, 'r') as file:
data = json.load(file)
author_comments = []
for item in data:
if item['author'] == author:
author_comments.append(item)
author_filtered_comments = [comment for comment in author_comments if comment['message'].strip()]
df = pd.DataFrame(author_filtered_comments)
df['timestamp'] = pd.to_datetime(df['time_elapsed'])
df.set_index('timestamp', inplace=True)
resampled_df = df.resample(f'{interval}T').size()
plt.figure(figsize=(14, 6))
plt.plot(resampled_df.index, resampled_df.values)
plt.xlabel('Time')
plt.ylabel('Comments')
plt.title(f'Comments each {interval} minutes')
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
plt.grid(True)
path = f'output/comentarios_{author}_por_minuto.png'
plt.savefig(path)
return path, author_filtered_comments
def get_top_authors(comments_json, n=5):
with open(comments_json, 'r') as file:
data = json.load(file)
filter_data = [item for item in data if item['message'].strip()]
authors = [item['author'] for item in filter_data]
author_freq = Counter(authors)
top_authors = author_freq.most_common(n)
return top_authors