# -*- coding: utf-8 -*- # Copyright 2020-2023 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.boosty.to/""" from .common import Extractor, Message from .. import text, util, exception from ..cache import cache BASE_PATTERN = r"(?:https?://)?boosty\.to" class BoostyExtractor(Extractor): """Base class for boosty extractors""" category = "boosty" root = "https://www.boosty.to" directory_fmt = ("{category}", "{author_name}") filename_fmt = "{post_id}_{id}.{extension}" archive_fmt = "{id}" cookies_domain = ".boosty.to" cookies_names = ("auth", ) def __init__(self, match): self.api = BoostyAPI(self) def posts(self): """Yield JSON content of all relevant posts""" class BoostyUserExtractor(Extractor): """Extractor for boosty.to user profiles""" category = "boosty" subcategory = "user" root = "https://api.boosty.to" pattern = BASE_PATTERN + r"/([^/?#]+)" example = "https://boosty.to/USER" def __init__(self, match): Extractor.__init__(self, match) self.user = match.group(1) def items(self): for post in self.posts(): post["num"] = 0 images = () if "data" in post: data = post["data"] if "hasAccess" in data: access = data["hasAccess"] print(access) if bool(access): image_data = data["data"] for image in image_data: if "url" in image: print(image["url"]) def posts(self): url = self.root + "/v1/blog/{}/post/".format(self.user) params = { "limit" : 5, "offset" : "", "comments_limit" : 2, "reply_limit" : 1, } data = self.request(url).json() yield from data["data"] offset = data["extra"].get("offset") if data["extra"].get("isLast"): return data["data"] params["offset"] = data["extra"].get("offset") while True: data = self.request(url, params=params).json() yield from data["data"] if data["extra"].get("isLast"): return data["data"] params["offset"] = data["extra"].get("offset") class BoostyAPI(): """Interface for the Boosty API""" root = "https://api.boosty.to" def __init__(self, extractor, access_token): self.extractor = extractor self.headers = { "Origin": self.root, "Authorization": "Bearer " + self.cookies.get( "auth", domain=".boosty.to").json()["accessToken"], } def post(self, post_id): endpoint = "/v1/post/" + post_id return self._call(endpoint) def user(self, username): endpoint = "/v1/blog/" + username return self._call(endpoint) def _call(self, endpoint): url = self.root + endpoint while True: response = self.extractor.request( url, headers=self.headers, fatal=None, allow_redirects=False) if response.status_code < 300: return response.json()["data"] elif response.status_code < 400: raise exception.AuthenticationError("Invalid API access token") elif response.status_code == 429: self.extractor.wait(seconds=600) else: self.extractor.log.debug(response.text) raise exception.StopExtraction("API request failed")