Commit ·
d3fb920
1
Parent(s): f1bbba1
initial commit
Browse files- aria.py +10 -42
- bot.py +11 -92
- bot_commands.py +0 -60
- bot_factory.py +0 -13
aria.py
CHANGED
|
@@ -1,52 +1,20 @@
|
|
| 1 |
import aria2p
|
| 2 |
import logging
|
| 3 |
-
import requests
|
| 4 |
|
|
|
|
|
|
|
| 5 |
logger = logging.getLogger(__name__)
|
| 6 |
|
| 7 |
-
class
|
| 8 |
def __init__(self, host="http://localhost", port=6800, secret=""):
|
| 9 |
-
self.
|
| 10 |
-
|
| 11 |
-
host=host,
|
| 12 |
-
port=port,
|
| 13 |
-
secret=secret
|
| 14 |
-
)
|
| 15 |
-
)
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
return self.api.add_magnet(link, {'dir': path})
|
| 19 |
-
|
| 20 |
-
def add_uris(self, link: str, path: str):
|
| 21 |
-
return self.api.add_uris([link], {'dir': path})
|
| 22 |
-
|
| 23 |
-
def get_download(self, gid: str):
|
| 24 |
-
download = self.api.get_download(gid)
|
| 25 |
-
logger.info(f"Download status: {download.status}")
|
| 26 |
-
return download
|
| 27 |
-
|
| 28 |
-
def check_url(self, link: str):
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
logger.error(f"
|
| 34 |
-
return False
|
| 35 |
-
|
| 36 |
-
class Aria2Downloader:
|
| 37 |
-
def __init__(self, client):
|
| 38 |
-
self.client = client
|
| 39 |
-
|
| 40 |
-
def add_download(self, link, path):
|
| 41 |
-
if not self.client.check_url(link):
|
| 42 |
-
logger.error(f"Invalid URL: {link}")
|
| 43 |
return None
|
| 44 |
-
|
| 45 |
-
if link.startswith("magnet:"):
|
| 46 |
-
return self.client.add_magnet(link, path)
|
| 47 |
-
else:
|
| 48 |
-
return self.client.add_uris(link, path)
|
| 49 |
-
|
| 50 |
-
def get_download_path(self, gid):
|
| 51 |
-
download = self.client.get_download(gid)
|
| 52 |
-
return download.dir, download.name
|
|
|
|
| 1 |
import aria2p
|
| 2 |
import logging
|
|
|
|
| 3 |
|
| 4 |
+
# Configure logging
|
| 5 |
+
logging.basicConfig(level=logging.INFO)
|
| 6 |
logger = logging.getLogger(__name__)
|
| 7 |
|
| 8 |
+
class Aria:
|
| 9 |
def __init__(self, host="http://localhost", port=6800, secret=""):
|
| 10 |
+
self.aria2 = aria2p.API(aria2p.Client(host=host, port=port, secret=secret))
|
| 11 |
+
logger.info("Aria2 client initialized")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def add_download(self, url):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
+
download = self.aria2.add_uris([url])
|
| 16 |
+
logger.info(f"Download started: {url}")
|
| 17 |
+
return download
|
| 18 |
except Exception as e:
|
| 19 |
+
logger.error(f"Failed to start download: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot.py
CHANGED
|
@@ -8,6 +8,7 @@ import aria2p
|
|
| 8 |
import asyncio
|
| 9 |
import tempfile
|
| 10 |
import shutil
|
|
|
|
| 11 |
|
| 12 |
# Configure logging
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -20,6 +21,7 @@ API_HASH = os.environ.get("API_HASH", "5243628014db9798d6775b330e542602")
|
|
| 20 |
TOKEN = os.environ.get("TOKEN", "6520286363:AAEe0c5v_JQf8ztatUx8ZeO_ARXrJkYsluI")
|
| 21 |
CHAT_ID = os.environ.get("CHAT_ID", "1487943213") # Add your chat ID here
|
| 22 |
|
|
|
|
| 23 |
|
| 24 |
async def clientbot():
|
| 25 |
bot = Client(":memory:", api_id=API_ID, api_hash=API_HASH, bot_token=TOKEN)
|
|
@@ -29,101 +31,18 @@ async def clientbot():
|
|
| 29 |
await message.reply_text("Bot has started!")
|
| 30 |
logger.info("Start command received and responded")
|
| 31 |
|
| 32 |
-
@bot.on_message(filters.command("
|
| 33 |
-
async def
|
| 34 |
-
if len(message.command) <
|
| 35 |
-
await message.reply_text("
|
| 36 |
return
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
await
|
| 42 |
-
await message.reply_text("Message sent successfully")
|
| 43 |
-
logger.info(f"Message sent to {chat_id}: {msg}")
|
| 44 |
-
except Exception as e:
|
| 45 |
-
await message.reply_text(f"Failed to send message: {str(e)}")
|
| 46 |
-
logger.error(f"Failed to send message: {str(e)}")
|
| 47 |
-
|
| 48 |
-
@bot.on_message(filters.command("id"))
|
| 49 |
-
async def id_command(client, message):
|
| 50 |
-
user = None
|
| 51 |
-
user_info = None
|
| 52 |
-
|
| 53 |
-
if message.reply_to_message:
|
| 54 |
-
if (
|
| 55 |
-
hasattr(message.reply_to_message, "forward_origin")
|
| 56 |
-
and message.reply_to_message.forward_origin
|
| 57 |
-
):
|
| 58 |
-
if (
|
| 59 |
-
hasattr(message.reply_to_message.forward_origin, "sender_user")
|
| 60 |
-
and message.reply_to_message.forward_origin.sender_user
|
| 61 |
-
):
|
| 62 |
-
user = message.reply_to_message.forward_origin.sender_user
|
| 63 |
-
elif (
|
| 64 |
-
hasattr(message.reply_to_message.forward_origin, "sender_chat")
|
| 65 |
-
and message.reply_to_message.forward_origin.sender_chat
|
| 66 |
-
):
|
| 67 |
-
user_info = {
|
| 68 |
-
"id": message.reply_to_message.forward_origin.sender_chat.id,
|
| 69 |
-
"name": message.reply_to_message.forward_origin.sender_chat.title,
|
| 70 |
-
"type": "channel",
|
| 71 |
-
"link": f"https://t.me/{message.reply_to_message.forward_origin.sender_chat.username}/{message.reply_to_message.id}",
|
| 72 |
-
"username": message.reply_to_message.forward_origin.sender_chat.username,
|
| 73 |
-
}
|
| 74 |
-
else:
|
| 75 |
-
user = message.reply_to_message.from_user
|
| 76 |
-
elif hasattr(message, "forward_origin") and message.forward_origin:
|
| 77 |
-
if (
|
| 78 |
-
hasattr(message.forward_origin, "sender_user")
|
| 79 |
-
and message.forward_origin.sender_user
|
| 80 |
-
):
|
| 81 |
-
user = message.forward_origin.sender_user
|
| 82 |
-
elif (
|
| 83 |
-
hasattr(message.forward_origin, "sender_chat")
|
| 84 |
-
and message.forward_origin.sender_chat
|
| 85 |
-
):
|
| 86 |
-
user_info = {
|
| 87 |
-
"id": message.forward_origin.sender_chat.id,
|
| 88 |
-
"name": message.forward_origin.sender_chat.title,
|
| 89 |
-
"type": "channel",
|
| 90 |
-
"link": f"https://t.me/{message.forward_origin.sender_chat.username}/{message.id}",
|
| 91 |
-
"username": message.forward_origin.sender_chat.username,
|
| 92 |
-
}
|
| 93 |
-
else:
|
| 94 |
-
user = message.from_user
|
| 95 |
-
|
| 96 |
-
if user:
|
| 97 |
-
user_id = user.id
|
| 98 |
-
user_name = user.first_name
|
| 99 |
-
if user.last_name:
|
| 100 |
-
user_name += f" {user.last_name}"
|
| 101 |
-
profile_link = f"tg://user?id={user_id}"
|
| 102 |
-
user_tag = (
|
| 103 |
-
f"@{user.username}" if user.username else "User has not set a user tag."
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
response = (
|
| 107 |
-
f"➲ **Username:** [{user_name}]({profile_link})\n"
|
| 108 |
-
f"➲ **Usertag:** {user_tag}\n"
|
| 109 |
-
f"➲ **Telegram ID:** `<code>{user_id}</code>`"
|
| 110 |
-
)
|
| 111 |
-
elif user_info:
|
| 112 |
-
user_tag = (
|
| 113 |
-
f"@{user_info['username']}"
|
| 114 |
-
if user_info["username"]
|
| 115 |
-
else "Channel has not set a user tag"
|
| 116 |
-
)
|
| 117 |
-
response = (
|
| 118 |
-
f"➲ **Channel:** [{user_info['name']}]({user_info['link']})\n"
|
| 119 |
-
f"➲ **Usertag:** {user_tag}\n"
|
| 120 |
-
f"➲ **Telegram ID:** `<code>{user_info['id']}</code>`"
|
| 121 |
-
)
|
| 122 |
else:
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
await message.reply_text(response, disable_web_page_preview=True)
|
| 126 |
-
logger.info(f"User info requested: {response}")
|
| 127 |
|
| 128 |
await bot.start()
|
| 129 |
return bot
|
|
|
|
| 8 |
import asyncio
|
| 9 |
import tempfile
|
| 10 |
import shutil
|
| 11 |
+
from aria import Aria
|
| 12 |
|
| 13 |
# Configure logging
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 21 |
TOKEN = os.environ.get("TOKEN", "6520286363:AAEe0c5v_JQf8ztatUx8ZeO_ARXrJkYsluI")
|
| 22 |
CHAT_ID = os.environ.get("CHAT_ID", "1487943213") # Add your chat ID here
|
| 23 |
|
| 24 |
+
aria2 = Aria()
|
| 25 |
|
| 26 |
async def clientbot():
|
| 27 |
bot = Client(":memory:", api_id=API_ID, api_hash=API_HASH, bot_token=TOKEN)
|
|
|
|
| 31 |
await message.reply_text("Bot has started!")
|
| 32 |
logger.info("Start command received and responded")
|
| 33 |
|
| 34 |
+
@bot.on_message(filters.command("download"))
|
| 35 |
+
async def download_command(client, message):
|
| 36 |
+
if len(message.command) < 2:
|
| 37 |
+
await message.reply_text("Please provide a URL to download.")
|
| 38 |
return
|
| 39 |
|
| 40 |
+
url = message.command[1]
|
| 41 |
+
download = aria2.add_download(url)
|
| 42 |
+
if download:
|
| 43 |
+
await message.reply_text(f"Download started: {url}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
+
await message.reply_text("Failed to start download.")
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
await bot.start()
|
| 48 |
return bot
|
bot_commands.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
| 1 |
-
import logging
|
| 2 |
-
import asyncio
|
| 3 |
-
from pyrogram import filters
|
| 4 |
-
from config import Config
|
| 5 |
-
from aria import Aria2Client, Aria2Downloader
|
| 6 |
-
|
| 7 |
-
logger = logging.getLogger(__name__)
|
| 8 |
-
|
| 9 |
-
class BotCommands:
|
| 10 |
-
def __init__(self, bot):
|
| 11 |
-
self.bot = bot
|
| 12 |
-
self.aria2_downloader = Aria2Downloader(Aria2Client())
|
| 13 |
-
self.register_commands()
|
| 14 |
-
|
| 15 |
-
def register_commands(self):
|
| 16 |
-
self.bot.on_message(filters.command("start"))(self.start_command)
|
| 17 |
-
self.bot.on_message(filters.command("aria"))(self.aria_command)
|
| 18 |
-
|
| 19 |
-
async def start_command(self, client, message):
|
| 20 |
-
await message.reply_text("Bot has started!")
|
| 21 |
-
logger.info("Start command received and responded")
|
| 22 |
-
|
| 23 |
-
async def aria_command(self, client, message):
|
| 24 |
-
if len(message.command) < 2:
|
| 25 |
-
await message.reply_text("Usage: /aria <link>")
|
| 26 |
-
return
|
| 27 |
-
|
| 28 |
-
link = message.command[1]
|
| 29 |
-
download = self.aria2_downloader.add_download(link, "./downloads")
|
| 30 |
-
|
| 31 |
-
if download:
|
| 32 |
-
await message.reply_text(f"Download started: {download.gid}\nDIR: downloads")
|
| 33 |
-
logger.info(f"Download started: {download.gid}")
|
| 34 |
-
|
| 35 |
-
# Periodically check the status of the download
|
| 36 |
-
while True:
|
| 37 |
-
await asyncio.sleep(5) # Check every 5 seconds
|
| 38 |
-
download_status = self.aria2_downloader.client.get_download(download.gid)
|
| 39 |
-
logger.info(f"Checking download status: {download_status.status}")
|
| 40 |
-
if download_status.status == "complete":
|
| 41 |
-
download_path, download_name = self.aria2_downloader.get_download_path(download.gid)
|
| 42 |
-
await message.reply_text(f"Download complete: {download_name}\nPath: {download_path}")
|
| 43 |
-
logger.info(f"Download complete: {download.gid} Path: {download_path}")
|
| 44 |
-
break
|
| 45 |
-
elif download_status.status == "error":
|
| 46 |
-
error_message = download_status.error_message
|
| 47 |
-
await message.reply_text(f"Download failed: {error_message}")
|
| 48 |
-
logger.error(f"Download failed: {download.gid} Error: {error_message}")
|
| 49 |
-
break
|
| 50 |
-
elif download_status.status == "removed":
|
| 51 |
-
await message.reply_text("Download was removed.")
|
| 52 |
-
logger.error(f"Download removed: {download.gid}")
|
| 53 |
-
break
|
| 54 |
-
elif download_status.status == "paused":
|
| 55 |
-
await message.reply_text("Download paused.")
|
| 56 |
-
logger.info(f"Download paused: {download.gid}")
|
| 57 |
-
break
|
| 58 |
-
else:
|
| 59 |
-
await message.reply_text("Failed to start download. Invalid URL or other error.")
|
| 60 |
-
logger.error("Failed to start download.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot_factory.py
DELETED
|
@@ -1,13 +0,0 @@
|
|
| 1 |
-
from pyrogram import Client
|
| 2 |
-
from config import Config
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
class BotFactory:
|
| 6 |
-
@staticmethod
|
| 7 |
-
def create_bot():
|
| 8 |
-
return Client(
|
| 9 |
-
":memory:",
|
| 10 |
-
api_id=Config.API_ID,
|
| 11 |
-
api_hash=Config.API_HASH,
|
| 12 |
-
bot_token=Config.TOKEN,
|
| 13 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|