141 lines
5.2 KiB
Python
Raw Normal View History

2015-11-09 02:29:33 +01:00
# -*- coding: utf-8 -*-
2014-10-12 21:56:44 +02:00
# Copyright 2014-2017 Mike Fährmann
2015-11-09 02:29:33 +01:00
#
# 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.
"""Extract images from https://chan.sankakucomplex.com/"""
from .common import Extractor, Message
from .. import text, util, exception
from ..cache import cache
import time
import random
2015-11-09 02:29:33 +01:00
2017-02-01 00:53:19 +01:00
class SankakuTagExtractor(Extractor):
2017-02-01 00:53:19 +01:00
"""Extractor for images from chan.sankakucomplex.com by search-tags"""
2015-11-21 04:26:30 +01:00
category = "sankaku"
subcategory = "tag"
2015-11-21 04:26:30 +01:00
directory_fmt = ["{category}", "{tags}"]
filename_fmt = "{category}_{id}_{md5}.{extension}"
pattern = [r"(?:https?://)?chan\.sankakucomplex\.com"
r"/\?(?:[^&#]*&)*tags=([^&#]+)"]
2016-09-19 16:15:27 +02:00
test = [("https://chan.sankakucomplex.com/?tags=bonocho", {
2017-09-04 17:41:11 +02:00
"count": 5,
"pattern": (r"https://cs\.sankakucomplex\.com/data/[^/]{2}/[^/]{2}"
r"/[^/]{32}\.\w+\?e=\d+&m=[^&#]+"),
2016-09-19 16:15:27 +02:00
})]
root = "https://chan.sankakucomplex.com"
cookienames = ("login", "pass_hash")
cookiedomain = "chan.sankakucomplex.com"
2014-10-12 21:56:44 +02:00
2015-11-09 02:29:33 +01:00
def __init__(self, match):
Extractor.__init__(self)
self.logged_in = True
self.pagestart = 1
self.tags = text.unquote(match.group(1).replace("+", " "))
self.wait_min = self.config("wait-min", 2)
self.wait_max = self.config("wait-max", 4)
if self.wait_max < self.wait_min:
self.wait_max = self.wait_min
2014-10-12 21:56:44 +02:00
def skip(self, num):
pages = min(num // 20, 49)
self.pagestart += pages
return pages * 20
2015-11-09 02:29:33 +01:00
def items(self):
self.login()
2015-11-09 02:29:33 +01:00
data = self.get_job_metadata()
2015-11-10 00:55:01 +01:00
yield Message.Version, 1
2015-11-09 02:29:33 +01:00
yield Message.Directory, data
for image in self.get_images():
2015-11-10 00:55:01 +01:00
image.update(data)
yield Message.Url, image["file_url"], image
2015-11-09 02:29:33 +01:00
def get_job_metadata(self):
"""Collect metadata for extractor-job"""
2016-09-25 14:22:07 +02:00
return {"tags": self.tags}
2015-11-09 02:29:33 +01:00
def get_images(self):
"""Yield all available images for the given tags"""
2015-11-09 02:29:33 +01:00
params = {
"tags": self.tags,
"page": self.pagestart,
2015-11-09 02:29:33 +01:00
}
while self.logged_in or params["page"] <= 25:
image = None
page = self.request(self.root, params=params, retries=10).text
2015-11-10 00:55:01 +01:00
pos = text.extract(page, '<div id=more-popular-posts-link>', '')[1]
for image_id in text.extract_iter(
page, '<span class="thumb blacklisted" id=p', '>', pos):
self.wait()
2015-11-10 00:55:01 +01:00
image = self.get_image_metadata(image_id)
2015-11-09 02:29:33 +01:00
yield image
if not image:
2015-11-09 02:29:33 +01:00
return
2014-10-12 21:56:44 +02:00
params["page"] += 1
params["next"] = image["id"] - 1
self.log.warning(
"Unauthenticated users may only access the first 500 images / 25 "
"pages. (Use '--range 501-' to continue downloading from this "
"point onwards after setting up an account.)")
2015-11-09 02:29:33 +01:00
2015-11-10 00:55:01 +01:00
def get_image_metadata(self, image_id):
"""Collect metadata for a single image"""
2015-11-10 00:55:01 +01:00
url = "https://chan.sankakucomplex.com/post/show/" + image_id
page = self.request(url, retries=10).text
file_url, pos = text.extract(page, '<li>Original: <a href="', '"')
if file_url:
width , pos = text.extract(page, '>', 'x', pos)
height, pos = text.extract(page, '', ' ', pos)
else:
width , pos = text.extract(page, '<object width=', ' ', pos)
height, pos = text.extract(page, 'height=', '>', pos)
file_url = text.extract(page, '<embed src="', '"', pos)[0]
data = text.nameext_from_url(file_url, {
"id": util.safe_int(image_id),
"file_url": "https:" + text.unescape(file_url),
"width": util.safe_int(width),
"height": util.safe_int(height),
2015-11-16 17:32:26 +01:00
})
data["md5"] = data["name"]
return data
def wait(self):
"""Wait for a randomly chosen amount of seconds"""
time.sleep(random.uniform(self.wait_min, self.wait_max))
def login(self):
"""Login and set necessary cookies"""
if self._check_cookies(self.cookienames):
return
username, password = self._get_auth_info()
if username:
cookies = self._login_impl(username, password)
for key, value in cookies.items():
self.session.cookies.set(
key, value, domain=self.cookiedomain)
else:
self.logged_in = False
@cache(maxage=90*24*60*60, keyarg=1)
def _login_impl(self, username, password):
"""Actual login implementation"""
self.log.info("Logging in as %s", username)
params = {
"url": "",
"user[name]": username,
"user[password]": password,
"commit": "Login",
}
response = self.request(self.root + "/user/authenticate",
method="POST", params=params)
if not response.history or response.url != self.root + "/user/home":
raise exception.AuthenticationError()
cookies = response.history[0].cookies
return {c: cookies[c] for c in self.cookienames}