upbit/pixivpy

support getting multiple illusts under same pid

Opened this issue · 1 comments

An artwork page for a pid could have multiple illusts under it.

But the following only return the first image url everytime:

json_result = pixiv_api.illust_detail(pid)
illust = json_result.illust
print(illust.image_urls["large"])

One can fix this by checking if json_result.illust contains meta_single_page or meta_pages fields.
If the pid correspond to an one-artwork entity, it would have data in meta_single_page but meta_pages=[].
On the other side, a multi-artwork entity would have meta_pages but no meta_single_page field at all.

here's an example:

def get_all_image_urls(pid: int) -> List[str]:
    json_result = pixiv_api.illust_detail(pid)
    multi = json_result.illust.meta_pages
    if len(multi) == 0:
        single = json_result.illust.meta_single_page
        return [single.original_image_url]
    else:
        urls = [page.image_urls.original for page in multi]
        return urls

pixivpy is simply returning what Pixiv server gave us, so we have nothing to do with the response shape.

My snippet of downloading illustrations is very similar to yours:

if illust.type == "ugoira":
    ...
elif illust.page_count == 1:
    img_urls = [illust.meta_single_page.original_image_url]
else:
    img_urls = [
        page.image_urls.original
        for page in illust.meta_pages
    ]