Skip to content

Commit

Permalink
Support Seiga thumbnail downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAplin committed Apr 20, 2024
1 parent 4f2a5fc commit b58f6f7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ nndownload allows you to download videos, images, manga, and process other links
- Running multiple download sessions on the same connection may lead to temporary blocks or throttling.
- These functions are not currently supported:
- Downloading Niconama timeshifts
- Downloading Seiga thumbnails and comments
- Downloading Seiga comments
- Downloading channel blog comments

## Features
Expand Down
2 changes: 1 addition & 1 deletion README_JA.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ nndownload では、 [Niconico](http://nicovideo.jp)(通称ニコニコ動画)
- 同じ接続で複数のダウンロードセッションを実行すると、一時的なブロックやスロットリングが発生する可能性があります。
- 次の機能は現在サポートされていません。
- ニコ生タイムシフトのダウンロード
- ニコニコ静画のサムネイルとコメントのダウンロード
- ニコニコ静画コメントのダウンロード
- チャンネルやブログ、コメントのダウンロード

## Features
Expand Down
19 changes: 14 additions & 5 deletions nndownload/nndownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
SEIGA_USER_ILLUST_URL = "https://seiga.nicovideo.jp/user/illust/{0}?page={1}"
SEIGA_USER_MANGA_URL = "https://seiga.nicovideo.jp/manga/list?user_id={0}&page={1}" # Not all manga are not listed with /user/manga/{0}
SEIGA_IMAGE_URL = "https://seiga.nicovideo.jp/seiga/{0}"
SEIGA_IMAGE_THUMBNAIL_URL = "https://lohas.nicoseiga.jp//thumb/{0}qz" # "cz" can be specified for a consistent 176x176 thumb
SEIGA_MANGA_URL = "https://seiga.nicovideo.jp/comic/{0}"
SEIGA_MANGA_THUMBNAIL_URL = "https://deliver.cdn.nicomanga.jp/thumb/mg_thumb/{0}q" # Not sure where this ID originates, so instead we pull "og:image"
SEIGA_CHAPTER_URL = "https://seiga.nicovideo.jp/watch/{0}"
SEIGA_SOURCE_URL = "https://seiga.nicovideo.jp/image/source/{0}"
SEIGA_CDN_URL = "https://lohas.nicoseiga.jp/"
Expand Down Expand Up @@ -667,6 +669,7 @@ def collect_seiga_image_parameters(session: requests.Session, document: Beautifu
template_params["clip_count"] = int(document.select("li.clip span.count_value")[0].text)
template_params["tags"] = document.select("meta[name=\"keywords\"]")[0]["content"]
template_params["document_url"] = SEIGA_IMAGE_URL.format(template_params["id"])
template_params["thumbnail_url"] = SEIGA_IMAGE_THUMBNAIL_URL.format(template_params["id"])

seiga_source_request = session.get(SEIGA_SOURCE_URL.format(template_params["id"].lstrip("im")))
seiga_source_request.raise_for_status()
Expand Down Expand Up @@ -698,6 +701,7 @@ def collect_seiga_manga_parameters(session, document, template_params):
template_params["view_count"] = int(document.select("#view_count")[0].text)
template_params["uploader"] = document.select("span.author_name")[0].text
template_params["document_url"] = SEIGA_CHAPTER_URL.format(template_params["id"])
template_params["thumbnail_url"] = document.select("meta[property='og\:image']")[0]["content"]

tags = []
tags_request = session.get(SEIGA_MANGA_TAGS_API.format(bare_chapter_id))
Expand Down Expand Up @@ -761,7 +765,8 @@ def download_manga_chapter(session, chapter_id):
metadata_path = os.path.join(chapter_directory, "metadata.json")
dump_metadata(metadata_path, template_params)
if _cmdl_opts.download_thumbnail:
output("Downloading thumbnails for Seiga comics is not currently supported.\n", logging.WARNING)
thumb_filename = os.path.join(chapter_directory, "folder")
download_thumbnail(session, thumb_filename, template_params)
if _cmdl_opts.download_comments:
output("Downloading comments for Seiga comics is not currently supported.\n", logging.WARNING)

Expand Down Expand Up @@ -809,7 +814,7 @@ def download_image(session, image_id):
if _cmdl_opts.dump_metadata:
dump_metadata(filename, template_params)
if _cmdl_opts.download_thumbnail:
output("Downloading thumbnails for Seiga images is not currently supported.\n", logging.WARNING)
download_thumbnail(session, filename, template_params, set_thumb_extension=True)
if _cmdl_opts.download_comments:
output("Downloading comments for Seiga images is not currently supported.\n", logging.WARNING)

Expand Down Expand Up @@ -1853,12 +1858,16 @@ def dump_metadata(filename: AnyStr, template_params: dict):
output("Finished downloading metadata for {0}.\n".format(template_params["id"]), logging.INFO)


def download_thumbnail(session: requests.Session, filename: AnyStr, template_params: dict):
"""Download the video thumbnail."""
def download_thumbnail(session: requests.Session, filename: AnyStr, template_params: dict, set_thumb_extension: bool = False):
"""Download the media thumbnail."""

output("Downloading thumbnail for {0}...\n".format(template_params["id"]), logging.INFO)

filename = replace_extension(filename, "jpg")
# TODO: Probably should check for mimetype
if set_thumb_extension:
filename = replace_extension(filename, "thumb.jpg")
else:
filename = replace_extension(filename, "jpg")

thumb_request = session.get(template_params["thumbnail_url"])
thumb_request.raise_for_status()
Expand Down

0 comments on commit b58f6f7

Please sign in to comment.