Skip to content

Commit

Permalink
✨ ascii2d 搜索结果增加一个字段 url
Browse files Browse the repository at this point in the history
  • Loading branch information
NekoAria committed Jun 19, 2022
1 parent a478679 commit 4656666
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
8 changes: 1 addition & 7 deletions PicImageSearch/ascii2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ def __init__(self, bovw: bool = False, **request_kwargs: Any):
super().__init__(**request_kwargs)
self.bovw: bool = bovw

@staticmethod
def _slice(resp: str) -> Ascii2DResponse:
utf8_parser = HTMLParser(encoding="utf-8")
d = PyQuery(fromstring(resp, parser=utf8_parser))("div.row.item-box")
return Ascii2DResponse(d)

async def search(
self, url: Optional[str] = None, file: Optional[BinaryIO] = None
) -> Ascii2DResponse:
Expand Down Expand Up @@ -63,4 +57,4 @@ async def search(
if self.bovw:
resp = await self.get(str(resp.url).replace("/color/", "/bovw/"))

return self._slice(resp.text)
return Ascii2DResponse(resp)
13 changes: 10 additions & 3 deletions PicImageSearch/model/ascii2d.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Dict, List

from httpx import Response
from lxml.html import HTMLParser, fromstring
from pyquery import PyQuery


Expand Down Expand Up @@ -48,7 +50,12 @@ def _get_info(data: PyQuery) -> Dict[str, str]:


class Ascii2DResponse:
def __init__(self, data: PyQuery):
self.origin: PyQuery = data # 原始数据
def __init__(self, resp: Response):
self.origin: str = resp.text # 原始数据
utf8_parser = HTMLParser(encoding="utf-8")
data = PyQuery(fromstring(self.origin, parser=utf8_parser))
# 结果返回值
self.raw: List[Ascii2DItem] = [Ascii2DItem(i) for i in data.items()]
self.raw: List[Ascii2DItem] = [
Ascii2DItem(i) for i in data.find("div.row.item-box").items()
]
self.url: str = str(resp.url)
1 change: 1 addition & 0 deletions demo/demo_ascii2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_sync() -> None:

def show_result(resp: Ascii2DResponse) -> None:
# logger.info(resp.origin) # 原始数据
logger.info(resp.url) # 搜索结果链接
logger.info(resp.raw[1].origin)
logger.info(resp.raw[1].thumbnail)
logger.info(resp.raw[1].title)
Expand Down

0 comments on commit 4656666

Please sign in to comment.