Skip to content

Commit 7b6cbe9

Browse files
committed
v0.17
- 프로그램 안전성 개선 - 댓글 표시 기능 추가 - Thread lock 안정화
1 parent 4093d17 commit 7b6cbe9

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

main.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def article_parse(self, dc_id, keyword, search_type, page=1, search_pos=''):
8181
global all_link, g_type
8282
try:
8383
url = f"https://gall.dcinside.com/{g_type}/lists/?id={dc_id}&page={page}&search_pos={search_pos}&s_type={search_type}&s_keyword={keyword}"
84-
print(url);
84+
#print(url);
8585
res = requests.get(url, headers=headers)
8686
soup = BeautifulSoup(res.text, "lxml")
8787

@@ -104,6 +104,9 @@ def article_parse(self, dc_id, keyword, search_type, page=1, search_pos=''):
104104
else:
105105
reply = "0"
106106

107+
if '/' in reply:
108+
reply = reply.split('/')[0]
109+
107110
nickname = element.select(".ub-writer")[0].text.strip()
108111
timestamp = element.select(".gall_date")[0].text
109112
refresh = element.select(".gall_count")[0].text
@@ -116,14 +119,12 @@ def article_parse(self, dc_id, keyword, search_type, page=1, search_pos=''):
116119
'refresh': refresh, 'recommend': recommend}
117120
self.QTableWidgetUpdate.emit(article_data) # 글 데이터 방출
118121

119-
120122
except Exception as e:
121123
# print(e)
122124
self.ThreadMessageEvent.emit('글을 가져오는 중 오류가 발생했습니다.')
123125

124126
def run(self):
125127
self.mutex.lock()
126-
127128
global running
128129

129130
search_pos = ''
@@ -133,23 +134,20 @@ def run(self):
133134
search_type = search_dict[self.parent.comboBox.currentText()]
134135

135136
idx = 0
136-
while (True):
137-
if running == False:
138-
return
139-
137+
while running:
140138
if idx > loop_count or search_pos == 'last':
141139
self.QLabelWidgetUpdate.emit('상태 : 검색 완료')
142140
self.ThreadMessageEvent.emit('작업이 완료되었습니다.')
143141
running = False
144142
break
145143

146144
page = self.parent.page_explorer(id, keyword, search_pos)
147-
print(page)
145+
#print(page)
148146

149147
if not page['start'] == 0: # 글이 있으면
150148

151149
for i in range(page['start'], page['end'] + 1):
152-
if running == False:
150+
if not running:
153151
return
154152

155153
self.QLabelWidgetUpdate.emit(f'상태 : {idx}/{loop_count} 탐색중...')
@@ -160,20 +158,24 @@ def run(self):
160158
if idx > loop_count or search_pos == 'last':
161159
break
162160

163-
time.sleep(0.1) # 디시 서버를 위한 딜레이
161+
# time.sleep(0.1) # 디시 서버를 위한 딜레이
162+
self.msleep(100) # ※주의 QThread에서 제공하는 sleep을 사용
164163

165164
self.QLabelWidgetUpdate.emit(f'상태 : {idx}/{loop_count} 탐색중...')
166165
idx += 1 # 글을 못찾고 넘어가도 + 1
167166

168167
search_pos = page['next_pos']
169-
170168
self.mutex.unlock()
169+
running = False
171170

172171
def stop(self):
172+
global running
173+
running = False
173174
self.working = False
174-
self.mutex.unlock()
175+
175176
self.quit()
176-
self.wait(5000) # 5000ms = 5s
177+
self.msleep(5000) #wait로 하면 안되고 msleep으로 해야 제대로 mutex lock이 작동하는듯 하다.
178+
#self.wait(5000) # 5000ms = 5s
177179

178180

179181
class SlotEvent:
@@ -183,6 +185,10 @@ def ThreadMessageEvent(self, n):
183185

184186
@pyqtSlot(dict)
185187
def QTableWidgetUpdate(self, data):
188+
#업데이트를 너무 자주하면 GUI에 제대로 반영이 안되는 것이 있음.
189+
#IO 성능을 포기하더라도 delay보단 print로 출력해서 주는게 더 낫다는 생각..
190+
print(data)
191+
186192
rowPosition = self.articleView.rowCount()
187193
self.articleView.insertRow(rowPosition)
188194

@@ -352,8 +358,8 @@ def page_explorer(self, dc_id, keyword, search_pos=''):
352358
def search(self): # 글검색
353359
global all_link, g_type, running
354360

355-
if running == True: # 이미 실행중이면
356-
dialog = QMessageBox.question(self, 'Message', '검색이 진행중입니다. 새로 검색을 시작하시겠습니까?',
361+
if running: # 이미 실행중이면
362+
dialog = QMessageBox.question(self, 'Message', '검색이 진행중입니다. 새로 검색을 시작하시겠습니까? (이전 검색을 중단하기 위해 프로그램이 잠시 멈출 수 있습니다.)',
357363
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
358364
if dialog == QMessageBox.Yes:
359365
running = False

0 commit comments

Comments
 (0)