-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
341 lines (286 loc) · 13.5 KB
/
main.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import sys
import os
import json
import feedparser
import webbrowser
import re
import ctypes
from PySide6.QtCore import (Qt,
Signal,
QTimer,
QThread)
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import (QApplication,
QWidget,
QLabel,
QScrollArea,
QMainWindow,
QPushButton,
QMessageBox,
QHBoxLayout,
QFrame,
QVBoxLayout)
from concurrent.futures import (ThreadPoolExecutor,
as_completed)
from datetime import datetime
from ctypes import wintypes
# Constants for taskbar flashing
FLASHW_TRAY = 0x2
FLASHW_TIMERNOFG = 0xC
class FLASHWINFO(ctypes.Structure):
_fields_ = [("cbSize", wintypes.UINT),
("hwnd", wintypes.HWND),
("dwFlags", wintypes.DWORD),
("uCount", wintypes.UINT),
("dwTimeout", wintypes.DWORD)]
# Load user32.dll for the FlashWindowEx function
user32 = ctypes.windll.user32
def flash_taskbar_icon(window_handle):
"""Flashes the taskbar icon when there are unread entries."""
fInfo = FLASHWINFO(
cbSize=ctypes.sizeof(FLASHWINFO),
hwnd=window_handle,
dwFlags=FLASHW_TRAY | FLASHW_TIMERNOFG,
uCount=3, # Number of flashes
dwTimeout=0
)
user32.FlashWindowEx(ctypes.byref(fInfo))
def extract_title(full_text):
"""Extract the title text from an HTML link within the given text."""
match = re.search(r"<a[^>]*>(.*?)</a>", full_text)
return match.group(1) if match else None
class FeedFetcher(QThread):
finished = Signal(list) # Signal to emit the fetched entries once the thread finishes
def __init__(self, feeds):
super().__init__()
self.feeds = feeds
def run(self):
entries = []
with ThreadPoolExecutor() as executor:
futures = {executor.submit(self.fetch_feed, url): name for name, url in self.feeds.items()}
for future in as_completed(futures):
feed_name = futures[future]
try:
feed = future.result()
for entry in feed.entries:
entries.append((feed_name, entry))
except Exception as e:
print(f"Failed to fetch {feed_name}: {e}")
entries.sort(key=lambda x: x[1].published_parsed, reverse=True)
self.finished.emit(entries) # Emit the signal with fetched entries
def fetch_feed(self, url):
return feedparser.parse(url)
def get_running_path(relative_path):
if '_internal' in os.listdir():
return os.path.join('_internal', relative_path)
else:
return relative_path
def boostrap_rss_feeds():
if not os.path.isfile('rss_feeds.json'):
with open('rss_feeds.json', 'w') as file_out_handle:
file_out_handle.write(json.dumps({"New On Steam": "https://store.steampowered.com/feeds/newreleases.xml"},
indent=2))
class RSSReader(QMainWindow):
new_entries_signal = Signal(list) # Signal to pass the fetched entries to the main thread
def __init__(self):
boostrap_rss_feeds()
super().__init__()
self.setWindowTitle("RSS Feed Reader V"+open(get_running_path('version.txt'), 'r').read())
self.resize(1200, 600)
self.setWindowIcon(QIcon(get_running_path('icon.ico')))
# Set the file path to store viewed entries
self.viewed_entries_file = "viewed_entries.json"
self.viewed_entries = self.load_viewed_entries() # Load viewed entries from file
# Load feeds from JSON
self.rss_feeds_file = 'rss_feeds.json'
self.feeds = self.load_rss_feeds()
# Setup main layout
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.main_layout = QVBoxLayout(self.central_widget)
# Add labels for total/ unread entries and last refresh datetime at the top (outside scroll area)
self.last_refresh_label = QLabel("Last refresh done at: TBA")
self.total_label = QLabel("Total Entries: 0")
self.unread_label = QLabel("Unread Entries: 0")
self.last_refresh_label.setAlignment(Qt.AlignCenter)
self.total_label.setAlignment(Qt.AlignCenter)
self.unread_label.setAlignment(Qt.AlignCenter)
# Prepare the horizontal are for total_label and unread_label
label_layout = QHBoxLayout()
label_layout.addWidget(self.total_label)
label_layout.addWidget(self.unread_label)
# Add last_refresh_label and label_layout to the main layout
self.main_layout.addWidget(self.last_refresh_label)
self.main_layout.addLayout(label_layout)
# Button layout for Refresh and Mark All as Read buttons
button_layout = QHBoxLayout()
# Add "Mark All as Read" button
self.mark_all_button = QPushButton("Mark All as Read")
self.mark_all_button.clicked.connect(self.mark_all_as_read)
button_layout.addWidget(self.mark_all_button)
# Add "Refresh Feeds" button
self.refresh_button = QPushButton("Refresh Feeds")
self.refresh_button.clicked.connect(self.refresh_feeds)
button_layout.addWidget(self.refresh_button)
# Add button layout to the main layout
self.main_layout.addLayout(button_layout)
# Scroll area for feed display
self.scroll_area = QScrollArea()
self.scroll_area.setWidgetResizable(True)
self.main_layout.addWidget(self.scroll_area)
# Container widget within scroll area
self.feed_container = QWidget()
self.feed_layout = QVBoxLayout(self.feed_container)
self.feed_layout.setSpacing(50)
self.scroll_area.setWidget(self.feed_container)
# Timer to simulate refresh the RSS feed entries
self.timer1 = QTimer(self)
self.timer1.timeout.connect(self.refresh_feeds)
self.timer1.start(30*60*1000) # Update every 30 minutes
# Timer to flash the taskbar icon if there are unread entries
self.unread_entries = 0
self.timer2 = QTimer(self)
self.timer2.timeout.connect(self.update_unread_entries)
self.timer2.start(5000) # Update every 5 seconds
# Connect the signal to a method that will update the GUI
self.new_entries_signal.connect(self.update_feed_entries)
# Display the feeds
self.refresh_feeds()
def update_unread_entries(self):
if self.unread_entries > 0:
flash_taskbar_icon(self.winId()) # Flash the taskbar icon if unread entries exist
def load_rss_feeds(self):
"""Load RSS feed URLs from a JSON file."""
try:
with open(self.rss_feeds_file, "r") as file:
return json.load(file)
except FileNotFoundError:
QMessageBox.warning(self, "Warning", f"File {self.rss_feeds_file} not found.")
return {}
except json.JSONDecodeError:
QMessageBox.critical(self, "Error", f"Error reading {self.rss_feeds_file}.")
return {}
def load_viewed_entries(self):
"""Load viewed entries from a JSON file."""
try:
with open(self.viewed_entries_file, "r") as file:
return set(json.load(file))
except (FileNotFoundError, json.JSONDecodeError):
return set()
def save_viewed_entries(self):
"""Save viewed entries to a JSON file."""
with open(self.viewed_entries_file, "w") as file:
json.dump(list(self.viewed_entries), file, indent=2)
def fetch_feed(self, url):
"""Fetch and parse a single RSS feed."""
return feedparser.parse(url)
def update_feed_entries(self, entries):
"""Update the feed entries in the GUI."""
# Clear existing feed entries
for i in reversed(range(self.feed_layout.count())):
widget = self.feed_layout.itemAt(i).widget()
if widget is not None:
widget.deleteLater()
# Count unread entries and update the labels
total_entries = len(entries)
self.unread_entries = sum(1 for _, entry in entries if entry.get("title") not in self.viewed_entries)
self.last_refresh_label.setText(f"Last refresh done at: {datetime.now().strftime("%d-%b-%Y %H:%M:%S")}")
self.total_label.setText(f"Total Entries: {total_entries}")
self.unread_label.setText(f"Unread Entries: {self.unread_entries}")
# Display each entry in the GUI
for feed_name, entry in entries:
border_color = "grey" if entry.title in self.viewed_entries else "blue"
self.entry_container = QFrame()
self.entry_container.setStyleSheet(f"""
QFrame {{
background-color: #f5f5f5;
border: 2px solid {border_color};
border-radius: 10px;
padding: 10px;
}}
""")
self.container_layout = QVBoxLayout(self.entry_container)
self.container_layout.setSpacing(1)
entry_widget = QLabel(f"<b>{feed_name}</b>: <a href='{entry.link}'>{entry.title}</a>")
entry_widget.setTextFormat(Qt.RichText)
entry_widget.setTextInteractionFlags(Qt.TextBrowserInteraction)
entry_widget.setOpenExternalLinks(False)
entry_widget.linkActivated.connect(
lambda _,
title=entry.title,
container=self.entry_container,
link=entry.link:
self.on_entry_click(title, container, link))
self.preview_text = entry.get("summary") or entry.get("description") or "No preview available."
self.preview_widget = QLabel(self.preview_text[:200] + '<br>...')
self.preview_widget.setWordWrap(True)
self.preview_widget.setTextFormat(Qt.RichText)
self.preview_widget.setTextInteractionFlags(Qt.TextBrowserInteraction)
self.preview_widget.setOpenExternalLinks(False)
self.preview_widget.setStyleSheet("font-size: 10pt; color: gray;")
self.entry_date = datetime(*entry.published_parsed[:6]).strftime("%Y-%m-%d %H:%M:%S")
self.date_widget = QLabel(f"Date: {self.entry_date}")
self.date_widget.setStyleSheet("font-size: 9pt; color: #666;")
self.container_layout.addWidget(entry_widget)
self.container_layout.addWidget(self.date_widget)
self.container_layout.addWidget(self.preview_widget)
self.feed_layout.addWidget(self.entry_container)
# Re-enable buttons after feed is refreshed
self.mark_all_button.setDisabled(False)
self.refresh_button.setDisabled(False)
def refresh_feeds(self):
"""Refresh and display all feeds, sorted by date."""
self.mark_all_button.setDisabled(True)
self.refresh_button.setDisabled(True)
# Start the FeedFetcher thread
self.fetcher_thread = FeedFetcher(self.feeds)
self.fetcher_thread.finished.connect(self.new_entries_signal.emit) # Connect the signal
self.fetcher_thread.start()
def on_entry_click(self, title, entry_container, entry_link):
"""Handle link click to mark the entry as read and update border color."""
webbrowser.open(entry_link)
if title not in self.viewed_entries:
self.viewed_entries.add(title)
# Change the border color of the clicked entry container to grey
entry_container.setStyleSheet("""
QFrame {
background-color: #f5f5f5;
border: 2px solid grey;
border-radius: 10px;
padding: 10px;
}
""")
# Save viewed entries after marking this entry as read
self.save_viewed_entries()
self.unread_entries -= 1
self.unread_label.setText(f"Unread Entries: {self.unread_entries}")
def mark_all_as_read(self):
"""Mark all displayed entries as read and update unread count."""
for i in range(self.feed_layout.count()):
frame = self.feed_layout.itemAt(i).widget()
if isinstance(frame, QFrame):
# Access the layout of each frame to find the title label
container_layout = frame.layout()
title_label = container_layout.itemAt(0).widget()
if isinstance(title_label, QLabel):
# Extract the title from the label's HTML content
title_text = extract_title(title_label.text())
self.viewed_entries.add(title_text)
# Change the border color of the frame to grey
frame.setStyleSheet("""
QFrame {
background-color: #f5f5f5;
border: 2px solid grey;
border-radius: 10px;
padding: 10px;
}
""")
# Save viewed entries and update unread count
self.save_viewed_entries()
self.unread_entries = 0
self.unread_label.setText(f"Unread Entries: {self.unread_entries}")
if __name__ == "__main__":
app = QApplication(sys.argv)
reader = RSSReader()
reader.show()
sys.exit(app.exec())