aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/poringa.py
blob: da17eaef35a6c847d2695f08097cd86bbab4d2b5 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*-

# 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 http://www.poringa.net/"""

from .common import Extractor, Message
from .. import text, exception
from ..cache import cache
import itertools

BASE_PATTERN = r"(?:https?://)?(?:www\.)?poringa\.net"


class PoringaExtractor(Extractor):
    category = "poringa"
    directory_fmt = ("{category}", "{user}", "{post_id}")
    filename_fmt = "{post_id}_{title}_{num:>03}_{filename}.{extension}"
    archive_fmt = "{post_id}_{num}"
    root = "http://www.poringa.net"

    def __init__(self, match):
        Extractor.__init__(self, match)
        self.item = match[1]
        self.__cookies = True

    def items(self):
        for post_id in self.posts():
            url = f"{self.root}/posts/imagenes/{post_id}"

            try:
                response = self.request(url)
            except exception.HttpError as exc:
                self.log.warning(
                    "Unable to fetch posts for '%s' (%s)", post_id, exc)
                continue

            if "/registro-login?" in response.url:
                self.log.warning("Private post '%s'", post_id)
                continue

            page = response.text
            title, pos = text.extract(
                page, 'property="og:title" content="', '"')

            try:
                pos = page.index('<div class="main-info', pos)
                user, pos = text.extract(
                    page, 'href="http://www.poringa.net/', '"', pos)
            except ValueError:
                user = None

            if not user:
                user = "poringa"

            data = {
                "post_id"      : post_id,
                "title"        : text.unescape(title),
                "user"         : text.unquote(user),
                "_http_headers": {"Referer": url},
            }

            main_post = text.extr(
                page, 'property="dc:content" role="main">', '</div>')
            urls = list(text.extract_iter(
                main_post, '<img class="imagen" border="0" src="', '"'))
            data["count"] = len(urls)

            yield Message.Directory, data
            for data["num"], url in enumerate(urls, 1):
                yield Message.Url, url, text.nameext_from_url(url, data)

    def posts(self):
        return ()

    def request(self, url, **kwargs):
        if self.__cookies:
            self.__cookies = False
            self.cookies_update(_cookie_cache())

        for _ in range(5):
            response = Extractor.request(self, url, **kwargs)
            if response.cookies:
                _cookie_cache.update("", response.cookies)
            if response.content.find(
                    b"<title>Please wait a few moments</title>", 0, 600) < 0:
                return response
            self.sleep(5.0, "check")

    def _pagination(self, url, params):
        for params["p"] in itertools.count(1):
            page = self.request(url, params=params).text

            posts_ids = PoringaPostExtractor.pattern.findall(page)
            posts_ids = list(dict.fromkeys(posts_ids))
            yield from posts_ids

            if len(posts_ids) < 19:
                return


class PoringaPostExtractor(PoringaExtractor):
    """Extractor for posts on poringa.net"""
    subcategory = "post"
    pattern = BASE_PATTERN + r"/posts/imagenes/(\d+)"
    example = "http://www.poringa.net/posts/imagenes/12345/TITLE.html"

    def posts(self):
        return (self.item,)


class PoringaUserExtractor(PoringaExtractor):
    subcategory = "user"
    pattern = BASE_PATTERN + r"/(\w+)$"
    example = "http://www.poringa.net/USER"

    def posts(self):
        url = self.root + "/buscar/"
        params = {"q": self.item}
        return self._pagination(url, params)


class PoringaSearchExtractor(PoringaExtractor):
    subcategory = "search"
    pattern = BASE_PATTERN + r"/buscar/\?&?q=([^&#]+)"
    example = "http://www.poringa.net/buscar/?q=QUERY"

    def posts(self):
        url = self.root + "/buscar/"
        params = {"q": self.item}
        return self._pagination(url, params)


@cache()
def _cookie_cache():
    return ()