Skip to content

Commit

Permalink
Update main.py
Browse files Browse the repository at this point in the history
Add Update checker
Add Error Messages
  • Loading branch information
EX3exp authored Jun 11, 2023
1 parent 29a6f3f commit 38e94b7
Showing 1 changed file with 103 additions and 30 deletions.
133 changes: 103 additions & 30 deletions main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -139,19 +162,69 @@ 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()

icon = QIcon('icon.bmp')
window.setWindowIcon(icon)
window.setWindowTitle(f'출처생성기 - 웹사이트 APA 출처 생성기 v{version}')
window.show()
window.check_update(False)
app.exec_()




0 comments on commit 38e94b7

Please sign in to comment.