~savoy/coppergal

7e6e50a7bcccd41cecfa0cf21a55a9ce0cb6ef28 — savoy 1 year, 8 months ago 7aa14cf
added: hierarchy classes have download methods

Signed-off-by: savoy <git@liberation.red>
1 files changed, 28 insertions(+), 0 deletions(-)

M src/coppergal/hierarchy.py
M src/coppergal/hierarchy.py => src/coppergal/hierarchy.py +28 -0
@@ 21,6 21,7 @@
from __future__ import annotations

import datetime as dt
import downloader
import re
import requests



@@ 28,6 29,7 @@ from bs4 import BeautifulSoup, PageElement
from dateutil.parser import parse as dt_parse
from enum import Enum
from location import Url, JoinType, IterUrl
from pathlib import Path
from requests.exceptions import Timeout
from typing import Optional



@@ 105,6 107,11 @@ class Image:
    def __repr__(self) -> str:
        return f"Image(File: {self.file.value}, " f"Date: {self.date})"

    def download(self, client: requests.Session, root: Path) -> None:
        resp, total, modified = downloader.get(client, self)
        filepath = root / self.file.value.replace(self.url.base, "")
        downloader.write(resp, total, modified, filepath)


class Album:
    url: Url


@@ 245,6 252,11 @@ class Album:
        except UnboundLocalError:
            return None, 0

    def download(self, client: requests.Session, root: Path) -> None:
        if self.images:
            for image in self.images:
                image.download(client, root)


class Category:
    url: Url


@@ 321,6 333,13 @@ class Category:

        return categories

    def download(self, client: requests.Session, root: Path) -> None:
        if self.albums:
            for album in self.albums:
                if album.images:
                    for image in album.images:
                        image.download(client, root)


class Site:
    url: Url


@@ 359,3 378,12 @@ class Site:
                    )

        return recent

    def download(self, client: requests.Session, root: Path) -> None:
        if self.categories:
            for category in self.categories:
                if category.albums:
                    for album in category.albums:
                        if album.images:
                            for image in album.images:
                                image.download(client, root)