import sys
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from NaverAPI import *
from urllib.request import urlopen
import webbrowser
class qtApp(QWidget):
def __init__(self):
super().__init__()
uic.loadUi('./studyPyQt/NaverAPIMovie.ui',self)
self.setWindowIcon(QIcon('./studyPyQt/movie.png'))
self.btnSearch.clicked.connect(self.btnSearchClicked)
self.txtSearch.returnPressed.connect(self.txtSearchReturned)
self.tblresult.doubleClicked.connect(self.tblresultDoubleClicked)
def tblresultDoubleClicked(self):
selected = self.tblresult.currentRow()
url = self.tblresult.item(selected,5).text()
webbrowser.open(url)
def txtSearchReturned(self):
self.btnSearchClicked()
def btnSearchClicked(self):
search = self.txtSearch.text()
if search == '':
QMessageBox.warning(self,'경고','영화명을 입력하세요.')
return
else:
api = NaverAPI()
node = 'movie'
display = 100
result = api.get_naver_search(node,search,1,display)
items = result['items']
self.makeTable(items)
def makeTable(self,items) -> None:
self.tblresult.setSelectionMode(QAbstractItemView.SingleSelection)
self.tblresult.setColumnCount(7)
self.tblresult.setRowCount(len(items))
self.tblresult.setHorizontalHeaderLabels(['영화제목','개봉년도','감독','배우진','평점','링크','포스터'])
self.tblresult.setColumnWidth(0,150)
self.tblresult.setColumnWidth(1,70)
self.tblresult.setColumnWidth(3,130)
self.tblresult.setColumnWidth(4,45)
self.tblresult.setEditTriggers(QAbstractItemView.NoEditTriggers)
for i, post in enumerate(items):
title = self.replaceHtmlTag(post['title'])
subtitle = post['subtitle']
title = f'{title}\n ({subtitle})'
pubDate = post['pubDate']
director = post['director'].replace('|',',')[:-1]
actor = post['actor'].replace('|',',')[:-1]
userRating = post['userRating']
link = post['link']
img_url = post['image']
if img_url != '':
data = urlopen(img_url).read()
image = QImage()
image.loadFromData(data)
imgLabel = QLabel()
imgLabel.setPixmap(QPixmap(image))
self.tblresult.setItem(i,0,QTableWidgetItem(title))
self.tblresult.setItem(i,1,QTableWidgetItem(pubDate))
self.tblresult.setItem(i,2,QTableWidgetItem(director))
self.tblresult.setItem(i,3,QTableWidgetItem(actor))
self.tblresult.setItem(i,4,QTableWidgetItem(userRating))
self.tblresult.setItem(i,5,QTableWidgetItem(link))
if img_url != '':
self.tblresult.setCellWidget(i,6,imgLabel)
self.tblresult.setRowHeight(i,110)
else:
self.tblresult.setItem(i,6,QTableWidgetItem('No Poster!'))
def replaceHtmlTag(self,sentence) -> str:
result = sentence.replace('<','<').replace('>','>').replace('<b>','').replace('</b>','').replace(''',"'").replace('"','"')
return result
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = qtApp()
ex.show()
sys.exit(app.exec_())
from urllib.request import Request, urlopen
from urllib.parse import quote
import datetime
import json
class NaverAPI:
def __init__(self) -> None:
print(f'[{datetime.datetime.now()}] Naver API 생성')
def get_request_url(self,url):
req = Request(url)
req.add_header('X-Naver-Client-Id','VAWxYc3u4xYAh_Tw4roC')
req.add_header('X-Naver-Client-Secret','GVdU6AhnmY')
try:
res = urlopen(req)
if res.getcode() == 200:
print(f'[{datetime.datetime.now()}] Naver API 요청 성공')
return res.read().decode('utf-8')
else:
print(f'[{datetime.datetime.now()}] Naver API 요청 실패')
return None
except Exception as e:
print(f'[{datetime.datetime.now()}] 예외발생 {e}')
return None
def get_naver_search(self,node,search,start,display):
base_url = 'https://openapi.naver.com/v1/search'
node_url = f'/{node}.json'
params = f'?query={quote(search)}&start={start}&display={display}'
url = base_url + node_url + params
retData = self.get_request_url(url)
if retData == None:
return None
else:
return json.loads(retData)