diff --git a/main.py b/main.py index 1735dcd..ee11758 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,11 @@ -from source import get_image_link +from source import XHS def example(): test_cookie = "abRequestId=25c57ab7-8cbf-5383-b020-08852c1704e1; webBuild=3.4.1; xsecappid=xhs-pc-web; a1=18a033d274338lwsfacj9x5bpf4fznhhc8xrakemj50000250510; webId=93c0636350d85103d93bca88da2959cd; websectiga=2a3d3ea002e7d92b5c9743590ebd24010cf3710ff3af8029153751e41a6af4a3; sec_poison_id=ae1f0190-4d0c-45f5-a75f-68c2d87c5573; web_session=030037a3ed570060b3a43845a6234a2f100ef4; gid=yY08qqfYy0SSyY08qqfJWYTd4qqY1EMi0SVjC6VC2DUi4F28iuIxx0888J282y880JfWY0Di; cache_feeds=[]" demo = "https://www.xiaohongshu.com/explore/64a3a5170000000031008914" - print(get_image_link(demo, cookie=test_cookie)) + xhs = XHS() + print(xhs.get_image(demo, cookie=test_cookie)) if __name__ == '__main__': diff --git a/source/Html.py b/source/Html.py index e2aa558..09036ad 100644 --- a/source/Html.py +++ b/source/Html.py @@ -10,8 +10,7 @@ def get_html( proxies=None, timeout=10, **kwargs): - if cookie: - update_cookie(cookie) + update_cookie(cookie) response = requests.get( url, params=params, @@ -23,4 +22,6 @@ def get_html( def update_cookie(cookie: str): + if not cookie: + return HEADERS["Cookie"] = cookie diff --git a/source/Image.py b/source/Image.py index 9b113da..24207c4 100644 --- a/source/Image.py +++ b/source/Image.py @@ -1,15 +1,14 @@ -# from .Params import HEADERS from .Params import ID from .Params import IMAGE_API -def get_id(html: str): +def get_id(html: str) -> list: return ID.findall(html) -def generate_url(ids: list): +def generate_url(ids: list) -> list: return [IMAGE_API + i for i in ids] -def download(url, path): - pass +def get_url(html: str) -> list: + return generate_url(get_id(html)) diff --git a/source/Params.py b/source/Params.py index d918242..1cf9ffd 100644 --- a/source/Params.py +++ b/source/Params.py @@ -1,3 +1,4 @@ +from pathlib import Path from re import compile HEADERS = { @@ -10,3 +11,8 @@ HEADERS = { } IMAGE_API = "https://sns-img-qc.xhscdn.com/" ID = compile(r'"traceId":"(.*?)"') + + +class Params: + def __init__(self, path: str): + self.path = Path(path) diff --git a/source/__init__.py b/source/__init__.py index c0cdff9..901fb0c 100644 --- a/source/__init__.py +++ b/source/__init__.py @@ -1,21 +1,22 @@ from .Html import get_html -from .Image import generate_url -from .Image import get_id +from .Image import get_url +from .Params import Params -def get_image_link( - url: str, - cookie=None, - params=None, - proxies=None, - timeout=10, - **kwargs): - html = get_html( - url, - cookie=cookie, - params=params, - proxies=proxies, - timeout=timeout, - **kwargs) - ids = get_id(html) - return generate_url(ids) +class XHS: + def __init__(self, path="./"): + self.params = Params(path) + self.image = Image(self.params) + + def get_image(self, url: str, cookie=None, ): + return self.image.get_image_link(url, cookie) + + +class Image: + def __init__(self, params): + self.params = params + + @staticmethod + def get_image_link(url: str, cookie=None, ): + html = get_html(url, cookie=cookie, ) + return get_url(html)