-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyqt-rss.py
executable file
·265 lines (229 loc) · 8.1 KB
/
pyqt-rss.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
#!/usr/bin/python
import sys
import time
import sqlite3
import yaml
import feedparser
import webbrowser
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QAction,
QTableWidget, QTableWidgetItem,QHeaderView, QAbstractItemView, QLabel,
QPushButton,QFormLayout,QHBoxLayout,QVBoxLayout,QScrollArea,QGroupBox,
QLineEdit)
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QTimer
def loadconfig():
with open('sources.yaml', 'r') as file:
config = yaml.safe_load(file)
return config
def saveconfig(config):
with open('sources.yaml', 'w') as file:
yaml.dump(config, file, allow_unicode=True)
# update database & create if not exist
def updatedb():
# connect to database & create if needed
con = sqlite3.connect(config['db_file'])
cur = con.cursor()
# Create table if not exist
try:
cur.execute("SELECT * FROM articles")
except sqlite3.OperationalError:
cur.execute("""CREATE TABLE articles(
source TEXT,
source_title TEXT,
title TEXT,
url TEXT UNIQUE,
date INTEGER,
read INTEGER)""")
for source in config['sources']:
feed = feedparser.parse(config['sources'][source]['url'])
for entry in feed.entries:
# Insert data if not exist, ignore if not unique (unique columns created above)
cur.execute("INSERT OR IGNORE INTO articles VALUES (?, ?, ?, ?, ?, ?)",(
source,
feed.feed.title,
entry.title,
entry.link,
time.mktime(entry.published_parsed),
# entry.author,
# entry.summary,
0))
# commit changes, must be after every entry
con.commit()
# close database, otherwise you lost all changes
con.close()
# fetch database data for table
def getdbdata():
con = sqlite3.connect(config['db_file'])
cur = con.cursor()
cur.execute("SELECT source_title, title, url, date, read FROM articles ORDER BY read,date DESC")
rows = cur.fetchall()
con.close()
return rows
def updatentry(url):
con = sqlite3.connect(config['db_file'])
cur = con.cursor()
# comma in (url,) very important
cur.execute("UPDATE articles SET read=1 WHERE url=?", (url,))
con.commit()
con.close()
class TableView(QTableWidget):
def __init__(self):
super().__init__()
rows = getdbdata()
self.setRowCount(len(rows))
self.setColumnCount(3)
# hide row headers
self.verticalHeader().hide()
# column titles
self.setHorizontalHeaderLabels(['Źródło', 'Data', 'Tytuł'])
# self.setData(rows)
# first two column resize to content, last fill rest of window
self.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents)
self.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeToContents)
self.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
# self.itemClicked.connect(self.clicked)
self.cellClicked.connect(self.clicked)
# when clicked select row instead cell
self.setSelectionBehavior(QAbstractItemView.SelectRows)
for i, row in enumerate(rows):
src = QTableWidgetItem(row[0])
dat = QTableWidgetItem(time.strftime('%Y-%m-%d %H:%M', time.localtime(row[3])))
tit = QTableWidgetItem(row[1])
# bold if article unread
font = QFont()
font.setBold(True)
if row[4] == 0:
tit.setFont(font)
elem = [src,dat,tit]
for x in range(len(elem)):
self.setItem(i, x, elem[x])
# hide url in tooltip
self.item(i, 2).setToolTip(row[2])
# click event, open article url in web browser
def clicked(self, qmodelindex):
item = self.currentItem()
# only when clicked third column - article title
if self.currentColumn() == 2:
# open url in web browser & update database entry
webbrowser.open(item.toolTip())
updatentry(item.toolTip())
# unbold font in table for item
font = QFont()
font.setBold(False)
item.setFont(font)
class Settings(QWidget):
def __init__(self, new_config):
super().__init__()
self.setFixedSize(300, 400)
self.setWindowTitle("PyQt RSS – Ustawienia")
self.new_config = new_config
font = QFont()
font.setBold(True)
self.mygroupbox = QGroupBox()
self.layout = QVBoxLayout()
# layout for ok/cancel buttons
self.layout_down = QHBoxLayout()
self.layout_main = QVBoxLayout()
# add sources
self.nameLine = QLineEdit()
self.urlLine = QLineEdit()
self.addSourceButton = QPushButton('Dodaj')
self.addSourceButton.clicked.connect(lambda: self.addSource(self.nameLine.text(), self.urlLine.text()))
self.addSourceLabel = QLabel('Dodaj źródło:')
self.addSourceLabel.setFont(font)
self.addSourceForm = QFormLayout()
self.addSourceForm.addRow('Nazwa:',self.nameLine)
self.addSourceForm.addRow('Url:',self.urlLine)
self.addSourceForm.addRow(self.addSourceButton)
self.layout_main.addWidget(self.addSourceLabel)
self.layout_main.addLayout(self.addSourceForm)
# remove sources
self.removeLabel = QLabel('Usuń wybrane źródła:')
self.removeLabel.setFont(font)
self.layout_main.addWidget(self.removeLabel)
# add buttons for all sources from config
for source in self.new_config['sources']:
self.addRemoveButton(source)
self.mygroupbox.setLayout(self.layout)
self.scroll = QScrollArea()
self.scroll.setWidget(self.mygroupbox)
self.scroll.setWidgetResizable(True)
self.layout_main.addWidget(self.scroll)
# ok/cancel button, disabled
self.confirmBtn = QPushButton("OK")
self.confirmBtn.clicked.connect(lambda: self.confirmSettings())
self.cancel = QPushButton("Anuluj")
self.cancel.clicked.connect(self.close)
self.layout_down.addWidget(self.cancel)
self.layout_down.addWidget(self.confirmBtn)
self.layout_main.addLayout(self.layout_down)
self.setLayout(self.layout_main)
def confirmSettings(self):
global config
# remove unknown sources from DB
con = sqlite3.connect(self.new_config['db_file'])
cur = con.cursor()
k = list(self.new_config['sources'].keys())
q = ', '.join('?' * len(k))
sqlstr = "DELETE FROM articles WHERE source NOT IN (" + q + ")"
cur.execute(sqlstr, k)
con.commit()
con.close()
# assign new config to variable & save to file
config = self.new_config
saveconfig(self.new_config)
self.close()
def addRemoveButton(self, source):
x = 'Usuń ' + source
self.removeSourceBtn = QPushButton(x)
self.removeSourceBtn.setObjectName(source)
self.removeSourceBtn.setToolTip(self.new_config['sources'][source]['url'])
self.removeSourceBtn.clicked.connect(lambda: self.removeSource())
self.layout.addWidget(self.removeSourceBtn)
def addSource(self, source, url):
newkey = {source: {'url': url}}
self.new_config['sources'].update(newkey)
self.addRemoveButton(source)
def removeSource(self):
x = self.sender().objectName()
self.new_config['sources'].pop(x)
self.sender().setHidden(True)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('PyQt RSS')
self.setMinimumSize(650, 800)
self.menu()
self.setCentralWidget(TableView())
self.timer = QTimer()
self.timer.timeout.connect(lambda: self.refresh())
self.timer.setInterval(3600000)
self.timer.start()
def refresh(self):
updatedb()
self.setCentralWidget(TableView())
def menu(self):
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('Menu')
refreshButton = QAction('Odśwież', self)
refreshButton.setShortcut('Ctrl+R')
refreshButton.triggered.connect(lambda: self.refresh())
fileMenu.addAction(refreshButton)
settingButton = QAction('Ustawienia', self)
settingButton.setShortcut('Ctrl+O')
settingButton.triggered.connect(lambda: self.settingShow())
fileMenu.addAction(settingButton)
exitButton = QAction('Zamknij', self)
exitButton.setShortcut('Ctrl+Q')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)
def settingShow(self):
self.settings = Settings(loadconfig())
self.settings.show()
if __name__ == "__main__":
config = loadconfig()
updatedb()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()