upbit / pixivpy

Pixiv API for Python
https://pypi.org/project/PixivPy3/#files
The Unlicense
1.8k stars 147 forks source link

'NoneType' object has no attribute 'meta_single_page' #371

Open Air847735 opened 1 day ago

Air847735 commented 1 day ago

I try to combine pixiv pictures in writing DC-bot The goal is to capture the pixiv URL in the DC chat room and then upload the pictures to DC for viewing.

But after setting it up for a period of time, NoneType' object has no attribute 'meta_single_page will appear.

Does anyone know how to avoid this situation?

Code:

DISCORD_TOKEN = ""
PIXIV_REFRESH_TOKEN = ""
import discord
from pixivpy3 import *
import re
import os

class PixivBot(discord.Client):
 def __init__(self, refresh_token):
 intents = discord.Intents.default()
 intents.message_content = True
 super().__init__(intents=intents)

 self.pixiv_api = AppPixivAPI()
 self.pixiv_api.auth(refresh_token=refresh_token)

 async def download_pixiv_image(self, pixiv_url):
 #getimageid
 illust_id = re.search(r'artworks/(\d+)', pixiv_url).group(1)

 # get image detail
 illust_detail = self.pixiv_api.illust_detail(illust_id)

 # Get image URL
 image_url = illust_detail.illust.meta_single_page.get('original_image_url',
 illust_detail.illust.image_urls.large)

 # Download Video
 filename = f"{illust_id}.jpg"
 self.pixiv_api.download(image_url, path='.', name=filename)
 return filename

 async def on_ready(self):
 print(f'Login Sucess: {self.user}')

 async def on_message(self, message):
 # Ignore Bot message
 if message.author == self.user:
 return

 # Check Pixiv link
 pixiv_pattern = r'https://www\.pixiv\.net/artworks/\d+'
 urls = re.findall(pixiv_pattern, message.content)

 for url in urls:
 try:
 # download image
 filename = await self.download_pixiv_image(url)

 # upload Discord
 with open(filename, 'rb') as file:
 await message.channel.send(file=discord.File(file, filename))

 # delete image
 os.remove(filename)

 except Exception as e:
 await message.channel.send(f"Download error: {str(e)}")

# ex
bot = PixivBot(PIXIV_REFRESH_TOKEN)
bot.run(DISCORD_TOKEN)``
Xdynix commented 1 day ago

If somehow the illustration ID is invalid or deleted, the response of illust_detail() will looks like:

res = api.illust_detail(9999999999)
print(res)
# {'error': {'user_message': 'Page not found', 'message': '', 'reason': '', 'user_message_details': {}}}

So illust attribute doesn't exist.

You can record the illustration IDs when you encountered any errors and it can help debugging a lot.