Пример #1
0
def make_photo(post, fix_photosets, blog_name):
    photo_post = PhotoPost(id=post["id"])

    caption_tag = post.find("photo-caption")
    if caption_tag:
        photo_post.caption = caption_tag.text.strip()

    # Only photosets have photo tags as children
    is_photoset = False if len(post.find_all("photo")) == 0 else True

    photos = []
    # TODO: combine these two cases intelligently maybe possibly perhaps
    if is_photoset:
        # Here, we ignore the base photo-url tags
        photo_tags = post.find_all("photo")

        rows_per_image = None
        if fix_photosets:
            rows_per_image = get_image_rows(post, blog_name)

        for i, photo_tag in enumerate(photo_tags):
            photo_url_tags = photo_tag.find_all("photo-url")

            for img in photo_url_tags:
                if img["max-width"] == "1280":
                    img_url = img.text.strip()
                    caption = photo_tag["caption"] or None
                    this_photo = Photo(post_id=post["id"],
                                       caption=caption,
                                       offset=i + 1,
                                       width=photo_tag["width"],
                                       height=photo_tag["height"],
                                       url=img_url)

                    if fix_photosets and rows_per_image is not None:
                        try:
                            this_photo.row = rows_per_image[i]
                        except IndexError:
                            # print(f"encountered IndexError for index {i} on post {post['id']}")
                            # print(f"rows per image: {rows_per_image}")
                            print(
                                f"unable to obtain the row for image {i + 1} for post {post['id']} - this photoset "
                                f"will not display properly.")
                            rows_per_image = None
                            for photo in photos:
                                photo.row = None
                            this_photo.row = None

                    photos.append(this_photo)
    else:
        photo_url_tags = post.find_all("photo-url")

        for img in photo_url_tags:
            if img["max-width"] == "1280":
                img_url = img.text.strip()
                this_photo = Photo(post_id=post["id"], offset=0, url=img_url)
                photos.append(this_photo)

    photo_post.is_photoset = is_photoset
    photo_post.photos = photos
    return photo_post