| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://www.imagepond.net/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?imagepond\.net" |
| 15 |
|
| 16 |
|
| 17 |
class ImagepondExtractor(Extractor): |
| 18 |
"""Base class for imagepond extractors""" |
| 19 |
basecategory = "chevereto" |
| 20 |
category = "imagepond" |
| 21 |
root = "https://www.imagepond.net" |
| 22 |
directory_fmt = ("{category}", "{user}", "{album}") |
| 23 |
archive_fmt = "{id}" |
| 24 |
parent = True |
| 25 |
|
| 26 |
def _pagination(self, url, callback=None, pattern=None): |
| 27 |
page = self.request(url).text |
| 28 |
|
| 29 |
if callback is not None: |
| 30 |
callback(page) |
| 31 |
|
| 32 |
if pattern is not None: |
| 33 |
find = text.re(pattern).findall |
| 34 |
|
| 35 |
while True: |
| 36 |
if pattern is None: |
| 37 |
yield from text.extract_iter( |
| 38 |
page, "'javascript:void(0)' : '", "'") |
| 39 |
else: |
| 40 |
yield from find(page) |
| 41 |
|
| 42 |
pos = page.find(' rel="next"') |
| 43 |
if pos < 0: |
| 44 |
break |
| 45 |
anchor = page[page.rfind("<", None, pos):page.find(">", pos)] |
| 46 |
url = text.extr(anchor, 'href="', '"') |
| 47 |
if not url: |
| 48 |
break |
| 49 |
page = self.request(text.unescape(url)).text |
| 50 |
|
| 51 |
|
| 52 |
class ImagepondFileExtractor(ImagepondExtractor): |
| 53 |
subcategory = "file" |
| 54 |
pattern = BASE_PATTERN + r"(/(?:i(?:mg|mage)?|video)/[^/?#]+)" |
| 55 |
example = "https://www.imagepond.net/i/ID" |
| 56 |
|
| 57 |
def items(self): |
| 58 |
url = self.root + self.groups[0] |
| 59 |
response = self.request(url) |
| 60 |
extr = text.extract_from(response.text) |
| 61 |
|
| 62 |
title = text.unescape(extr('property="og:title" content="', '"')) |
| 63 |
type = extr('property="og:type" content="', '"') |
| 64 |
|
| 65 |
if type == "image": |
| 66 |
file = { |
| 67 |
"type": "image", |
| 68 |
"url": extr('property="og:image" content="', '"'), |
| 69 |
"width": text.parse_int(extr( |
| 70 |
'property="og:image:width" content="', '"')), |
| 71 |
"height": text.parse_int(extr( |
| 72 |
'property="og:image:height" content="', '"')), |
| 73 |
} |
| 74 |
else: |
| 75 |
file = { |
| 76 |
"type": "video", |
| 77 |
"url": extr('property="og:video" content="', '"'), |
| 78 |
"mime": extr('property="og:video:type" content="', '"'), |
| 79 |
"width": text.parse_int(extr( |
| 80 |
'property="og:video:width" content="', '"')), |
| 81 |
"height": text.parse_int(extr( |
| 82 |
'property="og:video:height" content="', '"')), |
| 83 |
"thumbnail": extr('property="og:image" content="', '"'), |
| 84 |
} |
| 85 |
extr('<span class="uppercase', "</span>") |
| 86 |
m, _, s = extr('<span>', "</span>").partition(":") |
| 87 |
file["duration"] = text.parse_int(m) * 60 + text.parse_int(s) |
| 88 |
|
| 89 |
file["id"] = response.url.rpartition("/")[2] |
| 90 |
file["title"] = title |
| 91 |
file["date"] = self.parse_datetime(extr( |
| 92 |
'<span class="hidden sm:inline">', '<'), "%b %d, %Y") |
| 93 |
file["user"] = extr('/user/', '"') |
| 94 |
|
| 95 |
if aid := extr("/a/", '"'): |
| 96 |
file["album"] = text.unescape(extr( |
| 97 |
"<span ", "<").partition(">")[2]) |
| 98 |
file["album_id"] = aid |
| 99 |
else: |
| 100 |
file["album"] = file["album_id"] = "" |
| 101 |
|
| 102 |
text.nameext_from_url(file["url"], file) |
| 103 |
if "mime" not in file: |
| 104 |
file["mime"] = f"{file['type']}/{file['extension']}" |
| 105 |
yield Message.Directory, "", file |
| 106 |
yield Message.Url, file["url"], file |
| 107 |
|
| 108 |
|
| 109 |
class ImagepondAlbumExtractor(ImagepondExtractor): |
| 110 |
subcategory = "album" |
| 111 |
pattern = BASE_PATTERN + r"/a(?:lbum)?/(.+)" |
| 112 |
example = "https://www.imagepond.net/a/ID" |
| 113 |
|
| 114 |
def items(self): |
| 115 |
url = f"{self.root}/a/{self.groups[0]}" |
| 116 |
|
| 117 |
data = {"_extractor": ImagepondFileExtractor} |
| 118 |
for self.kwdict["num"], item_url in enumerate(self._pagination( |
| 119 |
url, callback=self._extract_metadata_album), 1): |
| 120 |
yield Message.Queue, item_url, data |
| 121 |
|
| 122 |
def _extract_metadata_album(self, page): |
| 123 |
kwdict = self.kwdict |
| 124 |
title, pos = text.extract(page, '<h1', '<') |
| 125 |
kwdict["album"] = "" if title is None else text.unescape( |
| 126 |
title[title.find(">")+1:]) |
| 127 |
kwdict["count"] = text.parse_int(text.extract( |
| 128 |
page, "<span>", " ", pos)[0]) |
| 129 |
|
| 130 |
|
| 131 |
class ImagepondUserExtractor(ImagepondExtractor): |
| 132 |
subcategory = "user" |
| 133 |
pattern = BASE_PATTERN + r"/(?:user/)?(.+)" |
| 134 |
example = "https://www.imagepond.net/user/USER" |
| 135 |
|
| 136 |
def items(self): |
| 137 |
url = f"{self.root}/user/{self.groups[0]}" |
| 138 |
|
| 139 |
base = self.root + "/i/" |
| 140 |
data_file = {"_extractor": ImagepondFileExtractor} |
| 141 |
for path in self._pagination(url, pattern=r'/i/([^/?#"]+)"'): |
| 142 |
yield Message.Queue, base + path, data_file |