From 38e94b71846658b7b5f31e34614f69f59ec7c9c5 Mon Sep 17 00:00:00 2001 From: EX3 <100339835+EX3exp@users.noreply.github.com> Date: Sun, 11 Jun 2023 14:30:10 +0900 Subject: [PATCH] Update main.py Add Update checker Add Error Messages --- main.py | 133 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 103 insertions(+), 30 deletions(-) diff --git a/main.py b/main.py index a8e10b1..3725fd2 100644 --- a/main.py +++ b/main.py @@ -1,40 +1,64 @@ - -import requests -import sys +from requests import get, HTTPError, RequestException from bs4 import BeautifulSoup from datetime import datetime from PyQt5 import uic -from PyQt5.QtWidgets import QApplication, QMainWindow +from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox +from json import loads as jsonloads from PyQt5.QtGui import QIcon from PyQt5.QtCore import QTimer -#import pyperclip +from PyQt5.QtCore import QUrl +from PyQt5.QtGui import QDesktopServices +#from pyperclip import copy as clipcopy from tkinter import Tk -version = '1.0.0' +version = '1.1.0' def make_APA_citation(url: str): try: - response = requests.get(url) - status_code = response.status_code - if response.status_code == 200: - html = response.text - soup = BeautifulSoup(html, 'html.parser') - title_ = soup.title.string.strip() - - date_now = datetime.now() - citation = f"{title_}[์›น์‚ฌ์ดํŠธ].({date_now.year}.{date_now.month}.{date_now.day}). URL: {url}" - return citation, True - else : - citation = f'[๐Ÿซ Error: {status_code}] ์ž…๋ ฅ๋œ url์— ๋ฌธ์ œ๊ฐ€ ์žˆ์–ด์š”.' - return citation, False + response = get(url) + response.raise_for_status() + html = response.text + soup = BeautifulSoup(html, 'html.parser') + title_ = soup.title.string.strip() + + date_now = datetime.now() + citation = f"{title_}[์›น์‚ฌ์ดํŠธ].({date_now.year}.{date_now.month}.{date_now.day}). URL: {url}" + return citation, True + except HTTPError as e: + status_code = e.response.status_code + error_message = "ํ•ด๋‹น URL์— ์ ‘์†ํ•˜๋Š” ๊ณผ์ •์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์–ด์š”." + if status_code == 404: + error_message = "์กด์žฌํ•˜์ง€ ์•Š๋Š” URL์ด์—์š”." + elif status_code == 500: + error_message = "์›น ์„œ๋ฒ„์— ๋ฌธ์ œ๊ฐ€ ์žˆ์–ด์š”." + elif status_code == 403: + error_message = "์ด URL์— ๋Œ€ํ•œ ์ ‘๊ทผ์ด ๋ง‰ํ˜€ ์žˆ์–ด์š”." + elif status_code == 400: + error_message = "์ ‘๊ทผ ์š”์ฒญ ๋ณด๋‚ด๊ธฐ์— ์‹คํŒจํ–ˆ์–ด์š”." + elif status_code == 401: + error_message = "์ ‘๊ทผ ๊ถŒํ•œ์ด ์—†์–ด์š”." + elif status_code == 410: + error_message = "์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅํ•œ URL์ด์—์š”." + elif status_code == 414: + error_message = "URL์ด ๋„ˆ๋ฌด ๊ธธ์–ด์„œ ์„œ๋ฒ„๊ฐ€ ๊ฑฐ๋ถ€ํ–ˆ์–ด์š”." + elif status_code == 502: + error_message = "๊ฒŒ์ดํŠธ์›จ์ด ์ƒํƒœ๊ฐ€ ๋‚˜๋น ์š”. ์„œ๋ฒ„์— ๊ณผ๋ถ€ํ•˜๊ฐ€ ๊ฑธ๋ ธ์„ ์ˆ˜ ์žˆ์–ด์š”." + elif status_code == 503: + error_message = "์„œ๋ฒ„๊ฐ€ ๋ฉˆ์ท„์–ด์š”. ์„œ๋ฒ„๊ฐ€ ํ„ฐ์กŒ๊ฑฐ๋‚˜, ์ž ์‹œ ์ ๊ฒ€์ค‘์ธ ๊ฒƒ ๊ฐ™์•„์š”." + + citation = f'[๐Ÿซ HTTPError {e.response.status_code}: {error_message}]' + return citation, False + except RequestException as e: + citation = f"[๐Ÿซ RequestException: {type(e).__name__}]" + return citation, False except Exception as e: - citation = f"[๐Ÿซ Error: {type(e).__name__}]" + citation = f"[๐Ÿซ {type(e).__name__}: {str(e)}]" return citation, False # def make_APA_citation_with_author(url: str): # '''์งœ๋‹ค ๋งŒ ์ฝ”๋“œ''' # try: -# response = requests.get(url) +# response = get(url) # status_code = response.status_code # if response.status_code == 200: # html = response.text @@ -83,6 +107,7 @@ def initUi(self): self.buttonCopy.clicked.connect(self.copy_to_clipboard) self.labelCopyCompleted.hide() self.pBarGo.hide() + self.actionUpdateCheck.triggered.connect(lambda: self.check_update(True)) def update_line_count(self): text = self.textInput.toPlainText() @@ -124,13 +149,11 @@ def make_citation(self): self.buttonCopy.setDisabled(False) self.pBarGo.hide() + # def copy_to_clipboard(self): - # pyperclip.copy(self.toCopy) + # clipcopy(self.toCopy) # self.labelCopyCompleted.show() # QTimer.singleShot(2100, self.labelCopyCompleted.hide) - - - def copy_to_clipboard(self): r = Tk() r.withdraw() @@ -139,10 +162,62 @@ def copy_to_clipboard(self): r.update() r.destroy() self.labelCopyCompleted.show() - QTimer.singleShot(2100, self.labelCopyCompleted.hide) + QTimer.singleShot(2100, self.labelCopyCompleted.hide) + + def check_update(self, version_check: bool): + owner = "EX3exp" + repo = "APA-Website-Citation-Generator" + + api_url = f"https://api.github.com/repos/{owner}/{repo}/releases/latest" + response = get(api_url) + + if response.status_code == 200: + response_text = response.text + release_info = jsonloads(response_text) + + latest_version = release_info["tag_name"][1:] + + if latest_version != version: + download_link = f"https://github.com/EX3exp/APA-Website-Citation-Generator/releases/download/v{latest_version}/APAGenerator{latest_version}.zip" + msg_box = QMessageBox() + msg_box.setIcon(QMessageBox.Information) + msg_box.setWindowIcon(icon) + msg_box.setWindowTitle(f"v{version} โ†’ v{latest_version}") + msg_box.setText(f"๐Ÿค”์ถœ์ฒ˜์ƒ์„ฑ๊ธฐ๊ฐ€ v{latest_version}์œผ๋กœ ์—…๋ฐ์ดํŠธ๋˜์—ˆ์–ด์š”!") + msg_box.setInformativeText("๋ฐ”๋กœ ๋‹ค์šด๋กœ๋“œ ๋งํฌ๋กœ ์ด๋™ํ• ๊นŒ์š”?") + msg_box.addButton("โœ”๏ธ๋‹ค์šด๋ฐ›์œผ๋Ÿฌ ๊ฐ€๊ธฐ", QMessageBox.AcceptRole) + msg_box.addButton("โŒ๊ทธ๋ƒฅ ์ด๋Œ€๋กœ ์“ธ๋ž˜์š”", QMessageBox.RejectRole) + + result = msg_box.exec_() + + if result == QMessageBox.AcceptRole: + QDesktopServices.openUrl(QUrl(download_link)) + elif version_check: + msg_box = QMessageBox() + msg_box.setIcon(QMessageBox.Information) + msg_box.setWindowIcon(icon) + msg_box.setWindowTitle(f"v{version}") + msg_box.setText(f"๐Ÿ˜Ž์ถœ์ฒ˜์ƒ์„ฑ๊ธฐ๊ฐ€ ํ˜„์žฌ ์ตœ์‹  ๋ฒ„์ „์ด์—์š”.") + msg_box.addButton("โœ”๏ธ์•Œ์•˜์–ด์š”", QMessageBox.RejectRole) + + result = msg_box.exec_() + else: + pass + elif version_check: + msg_box = QMessageBox() + msg_box.setIcon(QMessageBox.Information) + msg_box.setWindowIcon(icon) + msg_box.setWindowTitle(f"v{version}") + msg_box.setText(f"๐Ÿซ ์˜ค, ์ด๋Ÿฐ. ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ด ์—…๋ฐ์ดํŠธ ์ฒดํ‚น์— ์‹คํŒจํ–ˆ์–ด์š”.") + msg_box.addButton("๐Ÿซ ์•Œ์•˜์–ด์š”", QMessageBox.RejectRole) + + result = msg_box.exec_() + else: + pass + if __name__ == '__main__': - app = QApplication(sys.argv) + app = QApplication([]) app.setStyle('Fusion') window = APAGen() @@ -150,8 +225,6 @@ def copy_to_clipboard(self): window.setWindowIcon(icon) window.setWindowTitle(f'์ถœ์ฒ˜์ƒ์„ฑ๊ธฐ - ์›น์‚ฌ์ดํŠธ APA ์ถœ์ฒ˜ ์ƒ์„ฑ๊ธฐ v{version}') window.show() + window.check_update(False) app.exec_() - - -