aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/nsfwalbum.py
blob: 0b84f9ccec0df54e16b7d72f09145eb09d0ef07f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-

# Copyright 2019-2025 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

"""Extractors for https://nsfwalbum.com/"""

from .common import GalleryExtractor
from .. import text


class NsfwalbumAlbumExtractor(GalleryExtractor):
    """Extractor for image albums on nsfwalbum.com"""
    category = "nsfwalbum"
    subcategory = "album"
    root = "https://nsfwalbum.com"
    filename_fmt = "{album_id}_{num:>03}_{id}.{extension}"
    directory_fmt = ("{category}", "{album_id} {title}")
    archive_fmt = "{id}"
    referer = False
    pattern = r"(?:https?://)?(?:www\.)?nsfwalbum\.com(/album/(\d+))"
    example = "https://nsfwalbum.com/album/12345"

    def __init__(self, match):
        self.album_id = match[2]
        GalleryExtractor.__init__(self, match)

    def metadata(self, page):
        extr = text.extract_from(page)
        return {
            "album_id": text.parse_int(self.album_id),
            "title"   : text.unescape(extr('<h6>', '</h6>')),
            "models"  : text.split_html(extr('"models"> Models:', '</div>')),
            "studio"  : text.remove_html(extr('"models"> Studio:', '</div>')),
        }

    def images(self, page):
        iframe = self.root + "/iframe_image.php?id="
        backend = self.root + "/backend.php"
        retries = self._retries

        for image_id in text.extract_iter(page, 'data-img-id="', '"'):
            spirit = None
            tries = 0

            while tries <= retries:
                try:
                    if not spirit:
                        spirit = self._annihilate(text.extract(
                            self.request(iframe + image_id).text,
                            'giraffe.annihilate("', '"')[0])
                        params = {"spirit": spirit, "photo": image_id}
                    data = self.request_json(backend, params=params)
                    break
                except Exception:
                    tries += 1
            else:
                self.log.warning("Unable to fetch image %s", image_id)
                continue

            yield data[0], {
                "id"    : text.parse_int(image_id),
                "width" : text.parse_int(data[1]),
                "height": text.parse_int(data[2]),
                "_http_validate": self._validate_response,
                "_fallback": (f"{self.root}/imageProxy.php"
                              f"?photoId={image_id}&spirit={spirit}",),
            }

    def _validate_response(self, response):
        return not response.url.endswith(
            ("/no_image.jpg", "/placeholder.png", "/error.jpg"))

    def _annihilate(self, value, base=6):
        return "".join(
            chr(ord(char) ^ base)
            for char in value
        )