Skip to content
Merged

Dev #481

Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions assets/docs/sources/tutorial/0_common_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ download_album(123, option)
option.download_album(123)
```

## 获取本子/章节/图片的实体类,下载图片
## 获取本子/章节/图片的实体类,下载图片/封面图

```python
from jmcomic import *
Expand All @@ -49,23 +49,26 @@ client = JmOption.default().new_jm_client()
# 本子实体类
album: JmAlbumDetail = client.get_album_detail('427413')

# 下载本子封面图,保存为 cover.png (图片后缀可指定为jpg、webp等)
client.download_album_cover('427413', './cover.png')


def fetch(photo: JmPhotoDetail):
# 章节实体类
photo = client.get_photo_detail(photo.photo_id, False)
print(f'章节id: {photo.photo_id}')

# 图片实体类
image: JmImageDetail
for image in photo:
print(f'图片url: {image.img_url}')

# 下载单个图片
client.download_by_image_detail(image, './a.jpg')
# 如果是已知未混淆的图片,也可以直接使用url来下载
random_image_domain = JmModuleConfig.DOMAIN_IMAGE_LIST[0]
client.download_image(f'https://{random_image_domain}/media/albums/416130.jpg', './a.jpg')


# 多线程发起请求
multi_thread_launcher(
Expand Down
3 changes: 2 additions & 1 deletion src/jmcomic/jm_client_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(self,
postman: Postman,
domain_list: List[str],
retry_times=0,
domain_retry_strategy=None,
):
"""
创建JM客户端
Expand All @@ -26,7 +27,7 @@ def __init__(self,
super().__init__(postman)
self.retry_times = retry_times
self.domain_list = domain_list
self.domain_retry_strategy = None
self.domain_retry_strategy = domain_retry_strategy
self.CLIENT_CACHE = None
self._username = None # help for favorite_folder method
self.enable_cache()
Expand Down
8 changes: 8 additions & 0 deletions src/jmcomic/jm_client_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ def img_is_not_need_to_decode(cls, data_original: str, _resp) -> bool:
# https://cdn-msp2.18comic.vip/media/photos/498976/00027.gif
return data_original.endswith('.gif')

def download_album_cover(self, album_id, save_path: str):
self.download_image(
img_url=JmcomicText.get_album_cover_url(album_id),
img_save_path=save_path,
scramble_id=None,
decode_image=False,
)


class JmSearchAlbumClient:
"""
Expand Down
19 changes: 19 additions & 0 deletions src/jmcomic/jm_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,25 @@ def limit_text(cls, text: str, limit: int) -> str:
length = len(text)
return text if length <= limit else (text[:limit] + f'...({length - limit}')

@classmethod
def get_album_cover_url(cls,
album_id: str,
image_domain: str = None,
size: str = ''
) -> str:
"""
根据本子id生成封面url

:param album_id 本子id
:param image_domain: 图片cdn域名
:param size: 尺寸后缀,例如搜索列表页会使用 size="_3x4" 的封面图
"""
if image_domain is None:
import random
image_domain = random.choice(JmModuleConfig.DOMAIN_IMAGE_LIST)

return f'{JmModuleConfig.PROT}{image_domain}/media/albums/{cls.parse_to_jm_id(album_id)}{size}.jpg'

Comment thread
hect0x7 marked this conversation as resolved.

# 支持dsl: #{???} -> os.getenv(???)
JmcomicText.dsl_replacer.add_dsl_and_replacer(r'\$\{(.*?)\}', JmcomicText.match_os_env)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_jmcomic/test_jm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,7 @@ def print_page(page):
# 打印page内容
for aid, atitle in page:
print(aid, atitle)

def test_download_cover(self):
album_id = 123
self.client.download_album_cover(album_id, f'./{album_id}.jpg')
Loading