| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
"""Extractors for https://tumblrgallery.xyz/""" |
| 8 |
|
| 9 |
from .common import GalleryExtractor |
| 10 |
from .. import text |
| 11 |
|
| 12 |
BASE_PATTERN = r"(?:https?://)?tumblrgallery\.xyz" |
| 13 |
|
| 14 |
|
| 15 |
class TumblrgalleryExtractor(GalleryExtractor): |
| 16 |
"""Base class for tumblrgallery extractors""" |
| 17 |
category = "tumblrgallery" |
| 18 |
filename_fmt = "{category}_{gallery_id}_{num:>03}_{id}.{extension}" |
| 19 |
directory_fmt = ("{category}", "{gallery_id} {title}") |
| 20 |
root = "https://tumblrgallery.xyz" |
| 21 |
referer = False |
| 22 |
|
| 23 |
def _urls_from_page(self, page): |
| 24 |
return text.extract_iter( |
| 25 |
page, '<div class="report"> <a class="xx-co-me" href="', '"') |
| 26 |
|
| 27 |
def _data_from_url(self, url): |
| 28 |
filename = text.nameext_from_url(url)["filename"] |
| 29 |
parts = filename.split("_") |
| 30 |
try: |
| 31 |
return {"id": parts[1] if parts[1] != "inline" else parts[2]} |
| 32 |
except IndexError: |
| 33 |
return {"id": filename} |
| 34 |
|
| 35 |
|
| 36 |
class TumblrgalleryTumblrblogExtractor(TumblrgalleryExtractor): |
| 37 |
"""Extractor for Tumblrblog on tumblrgallery.xyz""" |
| 38 |
subcategory = "tumblrblog" |
| 39 |
pattern = BASE_PATTERN + r"(/tumblrblog/gallery/(\d+)\.html)" |
| 40 |
example = "https://tumblrgallery.xyz/tumblrblog/gallery/12345.html" |
| 41 |
|
| 42 |
def __init__(self, match): |
| 43 |
TumblrgalleryExtractor.__init__(self, match) |
| 44 |
self.gallery_id = text.parse_int(match[2]) |
| 45 |
|
| 46 |
def metadata(self, page): |
| 47 |
return { |
| 48 |
"title" : text.unescape(text.extr(page, "<h1>", "</h1>")), |
| 49 |
"gallery_id": self.gallery_id, |
| 50 |
} |
| 51 |
|
| 52 |
def images(self, _): |
| 53 |
base = f"{self.root}/tumblrblog/gallery/{self.gallery_id}/" |
| 54 |
pnum = 1 |
| 55 |
|
| 56 |
while True: |
| 57 |
url = f"{base}{pnum}.html" |
| 58 |
response = self.request(url, allow_redirects=False, fatal=False) |
| 59 |
|
| 60 |
if response.status_code >= 300: |
| 61 |
return |
| 62 |
|
| 63 |
for url in self._urls_from_page(response.text): |
| 64 |
yield url, self._data_from_url(url) |
| 65 |
pnum += 1 |
| 66 |
|
| 67 |
|
| 68 |
class TumblrgalleryPostExtractor(TumblrgalleryExtractor): |
| 69 |
"""Extractor for Posts on tumblrgallery.xyz""" |
| 70 |
subcategory = "post" |
| 71 |
pattern = BASE_PATTERN + r"(/post/(\d+)\.html)" |
| 72 |
example = "https://tumblrgallery.xyz/post/12345.html" |
| 73 |
|
| 74 |
def __init__(self, match): |
| 75 |
TumblrgalleryExtractor.__init__(self, match) |
| 76 |
self.gallery_id = text.parse_int(match[2]) |
| 77 |
|
| 78 |
def metadata(self, page): |
| 79 |
return { |
| 80 |
"title" : text.remove_html( |
| 81 |
text.unescape(text.extr(page, "<title>", "</title>")) |
| 82 |
).replace("_", "-"), |
| 83 |
"gallery_id": self.gallery_id, |
| 84 |
} |
| 85 |
|
| 86 |
def images(self, page): |
| 87 |
for url in self._urls_from_page(page): |
| 88 |
yield url, self._data_from_url(url) |
| 89 |
|
| 90 |
|
| 91 |
class TumblrgallerySearchExtractor(TumblrgalleryExtractor): |
| 92 |
"""Extractor for Search result on tumblrgallery.xyz""" |
| 93 |
subcategory = "search" |
| 94 |
filename_fmt = "{category}_{num:>03}_{gallery_id}_{id}_{title}.{extension}" |
| 95 |
directory_fmt = ("{category}", "{search_term}") |
| 96 |
pattern = BASE_PATTERN + r"(/s\.php\?q=([^&#]+))" |
| 97 |
example = "https://tumblrgallery.xyz/s.php?q=QUERY" |
| 98 |
|
| 99 |
def __init__(self, match): |
| 100 |
TumblrgalleryExtractor.__init__(self, match) |
| 101 |
self.search_term = match[2] |
| 102 |
|
| 103 |
def metadata(self, page): |
| 104 |
return { |
| 105 |
"search_term": self.search_term, |
| 106 |
} |
| 107 |
|
| 108 |
def images(self, _): |
| 109 |
page_url = "s.php?q=" + self.search_term |
| 110 |
while True: |
| 111 |
page = self.request(self.root + "/" + page_url).text |
| 112 |
|
| 113 |
for gallery_id in text.extract_iter( |
| 114 |
page, '<div class="title"><a href="post/', '.html'): |
| 115 |
|
| 116 |
url = f"{self.root}/post/{gallery_id}.html" |
| 117 |
post_page = self.request(url).text |
| 118 |
|
| 119 |
for url in self._urls_from_page(post_page): |
| 120 |
data = self._data_from_url(url) |
| 121 |
data["gallery_id"] = gallery_id |
| 122 |
data["title"] = text.remove_html(text.unescape( |
| 123 |
text.extr(post_page, "<title>", "</title>") |
| 124 |
)).replace("_", "-") |
| 125 |
yield url, data |
| 126 |
|
| 127 |
next_url = text.extr( |
| 128 |
page, '</span> <a class="btn btn-primary" href="', '"') |
| 129 |
if not next_url or page_url == next_url: |
| 130 |
return |
| 131 |
page_url = next_url |