synodriver / aioqbittorrent

Qbittorrent rpc with asyncio
4 stars 1 forks source link

AttributeError: 'bytes' object has no attribute 'name' #2

Open vvaarriiuumm opened 1 month ago

vvaarriiuumm commented 1 month ago

When calling torrents_add function and passing a torrent file in bytes, I get this error. This is being implemented into a discord bot hence the discord.py code. The commented code in the try except block was to convert the .torrent file into a magnet link because of this issue, but at some point the metadata gets deleted and it doesn't work with private trackers.

Code:

    @torrent.command(name="file", description="Upload a file to be torrented on the 411 seedbox.")
    @commands.has_role(1213818122973487134)
    async def file(self, ctx: Context, file: discord.Attachment):
        filename = file.filename
        await file.save(fp=f"torrents/{filename}")
        try:
            message = await ctx.reply("Attempting to start torrent...")

            # torrent_data = await file.read()
            # decoded_torrent = bencodepy.decode(torrent_data)
            # info_dict = decoded_torrent[b'info']
            # encoded_info = bencodepy.encode(info_dict)
            # sha1 = hashlib.sha1()
            # sha1.update(encoded_info)
            # magnet = f"magnet:?xt=urn:btih:{sha1.hexdigest()}"

            async with aiofiles.open(file=f"/root/411-bot/torrents/{filename}", mode="rb") as f:
                f = await f.read()

            async with Client(url="fakeurl", username="fakeuser", password="fakepass") as client:
                await client.auth_login()
                if await client.torrents_add(torrents=[f], tags=[ctx.author.name]) == "Ok.":
                    embed = discord.Embed(description="<a:blackverified:1281922160797024309> Torrent added successfully.", color=0x000000)
                    await message.edit(content=None, embed=embed)
                else:
                    embed = discord.Embed(description="<:error:1288663298765623360> Failed to add torrent. Only .torrent files are accepted.", color=0xFF0000)
                    await message.edit(content=None, embed=embed)
                await client.auth_logout()
        except Exception:
            embed = discord.Embed(description=f"{error_emoji} {traceback.format_exc()}", color=0xFF0000)
            await ctx.reply(embed=embed)
synodriver commented 2 weeks ago

Hi. Can you provide a full traceback?

synodriver commented 2 weeks ago

Ok, I see. Could you try wrap your bytes data in BytesIO?