| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://www.mangoxo.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
import hashlib |
| 14 |
import time |
| 15 |
|
| 16 |
|
| 17 |
class MangoxoExtractor(Extractor): |
| 18 |
"""Base class for mangoxo extractors""" |
| 19 |
category = "mangoxo" |
| 20 |
root = "https://www.mangoxo.com" |
| 21 |
cookies_domain = "www.mangoxo.com" |
| 22 |
cookies_names = ("SESSION",) |
| 23 |
_warning = True |
| 24 |
|
| 25 |
def login(self): |
| 26 |
username, password = self._get_auth_info() |
| 27 |
if username: |
| 28 |
self.cookies_update(self.cache( |
| 29 |
self._login_impl, username, password, _exp=3*3600, _mem=False)) |
| 30 |
elif MangoxoExtractor._warning: |
| 31 |
MangoxoExtractor._warning = False |
| 32 |
self.log.warning("Unauthenticated users cannot see " |
| 33 |
"more than 5 images per album") |
| 34 |
|
| 35 |
def _login_impl(self, username, password): |
| 36 |
self.log.info("Logging in as %s", username) |
| 37 |
|
| 38 |
url = self.root + "/login" |
| 39 |
page = self.request(url).text |
| 40 |
token = text.extr(page, 'id="loginToken" value="', '"') |
| 41 |
|
| 42 |
url = self.root + "/api/login" |
| 43 |
headers = { |
| 44 |
"X-Requested-With": "XMLHttpRequest", |
| 45 |
"Referer": self.root + "/login", |
| 46 |
} |
| 47 |
data = self._sign_by_md5(username, password, token) |
| 48 |
response = self.request(url, method="POST", headers=headers, data=data) |
| 49 |
|
| 50 |
data = response.json() |
| 51 |
if str(data.get("result")) != "1": |
| 52 |
raise self.exc.AuthenticationError(data.get("msg")) |
| 53 |
return {"SESSION": self.cookies.get("SESSION")} |
| 54 |
|
| 55 |
def _sign_by_md5(self, username, password, token): |
| 56 |
|
| 57 |
params = [ |
| 58 |
("username" , username), |
| 59 |
("password" , password), |
| 60 |
("token" , token), |
| 61 |
("timestamp", str(int(time.time()))), |
| 62 |
] |
| 63 |
query = "&".join("=".join(item) for item in sorted(params)) |
| 64 |
query += "&secretKey=340836904" |
| 65 |
sign = hashlib.md5(query.encode()).hexdigest() |
| 66 |
params.append(("sign", sign.upper())) |
| 67 |
return params |
| 68 |
|
| 69 |
def _total_pages(self, page): |
| 70 |
return text.parse_int(text.extract(page, "total :", ",")[0]) |
| 71 |
|
| 72 |
|
| 73 |
class MangoxoAlbumExtractor(MangoxoExtractor): |
| 74 |
"""Extractor for albums on mangoxo.com""" |
| 75 |
subcategory = "album" |
| 76 |
filename_fmt = "{album[id]}_{num:>03}.{extension}" |
| 77 |
directory_fmt = ("{category}", "{channel[name]}", "{album[name]}") |
| 78 |
archive_fmt = "{album[id]}_{num}" |
| 79 |
pattern = r"(?:https?://)?(?:www\.)?mangoxo\.com/album/(\w+)" |
| 80 |
example = "https://www.mangoxo.com/album/ID" |
| 81 |
|
| 82 |
def __init__(self, match): |
| 83 |
MangoxoExtractor.__init__(self, match) |
| 84 |
self.album_id = match[1] |
| 85 |
|
| 86 |
def items(self): |
| 87 |
self.login() |
| 88 |
url = f"{self.root}/album/{self.album_id}/" |
| 89 |
page = self.request(url).text |
| 90 |
data = self.metadata(page) |
| 91 |
imgs = self.images(url, page) |
| 92 |
|
| 93 |
yield Message.Directory, "", data |
| 94 |
|
| 95 |
data["extension"] = None |
| 96 |
for data["num"], path in enumerate(imgs, 1): |
| 97 |
data["id"] = text.parse_int(text.extr(path, "=", "&")) |
| 98 |
url = self.root + "/external/" + path.rpartition("url=")[2] |
| 99 |
yield Message.Url, url, text.nameext_from_url(url, data) |
| 100 |
|
| 101 |
def metadata(self, page): |
| 102 |
"""Return general metadata""" |
| 103 |
extr = text.extract_from(page) |
| 104 |
title = extr('<img id="cover-img" alt="', '"') |
| 105 |
cid = extr('href="https://www.mangoxo.com/user/', '"') |
| 106 |
cname = extr('<img alt="', '"') |
| 107 |
cover = extr(' src="', '"') |
| 108 |
count = extr('id="pic-count">', '<') |
| 109 |
date = extr('class="fa fa-calendar"></i>', '<') |
| 110 |
descr = extr('<pre>', '</pre>') |
| 111 |
|
| 112 |
return { |
| 113 |
"channel": { |
| 114 |
"id": cid, |
| 115 |
"name": text.unescape(cname), |
| 116 |
"cover": cover, |
| 117 |
}, |
| 118 |
"album": { |
| 119 |
"id": self.album_id, |
| 120 |
"name": text.unescape(title), |
| 121 |
"date": self.parse_datetime(date.strip(), "%Y.%m.%d %H:%M"), |
| 122 |
"description": text.unescape(descr), |
| 123 |
}, |
| 124 |
"count": text.parse_int(count), |
| 125 |
} |
| 126 |
|
| 127 |
def images(self, url, page): |
| 128 |
"""Generator; Yields all image URLs""" |
| 129 |
total = self._total_pages(page) |
| 130 |
num = 1 |
| 131 |
|
| 132 |
while True: |
| 133 |
yield from text.extract_iter( |
| 134 |
page, 'class="lightgallery-item" href="', '"') |
| 135 |
if num >= total: |
| 136 |
return |
| 137 |
num += 1 |
| 138 |
page = self.request(url + str(num)).text |
| 139 |
|
| 140 |
|
| 141 |
class MangoxoChannelExtractor(MangoxoExtractor): |
| 142 |
"""Extractor for all albums on a mangoxo channel""" |
| 143 |
subcategory = "channel" |
| 144 |
pattern = r"(?:https?://)?(?:www\.)?mangoxo\.com/(\w+)/album" |
| 145 |
example = "https://www.mangoxo.com/USER/album" |
| 146 |
|
| 147 |
def __init__(self, match): |
| 148 |
MangoxoExtractor.__init__(self, match) |
| 149 |
self.user = match[1] |
| 150 |
|
| 151 |
def items(self): |
| 152 |
self.login() |
| 153 |
num = total = 1 |
| 154 |
url = f"{self.root}/{self.user}/album/" |
| 155 |
data = {"_extractor": MangoxoAlbumExtractor} |
| 156 |
|
| 157 |
while True: |
| 158 |
page = self.request(url + str(num)).text |
| 159 |
|
| 160 |
for album in text.extract_iter( |
| 161 |
page, '<a class="link black" href="', '"'): |
| 162 |
yield Message.Queue, album, data |
| 163 |
|
| 164 |
if num == 1: |
| 165 |
total = self._total_pages(page) |
| 166 |
if num >= total: |
| 167 |
return |
| 168 |
num += 1 |