-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentPage.py
More file actions
199 lines (165 loc) · 7.55 KB
/
CommentPage.py
File metadata and controls
199 lines (165 loc) · 7.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from PySide6.QtCore import Signal, QTimer, QThread
from PySide6.QtWidgets import (
QWidget, QLabel, QVBoxLayout, QScrollArea, QSizePolicy
)
from typing import Optional, List
import re
import json
import os
from Backend.ScrapeComments import CommentFetcher
from Backend.AnalysisWorker import AnalysisWorker
from UI.SplashScreen import SplashScreen
from utils.AppState import app_state
from utils.Logger import logger
from widgets.DownloadableImage import DownloadableImage
def comments_to_sentences(data):
sentences = []
def extract(text: str):
parts = re.split(r"[.!?]\s+|\n+", text)
return [p.strip() for p in parts if p.strip()]
def walk(item):
if isinstance(item, str):
sentences.extend(extract(item))
return
if isinstance(item, dict):
text = item.get("text")
if isinstance(text, str):
sentences.extend(extract(text))
for r in item.get("replies", []):
walk(r)
if isinstance(data, dict):
for comments in data.values():
for c in comments:
walk(c)
elif isinstance(data, list):
for c in data:
walk(c)
return sentences
class Comment(QWidget):
comment_page_scrape_comments_signal = Signal()
def __init__(self, parent: Optional[QWidget] = None):
super().__init__(parent)
self.db = app_state.db
self.comment_fetcher = CommentFetcher()
self.comment_page_scrape_comments_signal.connect(self.scrape_comments)
self.sentiment_image = None
self.wordcloud_image = None
self.layout = QVBoxLayout(self)
self.setLayout(self.layout)
self.scroll_area = QScrollArea()
self.scroll_area.setWidgetResizable(True)
self.layout.addWidget(self.scroll_area)
self.scroll_content = QWidget()
self.scroll_layout = QVBoxLayout(self.scroll_content)
self.scroll_area.setWidget(self.scroll_content)
self.comments: List[str] = []
# Auto-run on load
QTimer.singleShot(0, self.scrape_comments)
def scrape_comments(self):
video_details = app_state.video_list
if not video_details:
logger.warning("CommentPage: No videos in app_state.video_list")
for i in reversed(range(self.scroll_layout.count())):
w = self.scroll_layout.itemAt(i).widget()
if w:
w.deleteLater()
self.scroll_layout.addWidget(QLabel("No comments found."))
return
if isinstance(video_details, list):
video_details = {"default": video_details}
results = self.comment_fetcher.fetch_comments(video_details)
if not results:
logger.error("CommentPage: fetch_comments returned no results")
self.scroll_layout.addWidget(QLabel("No comments found."))
return
all_comment_dicts = []
for channel_id, vids in results.items():
for vid, meta in vids.items():
filepath = meta.get("filepath")
if not filepath or not os.path.exists(filepath):
continue
try:
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, list):
all_comment_dicts.extend(data)
except Exception:
logger.exception("Error reading comments file")
self.comments = comments_to_sentences(all_comment_dicts)
self._generate_and_display_images()
def _generate_and_display_images(self):
# Clear previous
for i in reversed(range(self.scroll_layout.count())):
w = self.scroll_layout.itemAt(i).widget()
if w:
w.deleteLater()
if not self.comments:
self.scroll_layout.addWidget(QLabel("No comments found."))
return
# Sizes
sent_w = 1600
sent_h = int(sent_w * 0.33)
wc_w = 2800
wc_h = int(wc_w * 0.6)
logger.info(f"CommentPage: Queuing analysis sentiment {sent_w}x{sent_h}, wordcloud {wc_w}x{wc_h}")
self.analysis_thread = QThread()
self.analysis_worker = AnalysisWorker(self.comments, sentiment_size=(sent_w, sent_h), wordcloud_size=(wc_w, wc_h), max_words=100)
self.analysis_worker.moveToThread(self.analysis_thread)
# Create splash
parent_win = self.window() if hasattr(self, "window") else None
self.splash = SplashScreen(parent=parent_win)
self.splash.set_title("Analyzing comments...")
self.splash.update_status("Preparing analysis...")
self.splash.set_progress(0)
self.splash.enable_runtime_mode(parent_window=parent_win, cancel_callback=self._cancel_analysis)
self.splash.show_with_animation()
# Wire signals
self.analysis_thread.started.connect(self.analysis_worker.run)
self.analysis_worker.progress_updated.connect(lambda m: (self.splash.update_status(m) if self.splash else None))
self.analysis_worker.progress_percentage.connect(lambda p: (self.splash.set_progress(p) if self.splash else None))
self.analysis_worker.sentiment_ready.connect(self._on_sentiment_ready)
self.analysis_worker.wordcloud_ready.connect(self._on_wordcloud_ready)
self.analysis_worker.finished.connect(self.analysis_thread.quit)
self.analysis_worker.finished.connect(self.analysis_worker.deleteLater)
self.analysis_thread.finished.connect(self.analysis_thread.deleteLater)
# When thread fully finishes, fade splash
self.analysis_thread.finished.connect(lambda: (self.splash.fade_and_close(300) if self.splash else None))
self.analysis_thread.start()
# helper cancel method
def _cancel_analysis(self):
if hasattr(self, "analysis_worker") and self.analysis_worker:
try:
self.analysis_worker.cancel()
except Exception:
pass
# also attempt to stop thread gracefully
if hasattr(self, "analysis_thread") and self.analysis_thread.isRunning():
try:
self.analysis_thread.requestInterruption()
self.analysis_thread.quit()
self.analysis_thread.wait(200)
except Exception:
pass
# ensure UI shows canceled
for i in reversed(range(self.scroll_layout.count())):
w = self.scroll_layout.itemAt(i).widget()
if w:
w.deleteLater()
self.scroll_layout.addWidget(QLabel("Analysis cancelled."))
# slots to receive images
def _on_sentiment_ready(self, qimage):
self.sentiment_image = qimage
# show immediately (title)
channel_name = next(iter(app_state.video_list.keys()), "unknown")
self.scroll_layout.addWidget(QLabel("<b>Sentiment Analysis</b>"))
sent_widget = DownloadableImage(qimage, default_name=f"comment_sentiment_{channel_name}.png")
sent_widget.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.scroll_layout.addWidget(sent_widget)
def _on_wordcloud_ready(self, qimage):
self.wordcloud_image = qimage
self.scroll_layout.addWidget(QLabel("<b>Word Cloud</b>"))
channel_name = next(iter(app_state.video_list.keys()), "unknown")
wc_widget = DownloadableImage(qimage, default_name=f"comment_wordcloud_{channel_name}.png")
wc_widget.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.scroll_layout.addWidget(wc_widget)
self.scroll_layout.addStretch(1)