2021-03-03 15:20:47 +01:00
|
|
|
# -*- 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.
|
|
|
|
|
2021-03-03 15:37:56 +01:00
|
|
|
"""Extractors for https://tumblrgallery.xyz/"""
|
2021-03-03 15:20:47 +01:00
|
|
|
|
|
|
|
from .common import GalleryExtractor
|
|
|
|
from .. import text
|
|
|
|
|
2021-03-03 15:37:56 +01:00
|
|
|
BASE_PATTERN = r"(?:https?://)?tumblrgallery\.xyz"
|
2021-03-03 15:20:47 +01:00
|
|
|
|
|
|
|
|
2021-03-03 15:37:56 +01:00
|
|
|
class TumblrgalleryExtractor(GalleryExtractor):
|
2021-03-03 15:20:47 +01:00
|
|
|
"""Base class for tumblrgallery extractors"""
|
|
|
|
category = "tumblrgallery"
|
2021-03-03 15:37:56 +01:00
|
|
|
filename_fmt = "{category}_{gallery_id}_{num:>03}_{id}.{extension}"
|
|
|
|
directory_fmt = ("{category}", "{gallery_id} {title}")
|
|
|
|
root = "https://tumblrgallery.xyz"
|
2021-03-03 15:20:47 +01:00
|
|
|
|
2021-12-19 05:46:02 +01:00
|
|
|
@staticmethod
|
|
|
|
def _urls_from_page(page):
|
|
|
|
return text.extract_iter(
|
|
|
|
page, '<div class="report"> <a class="xx-co-me" href="', '"')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _data_from_url(url):
|
|
|
|
filename = text.nameext_from_url(url)["filename"]
|
|
|
|
parts = filename.split("_")
|
|
|
|
try:
|
|
|
|
return {"id": parts[1] if parts[1] != "inline" else parts[2]}
|
|
|
|
except IndexError:
|
|
|
|
return {"id": filename}
|
|
|
|
|
2021-03-03 15:20:47 +01:00
|
|
|
|
2021-03-03 15:37:56 +01:00
|
|
|
class TumblrgalleryTumblrblogExtractor(TumblrgalleryExtractor):
|
2021-03-03 15:20:47 +01:00
|
|
|
"""Extractor for Tumblrblog on tumblrgallery.xyz"""
|
|
|
|
subcategory = "tumblrblog"
|
2021-03-03 15:37:56 +01:00
|
|
|
pattern = BASE_PATTERN + r"(/tumblrblog/gallery/(\d+)\.html)"
|
|
|
|
test = ("https://tumblrgallery.xyz/tumblrblog/gallery/103975.html",)
|
2021-03-03 15:20:47 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2021-03-03 15:37:56 +01:00
|
|
|
TumblrgalleryExtractor.__init__(self, match)
|
2021-03-03 15:20:47 +01:00
|
|
|
self.gallery_id = text.parse_int(match.group(2))
|
|
|
|
|
|
|
|
def metadata(self, page):
|
|
|
|
return {
|
|
|
|
"title" : text.unescape(text.extract(page, "<h1>", "</h1>"))[0],
|
|
|
|
"gallery_id": self.gallery_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
def images(self, _):
|
|
|
|
page_num = 1
|
|
|
|
while True:
|
2021-12-19 05:46:02 +01:00
|
|
|
url = "{}/tumblrblog/gallery/{}/{}.html".format(
|
|
|
|
self.root, self.gallery_id, page_num)
|
|
|
|
response = self.request(url, allow_redirects=False, fatal=False)
|
|
|
|
|
|
|
|
if response.status_code >= 300:
|
2021-03-03 15:20:47 +01:00
|
|
|
return
|
|
|
|
|
2021-12-19 05:46:02 +01:00
|
|
|
for url in self._urls_from_page(response.text):
|
|
|
|
yield url, self._data_from_url(url)
|
2021-03-03 15:20:47 +01:00
|
|
|
page_num += 1
|
|
|
|
|
|
|
|
|
2021-03-03 15:37:56 +01:00
|
|
|
class TumblrgalleryPostExtractor(TumblrgalleryExtractor):
|
2021-03-03 15:20:47 +01:00
|
|
|
"""Extractor for Posts on tumblrgallery.xyz"""
|
|
|
|
subcategory = "post"
|
2021-03-03 15:37:56 +01:00
|
|
|
pattern = BASE_PATTERN + r"(/post/(\d+)\.html)"
|
2021-12-17 19:55:53 +01:00
|
|
|
test = ("https://tumblrgallery.xyz/post/405674.html", {
|
|
|
|
"pattern": r"https://78\.media\.tumblr\.com/bec67072219c1f3bc04fd9711d"
|
|
|
|
r"ec42ef/tumblr_p51qq1XCHS1txhgk3o1_1280\.jpg",
|
|
|
|
"count": 3,
|
|
|
|
})
|
2021-03-03 15:20:47 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2021-03-03 15:37:56 +01:00
|
|
|
TumblrgalleryExtractor.__init__(self, match)
|
2021-03-03 15:20:47 +01:00
|
|
|
self.gallery_id = text.parse_int(match.group(2))
|
|
|
|
|
|
|
|
def metadata(self, page):
|
|
|
|
return {
|
|
|
|
"title" : text.remove_html(
|
|
|
|
text.unescape(text.extract(page, "<title>", "</title>")[0])
|
|
|
|
).replace("_", "-"),
|
|
|
|
"gallery_id": self.gallery_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
def images(self, page):
|
2021-12-19 05:46:02 +01:00
|
|
|
for url in self._urls_from_page(page):
|
|
|
|
yield url, self._data_from_url(url)
|
2021-03-03 15:20:47 +01:00
|
|
|
|
|
|
|
|
2021-03-03 15:37:56 +01:00
|
|
|
class TumblrgallerySearchExtractor(TumblrgalleryExtractor):
|
2021-03-03 15:20:47 +01:00
|
|
|
"""Extractor for Search result on tumblrgallery.xyz"""
|
|
|
|
subcategory = "search"
|
|
|
|
filename_fmt = "{category}_{num:>03}_{gallery_id}_{id}_{title}.{extension}"
|
|
|
|
directory_fmt = ("{category}", "{search_term}")
|
2021-03-03 15:37:56 +01:00
|
|
|
pattern = BASE_PATTERN + r"(/s\.php\?q=([^&#]+))"
|
|
|
|
test = ("https://tumblrgallery.xyz/s.php?q=everyday-life",)
|
2021-03-03 15:20:47 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2021-03-03 15:37:56 +01:00
|
|
|
TumblrgalleryExtractor.__init__(self, match)
|
2021-03-03 15:20:47 +01:00
|
|
|
self.search_term = match.group(2)
|
|
|
|
|
|
|
|
def metadata(self, page):
|
|
|
|
return {
|
|
|
|
"search_term": self.search_term,
|
|
|
|
}
|
|
|
|
|
|
|
|
def images(self, _):
|
|
|
|
page_num = 1
|
|
|
|
while True:
|
2021-12-19 05:46:02 +01:00
|
|
|
url = "{}/s.php?q={}&page={}".format(
|
|
|
|
self.root, self.search_term, page_num)
|
|
|
|
response = self.request(url, allow_redirects=False, fatal=False)
|
|
|
|
|
|
|
|
if response.status_code >= 300:
|
2021-03-03 15:20:47 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
page = response.text
|
|
|
|
page_num += 1
|
|
|
|
|
2021-12-19 05:46:02 +01:00
|
|
|
for gallery_id in text.extract_iter(
|
|
|
|
page, '<div class="title"><a href="post/', '.html'):
|
|
|
|
|
|
|
|
url = "{}/post/{}.html".format(self.root, gallery_id)
|
|
|
|
post_page = self.request(url).text
|
|
|
|
|
|
|
|
for url in self._urls_from_page(post_page):
|
|
|
|
data = self._data_from_url(url)
|
|
|
|
data["gallery_id"] = gallery_id
|
|
|
|
data["title"] = text.remove_html(text.unescape(
|
|
|
|
text.extract(post_page, "<title>", "</title>")[0]
|
|
|
|
)).replace("_", "-")
|
|
|
|
yield url, data
|