Skip to content

Commit

Permalink
feat(torrent_creator.py): add torrent creation functionality
Browse files Browse the repository at this point in the history
feat(TORRENT_CREATOR.json): add qbittorrent api connection info
feat(.gitignore): add temp and torrents to gitignore
feat(requirements.txt): add qbittorrent-api, py3createtorrent, requests to dependencies
  • Loading branch information
ToasterUwU committed Oct 2, 2023
1 parent 299ad36 commit 1b7ee1a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ __pycache__
/build
/windows_installer.exe
/data
/config/*.json
/config/*.json
temp
torrents
105 changes: 105 additions & 0 deletions cogs/torrent_creator.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,124 @@
import asyncio
import io
import os
import subprocess
import aiofiles
import aiohttp

import nextcord
import qbittorrentapi
from nextcord.ext import commands
import requests

from internal_tools.configuration import CONFIG
from internal_tools.discord import *


class TorrentCreator(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot

self.qbt_api = qbittorrentapi.Client(
**CONFIG["TORRENT_CREATOR"]["QBT_API_CONN_INFO"]
)

async def cog_application_command_check(self, interaction: nextcord.Interaction):
"""
Everyone can use this.
"""
return True

def is_valid_file_url(self, file_url: str):
try:
response = requests.head(file_url)
if response.status_code == 200:
filename = file_url.rsplit("/", 1)[1]
if "?" in filename:
filename = filename.split("?", 1)[0]

if "." in filename:
return True

except Exception:
pass

return False

async def create_torrent(self, file_url: str):
if not os.path.isdir("./temp"):
os.mkdir("temp")

if not os.path.isdir("./torrents"):
os.mkdir("torrents")

if "Birb Enjoyer" not in self.qbt_api.torrent_tags.tags:
self.qbt_api.torrents_create_tags("Birb Enjoyer")

filename = file_url.rsplit("/", 1)[1]
if "?" in filename:
filename = filename.split("?", 1)[0]

async with aiohttp.ClientSession() as session:
async with session.get(file_url) as resp:
async with aiofiles.open(
"./temp/" + filename,
mode="wb+",
) as f:
await f.write(await resp.read())

await asyncio.get_running_loop().run_in_executor(
None,
lambda: subprocess.Popen(
f'./.venv/bin/py3createtorrent ./temp/{filename} -o torrents/ --quiet --node router.bittorrent.com,8991 -p 512 -c "Created by Birb Enjoyer" -f --md5 --webseed {file_url}',
shell=True,
).communicate(),
)

async with aiofiles.open(
"./temp/" + filename,
mode="rb",
) as src:
async with aiofiles.open(
"/mnt/home/Drive/torrents/data/" + filename,
mode="wb+",
) as dst:
await dst.write(await src.read())

os.remove(f"./temp/{filename}")

async with aiofiles.open(
f"./torrents/{filename}.torrent",
mode="rb",
) as f:
torrent_file_data = await f.read()

os.remove(f"./torrents/{filename}.torrent")

self.qbt_api.torrents_add(
torrent_files=torrent_file_data,
tags="Birb Enjoyer",
seeding_time_limit=10080,
)

return nextcord.File(io.BytesIO(torrent_file_data), f"{filename}.torrent")

@nextcord.slash_command(
"create-torrent",
description="Takes a URL to a file, downloads it, makes a Torrent from it, returns you the .torrent file.",
dm_permission=False
default_member_permissions=nextcord.Permissions(administrator=True)
)
async def create_torrent_command(
self, interaction: nextcord.Interaction, file_url: str
):
if not self.is_valid_file_url(file_url):
await interaction.send("Thats not a valid file URL", ephemeral=True)
return

await interaction.response.defer()

torrent_file = await self.create_torrent(file_url)
await interaction.send(file=torrent_file)

async def setup(bot):
bot.add_cog(TorrentCreator(bot))
8 changes: 8 additions & 0 deletions config/default/TORRENT_CREATOR.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"QBT_API_CONN_INFO": {
"host": "localhost",
"port": 8080,
"username": "admin",
"password": "adminadmin"
}
}
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
nextcord[speed, voice] == 2.6.*
orjson == 3.9.*
aiohttp == 3.8.*
aiofiles == 23.2.*
aiofiles == 23.2.*
qbittorrent-api == 2023.9.53
py3createtorrent == 1.1.*
requests == 2.31.*

0 comments on commit 1b7ee1a

Please sign in to comment.