-
Notifications
You must be signed in to change notification settings - Fork 0
/
xkcd_viewer.py
244 lines (191 loc) · 7.64 KB
/
xkcd_viewer.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
import string
from PyQt6.QtWidgets import (
QMainWindow,
QLabel,
QPushButton,
QVBoxLayout,
QWidget,
QApplication,
QSizePolicy,
QLayout,
)
from PyQt6.QtCore import QThread, pyqtSignal, QObject, Qt, QTimer, QTime
from PyQt6.QtGui import QPixmap, QResizeEvent
import sys
from urllib import request
import requests
from bs4 import BeautifulSoup
URL = "https://c.xkcd.com/random/comic/"
# Timeframes have to be in order from earliest to latest
timeframes = [QTime(9, 0, 0, 0), QTime(12, 0, 0, 0), QTime(15, 0, 0, 0)]
class DownloadThread(QThread):
def __init__(self):
super(DownloadThread, self).__init__()
def startThread(self):
self.worker = DownloadWorker()
self.worker.moveToThread(self)
# Connects
self.started.connect(self.worker.downloadPicRaw)
self.worker.downloadDone.connect(window.updateImage)
self.worker.scrapingUrl.connect(
lambda: window.button.setText("Scraping xkcd.com for comic URL...")
)
self.worker.downloadingPic.connect(
lambda: window.button.setText("Downloading comic...")
)
# Cleanup
window.receivedPic.connect(self.quit) # Shutdown thread
window.receivedPic.connect(
self.worker.deleteLater
) # Schedule worker for deletion
self.finished.connect(self.deleteLater) # Schedule thread itself for deletion
self.finished.connect(lambda: window.button.setText("Click for new comic now!"))
self.start() # Start the thread
class DownloadWorker(QObject):
scrapingUrl = pyqtSignal()
downloadingPic = pyqtSignal()
downloadDone = pyqtSignal(object, object, object)
def downloadPicRaw(self):
self.scrapingUrl.emit()
webpage_html = requests.get(URL)
webpage_soup = BeautifulSoup(webpage_html.content, "html.parser")
comic_html = webpage_soup.find(id="comic")
main_title = webpage_soup.find(id="ctitle").string
if comic_html.img["title"] is not None:
sub_title = str(comic_html.img["title"])
if comic_html.img.has_attr("srcset"):
comic_url = comic_html.img["srcset"]
comic_url = comic_url[: len(comic_url) - 3] # Remove "2x" at end of string
else:
comic_url = comic_html.img["src"]
comic_url = "http:" + comic_url
print(comic_url)
self.downloadingPic.emit()
file_tuple = request.urlretrieve(comic_url)
filepath = file_tuple[0]
self.downloadDone.emit(filepath, main_title, sub_title)
class MainWindow(QMainWindow):
receivedPic = pyqtSignal()
def __init__(self):
super(MainWindow, self).__init__()
self.pic_pixmap = None
self.main_title_string = None
self.sub_title_string = None
self.old_timeframe_idx = 0
self.print_string = ""
self.setWindowTitle("xkcd viewer (github: rubenstar/xkcd_viewer)")
# Set up button widget
self.button = QPushButton("Click for new comic now!")
self.button.setCheckable(True)
self.button.clicked.connect(self.downloadThreadStart)
# Set up center image widget
self.pic = QLabel(self)
self.pic.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.pic.setMinimumSize(1, 1)
# Set up text widgets
self.main_title_text = QLabel(self)
self.main_title_text.setFixedHeight(40)
self.main_title_text.setAlignment(
Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignBottom
)
self.sub_title_text = QLabel(self)
self.sub_title_text.setWordWrap(True)
self.sub_title_text.setMaximumHeight(40)
self.sub_title_text.setAlignment(
Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignTop
)
self.time_text = QLabel(self)
self.time_text.setFixedHeight(30)
self.time_text.setAlignment(
Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignBottom
)
# Start a timer
self.timer = QTimer()
self.timer.start(10000) # Generate signal every 10s
self.timer.timeout.connect(self.checkTime)
self.time = QTime()
# Set up layouting
self.layout = QVBoxLayout()
self.layout.addWidget(self.main_title_text)
self.layout.addWidget(self.pic)
self.layout.addWidget(self.sub_title_text)
self.layout.addWidget(self.time_text)
self.layout.addWidget(self.button)
self.widget = QWidget()
self.widget.setLayout(self.layout)
self.setCentralWidget(self.widget)
def checkTime(self):
no_of_timeframes = len(timeframes)
self.current_time = self.time.currentTime()
# Initial value. If current time is smaller then the first timeframe, its the same as bigger than the last
self.current_timeframe_idx = len(timeframes) - 1
for i in range(no_of_timeframes):
if self.current_time > timeframes[i]:
self.current_timeframe_idx = i
if self.current_timeframe_idx != self.old_timeframe_idx:
self.downloadThreadStart()
self.old_timeframe_idx = self.current_timeframe_idx
self.next_timeframe_idx = (self.current_timeframe_idx + 1) % no_of_timeframes
seconds_to_next = self.current_time.secsTo(timeframes[self.next_timeframe_idx])
if seconds_to_next < 0:
seconds_to_next = seconds_to_next + 86400
self.hours_to_next = seconds_to_next // 3600
self.minutes_to_next = ((seconds_to_next % 3600) // 60) + 1
next_time = timeframes[self.next_timeframe_idx].toString("hh:mm")
print_string = (
"Next comic at "
+ str(next_time)
+ ". (That's in "
+ str(self.hours_to_next)
+ " hour(s) and "
+ str(self.minutes_to_next)
+ " minute(s).)"
)
self.time_text.setText(print_string)
def loadImage(self):
if self.pic_pixmap is not None:
self.pic.setPixmap(
self.pic_pixmap.scaled(
self.pic.width(),
self.pic.height(),
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation,
)
)
def loadTitle(self):
if self.main_title_string is not None:
self.main_title_text.setText(
"<h1><strong>" + self.main_title_string + "</strong></h1>"
)
else:
self.main_title_text.setText(
"<h1><strong> No main title found :( </strong></h1>"
)
if self.sub_title_string is not None:
self.sub_title_text.setText(
"<h3><strong>" + self.sub_title_string + "<h3><strong>"
)
else:
self.sub_title_text.setText("<h6><strong> No subtitle found :(<h6><strong>")
def updateImage(self, filepath, main_title, sub_title):
self.pic_pixmap = QPixmap(filepath)
self.main_title_string = main_title
self.sub_title_string = sub_title
self.receivedPic.emit() # Signal back to thread/worker that we're done
self.button.setEnabled(True)
self.button.setChecked(False)
self.loadImage()
self.loadTitle()
def downloadThreadStart(self):
self.button.setEnabled(False)
self.downloadThread = DownloadThread()
self.downloadThread.startThread()
# Overwrite resizeEvent
def resizeEvent(self, a0: QResizeEvent):
self.loadImage()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
window.checkTime()
app.exec()