네이버 API 영화검색 앱
네이버 뉴스검색 앱의 NaverApi.py는 그대로 사용하고 기존 NaverSearchApp.py를 수정하여 NaverMovieApp.py로 변경하여 사용
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import *
from NaverApi import *
import webbrowser
from PyQt5.QtGui import *
from urllib.request import urlopen
class qtApp(QWidget):
def __init__(self):
super().__init__()
uic.loadUi('./PyQt_Practice/NaverApiMovie.ui',self)
self.setWindowIcon(QIcon('./PyQt_practice/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.getNaverSearch(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(4,50)
self.tblResult.setEditTriggers(QAbstractItemView.NoEditTriggers)
for i,post in enumerate(items):
title=self.replaceHtmlTab(post['title'])
subtilte=post['subtitle']
title=f'{title} ({subtilte})'
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 replaceHtmlTab(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_())