async def getPost(self, post_id: int): url = 'https://' + self.url + '/post/show/' + str(post_id) async with Request() as request: async with request.get(url) as response: try: response.raise_for_status() except ClientResponseError as err: raise NazurinError(err) from None response = await response.text() soup = BeautifulSoup(response, 'html.parser') tag = soup.find(id="post-view").find(recursive=False) if tag.name == 'script': content = str.strip(tag.string) elif tag.name == 'div' and ('status-notice' in tag['class']): raise NazurinError(tag.get_text(strip=True)) else: logger.error(tag) raise NazurinError('Unknown error') info = content[19:-2] try: info = json.loads(info) post = info['posts'][0] tags = info['tags'] except json.decoder.JSONDecodeError as err: logger.error(err) return post, tags
async def bookmark(self, artwork_id: int): response = await self.call(Pixiv.illust_bookmark_add, artwork_id) if 'error' in response.keys(): logger.error(response) raise NazurinError(response['error']['user_message']) else: logger.info('Bookmarked artwork %s', artwork_id) return True
async def on_error(update: Update, exception: Exception): try: raise exception except NazurinError as error: await update.message.reply(error.msg) except Exception as error: logger.error('Update %s caused %s: %s', update, type(error), error) traceback.print_exc() if not isinstance(error, TelegramAPIError): await update.message.reply('Error: ' + str(error)) return True
async def bookmark(self, artwork_id: int, privacy: PixivPrivacy = PixivPrivacy.PUBLIC): response = await self.call(Pixiv.illust_bookmark_add, artwork_id, privacy.value) if 'error' in response.keys(): logger.error(response) raise NazurinError(response.error.user_message or response.error.message) logger.info('Bookmarked artwork %s, privacy = %s', artwork_id, privacy.value) return True
def convert(config: File, output: File): cmd = f'ffmpeg -i {config.path} -vcodec libx264 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -y {output.path}' logger.info('Calling FFmpeg with command: %s', cmd) args = shlex.split(cmd) try: output = subprocess.check_output(args, stderr=subprocess.STDOUT, shell=False) except subprocess.CalledProcessError as error: logger.error('FFmpeg failed with code %s, output:\n %s', error.returncode, error.output) raise NazurinError( 'Failed to convert ugoira to mp4.') from None
def convert(config: File, output: File): # For some illustrations like https://www.pixiv.net/artworks/44298467, # the output video is in YUV444P colorspace, which can't be played on some devices, # thus we convert to YUV420P colorspace for better compatibility. cmd = f'ffmpeg -i "{config.path}" -vcodec libx264 -pix_fmt yuv420p -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -y "{output.path}"' logger.info('Calling FFmpeg with command: %s', cmd) args = shlex.split(cmd) try: output = subprocess.check_output(args, stderr=subprocess.STDOUT, shell=False) except subprocess.CalledProcessError as error: logger.error('FFmpeg failed with code %s, output:\n %s', error.returncode, error.output.decode()) raise NazurinError( 'Failed to convert ugoira to mp4.') from None