# -*- coding: utf-8 -*- # Copyright 2020 Mike Fährmann # # 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. """Extractors for https://www.subscribestar.com/""" from .common import Extractor, Message from .. import text, exception from ..cache import cache import datetime import json BASE_PATTERN = r"(?:https?://)?(?:www\.)?subscribestar\.(com|adult)" class SubscribestarExtractor(Extractor): """Base class for subscribestar extractors""" category = "subscribestar" root = "https://www.subscribestar.com" directory_fmt = ("{category}", "{author_name}") filename_fmt = "{post_id}_{id}.{extension}" archive_fmt = "{id}" cookiedomain = "www.subscribestar.com" cookienames = ("auth_token",) def __init__(self, match): tld, self.item = match.groups() if tld == "adult": self.root = "https://subscribestar.adult" self.cookiedomain = "subscribestar.adult" self.subcategory += "-adult" Extractor.__init__(self, match) self.metadata = self.config("metadata", False) self._year = " " + str(datetime.date.today().year) def items(self): self.login() for post_html in self.posts(): media = self._media_from_post(post_html) if not media: continue data = self._data_from_post(post_html) yield Message.Directory, data for item in media: item.update(data) url = item["url"] yield Message.Url, url, text.nameext_from_url(url, item) def posts(self): """Yield HTML content of all relevant posts""" def login(self): if self._check_cookies(self.cookienames): return username, password = self._get_auth_info() if username: cookies = self._login_impl(username, password) self._update_cookies(cookies) @cache(maxage=28*24*3600, keyarg=1) def _login_impl(self, username, password): self.log.info("Logging in as %s", username) url = "https://www.subscribestar.com/session.json" headers = { "Origin" : "https://www.subscribestar.com", "Referer" : "https://www.subscribestar.com/login", "X-Requested-With": "XMLHttpRequest", } data = { "utf8" : "✓", "email" : username, "password": password, } response = self.request( url, method="POST", headers=headers, data=data, fatal=False) if response.json().get("errors"): self.log.debug(response.json()["errors"]) raise exception.AuthenticationError() return { cookie.name: cookie.value for cookie in response.cookies if cookie.name.startswith("auth") } @staticmethod def _media_from_post(html): gallery = text.extract(html, 'data-gallery="', '"')[0] if gallery: return [ item for item in json.loads(text.unescape(gallery)) if "/previews/" not in item["url"] ] return () def _data_from_post(self, html): extr = text.extract_from(html) data = { "post_id" : text.parse_int(extr('data-id="', '"')), "author_id" : text.parse_int(extr('data-user-id="', '"')), "author_name": text.unescape(extr('href="/', '"')), "author_nick": text.unescape(extr('>', '<')), "content" : (extr( '