def importUserJSON(self): #Non-authenticated API login aapi = AppPixivAPI() self.userJSON = aapi.user_detail(self.user_ID) self.webpage = self.userJSON['profile']['webpage'] self.twitter_name = self.userJSON['profile']['twitter_account'] self.twitter_URL = self.userJSON['profile']['twitter_url'] self.pawoo_URL = self.userJSON['profile']['pawoo_url'] self.userImported = True
class Pixiv(DummySite): def __init__(self, url, username, password, proxy=None): proxies = get_proxy(proxy) requests_kwargs = { "timeout": (3, 10), } requests_kwargs.update(proxies) self.api = AppPixivAPI(**requests_kwargs) self._fetcher = PixivFetcher(**proxies) self.api.login(username, password) self._user_id = int(url.split("/")[-1]) self._dir_name = None self._total_illustrations = 0 self._fetch_user_detail() @property def fetcher(self): return self._fetcher @property def dir_name(self): assert self._dir_name is not None return self._dir_name def _fetch_user_detail(self): assert self._user_id is not None profile = self.api.user_detail(self._user_id) user = profile['user'] self._dir_name = "-".join([ user['name'], user['account'], str(user['id']), ]) self._dir_name = normalize_filename(self._dir_name) self._total_illustrations = profile['profile']['total_illusts'] return self.dir_name def _fetch_image_list(self, ): ret = self.api.user_illusts(self._user_id) while True: for illustration in ret.illusts: yield from parse_image_urls(illustration) if ret.next_url is None: break ret = self.api.user_illusts(**self.api.parse_qs(ret.next_url)) def _fetch_single_image_url(self, illustration_id): json_result = self.api.illust_detail(illustration_id) illustration_info = json_result.illust return illustration_info.image_urls['large'] @property def tasks(self): yield from self._fetch_image_list()
class Pixiv: def __init__(self): self.enabled = config.getboolean('Pixiv', 'enabled', fallback=False) self._refresh_token = config.get('Pixiv', 'refresh_token', fallback=None) self._log = logging.getLogger(__name__) self._pixiv = AppPixivAPI() self._pixiv.set_accept_language(config.get('Pixiv', 'language', fallback='en-US')) self._re_twitter = re.compile(r'^https?://(www.)?twitter.com/(?P<username>.+)$') if self.enabled: self._login() def _login(self) -> None: """ Authenticate to Pixiv Returns: None """ self._log.debug(f'[PIXIV] Authenticating to Pixiv with the token {self._refresh_token}') try: self._pixiv.auth(refresh_token=self._refresh_token) except Exception as error: self._log.exception("[PIXIV] Failed to authenticate to Pixiv", exc_info=error) def get_illust(self, illust_id: int) -> typing.Optional[dict]: """ Look up the provided illustration ID from SauceNao Args: illust_id (int): Returns: typing.Optional[dict] """ if not self.enabled: return None illust = self._pixiv.illust_detail(illust_id) if 'error' in illust and 'invalid_grant' in illust['error']['message']: self._log.warning(f'Pixiv login session is no longer valid; re-authenticating with saved token') self._login() illust = self._pixiv.illust_detail(illust_id) return illust['illust'] if illust and 'illust' in illust else None def get_author(self, author_id: int) -> typing.Optional[dict]: """ Get the author for the specified illustration Args: author_id (int): Returns: typing.Optional[dict] """ if not self.enabled: return None user = self._pixiv.user_detail(author_id) if 'error' in user and 'invalid_grant' in user['error']['message']: self._log.info(f'Re-Authenticating to Pixiv with the saved refresh token') self._login() user = self._pixiv.user_detail(author_id) return user def get_author_twitter(self, author_id: int) -> typing.Optional[str]: """ Get the Pixiv artists Twitter page, if available Args: author_id (int): Returns: typing.Optional[str] """ if not self.enabled: return None user = self.get_author(author_id) twitter_url = user['profile']['twitter_url'] if (user and 'profile' in user) else None if twitter_url: match = self._re_twitter.match(twitter_url) if match and match.group('username'): return f"@{match.group('username')}"