| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://imgpile.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?imgpile\.com" |
| 15 |
|
| 16 |
|
| 17 |
class ImgpileExtractor(Extractor): |
| 18 |
"""Base class for imgpile extractors""" |
| 19 |
category = "imgpile" |
| 20 |
root = "https://imgpile.com" |
| 21 |
directory_fmt = ("{category}", "{post[author]}", |
| 22 |
"{post[title]} ({post[id_slug]})") |
| 23 |
archive_fmt = "{post[id_slug]}_{id}" |
| 24 |
|
| 25 |
|
| 26 |
class ImgpilePostExtractor(ImgpileExtractor): |
| 27 |
subcategory = "post" |
| 28 |
pattern = BASE_PATTERN + r"/p/(\w+)" |
| 29 |
example = "https://imgpile.com/p/AbCdEfG" |
| 30 |
|
| 31 |
def items(self): |
| 32 |
post_id = self.groups[0] |
| 33 |
url = f"{self.root}/p/{post_id}" |
| 34 |
page = self.request(url).text |
| 35 |
extr = text.extract_from(page) |
| 36 |
|
| 37 |
post = { |
| 38 |
"id_slug": post_id, |
| 39 |
"title" : text.unescape(extr("<title>", " - imgpile<")), |
| 40 |
"id" : text.parse_int(extr('data-post-id="', '"')), |
| 41 |
"author" : extr('/u/', '"'), |
| 42 |
"score" : text.parse_int(text.remove_html(extr( |
| 43 |
'class="post-score">', "</"))), |
| 44 |
"views" : text.parse_int(extr( |
| 45 |
'class="meta-value">', "<").replace(",", "")), |
| 46 |
"tags" : text.split_html(extr( |
| 47 |
" <!-- Tags -->", '<!-- "')), |
| 48 |
} |
| 49 |
|
| 50 |
files = self._extract_files(extr) |
| 51 |
data = {"post": post} |
| 52 |
data["count"] = post["count"] = len(files) |
| 53 |
|
| 54 |
yield Message.Directory, "", data |
| 55 |
for data["num"], file in enumerate(files, 1): |
| 56 |
data.update(file) |
| 57 |
url = file["url"] |
| 58 |
yield Message.Url, url, text.nameext_from_url(url, data) |
| 59 |
|
| 60 |
def _extract_files(self, extr): |
| 61 |
files = [] |
| 62 |
|
| 63 |
while True: |
| 64 |
media = extr('lass="post-media', '</div>') |
| 65 |
if not media: |
| 66 |
break |
| 67 |
files.append({ |
| 68 |
"id_slug": text.extr(media, 'data-id="', '"'), |
| 69 |
"id" : text.parse_int(text.extr( |
| 70 |
media, 'data-media-id="', '"')), |
| 71 |
"url": "http" + text.extr(media, '<a href="http', '"'), |
| 72 |
}) |
| 73 |
return files |
| 74 |
|
| 75 |
|
| 76 |
class ImgpileUserExtractor(ImgpileExtractor): |
| 77 |
subcategory = "user" |
| 78 |
pattern = BASE_PATTERN + r"/u/([^/?#]+)" |
| 79 |
example = "https://imgpile.com/u/USER" |
| 80 |
|
| 81 |
def items(self): |
| 82 |
url = self.root + "/api/v1/posts" |
| 83 |
params = { |
| 84 |
"limit" : "100", |
| 85 |
"sort" : "latest", |
| 86 |
"period" : "all", |
| 87 |
"visibility": "public", |
| 88 |
"username" : self.groups[0], |
| 89 |
} |
| 90 |
headers = { |
| 91 |
"Accept" : "application/json", |
| 92 |
"Content-Type": "application/json", |
| 93 |
} |
| 94 |
|
| 95 |
base = self.root + "/p/" |
| 96 |
while True: |
| 97 |
data = self.request_json(url, params=params, headers=headers) |
| 98 |
|
| 99 |
if params is not None: |
| 100 |
params = None |
| 101 |
self.kwdict["total"] = data["meta"]["total"] |
| 102 |
|
| 103 |
for item in data["data"]: |
| 104 |
item["_extractor"] = ImgpilePostExtractor |
| 105 |
url = base + item["slug"] |
| 106 |
yield Message.Queue, url, item |
| 107 |
|
| 108 |
url = data["links"].get("next") |
| 109 |
if not url: |
| 110 |
return |