언어 자료구조 알고리즘/파이썬(Python)

[python] 도서 검색기 feat. 네이버 개발자센터

언제나 휴일 언제나휴일 2020. 11. 9. 14:43
반응형

실행화면

Book.py

Book.py
0.00MB

 

 

#Book.py
class Book:
    def __init__(self,title,author,price,content="",publisher="",link="",imglink=""):
        self.title = title
        self.author = author
        self.price = price
        self.content = content
        self.publisher = publisher
        self.link = link
        self.imglink=imglink


 

NaverBookSearcher.py

NaverBookSearcher.py
0.00MB

 

 

 

#NaverBookSearcher.py
import urllib.request
import json
from Book import Book
class NaverBookSearcher:
    @staticmethod
    def MakeBook(jsbook):
        title = jsbook["title"]
        author = jsbook["author"]
        price = jsbook["price"]
        content = jsbook["description"]
        publisher = jsbook["publisher"]
        link = jsbook["link"]
        imglink=jsbook["image"]
        return Book(title,author,price,content,publisher,link,imglink)
    def __init__(self):
        self.client_id = "네이버 개발자센터에서 발급받은 자신의 Client ID"
        self.client_secret =  "네이버 개발자센터에서 발급받은 자신의 Client Secret"
        self.url = "https://openapi.naver.com/v1/search/book.json"
        self.query=""
        self.query_str=""
    def SetQuery(self,qstr):
        self.query = "query="+urllib.parse.quote(qstr)
        self.query_str = self.url+"?"+self.query
    def Request(self,start,display):
        start_str = "start="+str(start)
        display_str = "display="+str(display)
        fquery_str = self.query_str +"&"+start_str+"&"+display_str
        request = urllib.request.Request(fquery_str)
        request.add_header("X-Naver-Client-Id",self.client_id)
        request.add_header("X-Naver-Client-Secret",self.client_secret)
        try:
            response = urllib.request.urlopen(request)
        except:
            return None
        rescode = response.getcode()
        if rescode != 200:
            return None
        content = response.read()
        data = content.decode('utf-8')
        jdata = json.loads(data)
        total = jdata['total']  
        return jdata['items'],total
    def RequestAll(self):
        redatas=list()
        start = 1
        display=10
        datas,total = self.Request(start,display)
        redatas.extend(datas)
        start = start+display
        while (start<total)and(start<1000):
            datas,total = self.Request(start,display)
            redatas.extend(datas)
            start = start+display
        return redatas

BookSearcherWidget.py

BookSearcherWidget.py
0.00MB

 

 

#BookSearcherWidget.py
from NaverBookSearcher import NaverBookSearcher
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class BookSearcherWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.books = list()
        self.setWindowTitle("도서 검색기(feat.네이버 개발자 센터)")
        self.resize(1000,1000)
        lb_query = QLabel("질의:",self)
        lb_query.move(20,20)
        self.te_query = QTextEdit("",self)
        self.te_query.move(120,15)
        self.te_query.resize(600,70)
        self.btn_search = QPushButton("검색",self)
        self.btn_search.move(740,15)
        lb_blist = QLabel("도서 목록",self)
        lb_blist.move(20,110)
        self.lbox_book = QListWidget(self)
        self.lbox_book.move(20,180)
        self.lbox_book.resize(300,800)
        self.btn_search.clicked.connect(self.SearchBook)
        self.lb_title = QLabel("[제목]",self)
        self.lb_title.move(340,200)
        self.lb_title.resize(650,60)
        self.lb_author = QLabel("[저자]",self)
        self.lb_author.move(340,280)
        self.lb_author.resize(650,60)
        self.lb_price = QLabel("[가격]",self)
        self.lb_price.move(340,360)
        self.lb_price.resize(650,60)
        self.te_content = QTextEdit("[내용]",self)
        self.te_content.setReadOnly(True)
        self.te_content.move(340,440)
        self.te_content.resize(650,550)
        self.lbox_book.currentItemChanged.connect(self.OnSelectChange)
    def OnSelectChange(self):
        index = self.lbox_book.currentRow()
        try:
            book = self.books[index]
        except:
            return
        self.lb_title.setText(book.title)
        self.lb_author.setText(book.author)
        self.lb_price.setText(str(book.price))
        self.te_content.setText(book.content)
    def SearchBook(self):
        self.books.clear()
        self.lbox_book.clear()
        q = self.te_query.toPlainText()
        ns = NaverBookSearcher()
        ns.SetQuery(q)
        books = ns.RequestAll()
        for bookjs in books:
            book = NaverBookSearcher.MakeBook(bookjs)
            self.books.append(book)
            self.lbox_book.addItem(book.title)

Main.py

import sys
from PyQt5.QtWidgets import QApplication
from BookSearcherWidget import BookSearcherWidget
app = QApplication(sys.argv)
bsw = BookSearcherWidget()
bsw.show()
sys.exit(app.exec_())
반응형