Skip to content

Commit 739ee30

Browse files
authored
Merge pull request #79 from SayanthD/staging
might fix #49
2 parents 67cf855 + a63c90d commit 739ee30

15 files changed

+194
-136
lines changed

Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
worker: python3 bot.py
1+
worker: python3 -m anydlbot

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ virtualenv -p python3 VENV
2222
. ./VENV/bin/activate
2323
pip install -r requirements.txt
2424
# <Create config.py with variables as given below>
25-
python bot.py
25+
python3 -m anydlbot
2626
```
2727

2828
An example `config.py` file could be:
2929

3030
**Not All of the variables are mandatory**
3131

3232
```python3
33-
from sample_config import Config
33+
from anydlbot.sample_config import Config
3434

3535
class Development(Config):
3636
APP_ID = 6

anydlbot/__init__.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# (c) Shrimadhav U K
4+
5+
import os
6+
7+
# the secret configuration specific things
8+
if bool(os.environ.get("ENV", False)):
9+
from anydlbot.sample_config import Config
10+
else:
11+
from anydlbot.config import Config
12+
13+
14+
# TODO: is there a better way?
15+
TG_BOT_TOKEN = Config.TG_BOT_TOKEN
16+
APP_ID = Config.APP_ID
17+
API_HASH = Config.API_HASH
18+
AUTH_USERS = Config.AUTH_USERS
19+
DOWNLOAD_LOCATION = Config.DOWNLOAD_LOCATION
20+
MAX_FILE_SIZE = Config.MAX_FILE_SIZE
21+
TG_MAX_FILE_SIZE = Config.TG_MAX_FILE_SIZE
22+
CHUNK_SIZE = Config.CHUNK_SIZE
23+
DEF_THUMB_NAIL_VID_S = Config.DEF_THUMB_NAIL_VID_S
24+
MAX_MESSAGE_LENGTH = Config.MAX_MESSAGE_LENGTH
25+
PROCESS_MAX_TIMEOUT = Config.PROCESS_MAX_TIMEOUT
26+
HTTP_PROXY = Config.HTTP_PROXY
27+
FINISHED_PROGRESS_STR = Config.FINISHED_PROGRESS_STR
28+
UN_FINISHED_PROGRESS_STR = Config.UN_FINISHED_PROGRESS_STR

bot.py anydlbot/__main__.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,30 @@
1111
LOGGER = logging.getLogger(__name__)
1212

1313
import os
14+
from anydlbot import(
15+
APP_ID,
16+
API_HASH,
17+
TG_BOT_TOKEN,
18+
DOWNLOAD_LOCATION
19+
)
1420

15-
# the secret configuration specific things
16-
if bool(os.environ.get("ENV", False)):
17-
from sample_config import Config
18-
else:
19-
from config import Config
20-
21-
import pyrogram
21+
from pyrogram import Client
2222
logging.getLogger("pyrogram").setLevel(logging.WARNING)
2323

2424

2525
if __name__ == "__main__" :
2626
# create download directory, if not exist
27-
if not os.path.isdir(Config.DOWNLOAD_LOCATION):
28-
os.makedirs(Config.DOWNLOAD_LOCATION)
27+
if not os.path.isdir(DOWNLOAD_LOCATION):
28+
os.makedirs(DOWNLOAD_LOCATION)
2929
plugins = dict(
30-
root="plugins"
30+
root="anydlbot/plugins"
3131
)
32-
app = pyrogram.Client(
32+
app = Client(
3333
"AnyDLBot",
34-
bot_token=Config.TG_BOT_TOKEN,
35-
api_id=Config.APP_ID,
36-
api_hash=Config.API_HASH,
34+
bot_token=TG_BOT_TOKEN,
35+
api_id=APP_ID,
36+
api_hash=API_HASH,
3737
plugins=plugins
3838
)
3939
app.set_parse_mode("html")
40-
Config.AUTH_USERS.add(7351948)
4140
app.run()

helper_funcs/display_progress.py anydlbot/helper_funcs/display_progress.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
import os
1515
import time
1616

17-
# the secret configuration specific things
18-
if bool(os.environ.get("ENV", False)):
19-
from sample_config import Config
20-
else:
21-
from config import Config
17+
from anydlbot import(
18+
FINISHED_PROGRESS_STR,
19+
UN_FINISHED_PROGRESS_STR
20+
)
2221

2322
# the Strings used for this "thing"
2423
from translation import Translation
@@ -45,8 +44,8 @@ async def progress_for_pyrogram(
4544
estimated_total_time = TimeFormatter(milliseconds=estimated_total_time)
4645

4746
progress = "[{0}{1}] \nP: {2}%\n".format(
48-
''.join([Config.FINISHED_PROGRESS_STR for i in range(math.floor(percentage / 5))]),
49-
''.join([Config.UN_FINISHED_PROGRESS_STR for i in range(20 - math.floor(percentage / 5))]),
47+
''.join([FINISHED_PROGRESS_STR for i in range(math.floor(percentage / 5))]),
48+
''.join([UN_FINISHED_PROGRESS_STR for i in range(20 - math.floor(percentage / 5))]),
5049
round(percentage, 2))
5150

5251
tmp = progress + "{0} of {1}\nSpeed: {2}/s\nETA: {3}\n".format(
File renamed without changes.
File renamed without changes.

plugins/cb_buttons.py anydlbot/plugins/cb_buttons.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,20 @@
1212

1313
import os
1414

15-
# the secret configuration specific things
16-
if bool(os.environ.get("ENV", False)):
17-
from sample_config import Config
18-
else:
19-
from config import Config
15+
from anydlbot import AUTH_USERS
2016

2117

22-
import pyrogram
18+
from pyrogram import Client
2319
logging.getLogger("pyrogram").setLevel(logging.WARNING)
2420

2521

26-
from plugins.youtube_dl_button import youtube_dl_call_back
27-
from plugins.dl_button import ddl_call_back
22+
from anydlbot.plugins.youtube_dl_button import youtube_dl_call_back
23+
from anydlbot.plugins.dl_button import ddl_call_back
2824

2925

30-
@pyrogram.Client.on_callback_query()
26+
@Client.on_callback_query()
3127
async def button(bot, update):
32-
if update.from_user.id not in Config.AUTH_USERS:
28+
if update.from_user.id not in AUTH_USERS:
3329
await update.message.delete()
3430
return
3531
# LOGGER.info(update)

plugins/custom_thumbnail.py anydlbot/plugins/custom_thumbnail.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,32 @@
1414
from PIL import Image
1515
import time
1616

17-
# the secret configuration specific things
18-
if bool(os.environ.get("ENV", False)):
19-
from sample_config import Config
20-
else:
21-
from config import Config
17+
from anydlbot import(
18+
AUTH_USERS,
19+
DOWNLOAD_LOCATION
20+
)
2221

2322
# the Strings used for this "thing"
2423
from translation import Translation
2524

26-
import pyrogram
25+
from pyrogram import(
26+
Client,
27+
Filters
28+
)
2729
logging.getLogger("pyrogram").setLevel(logging.WARNING)
2830

2931

30-
@pyrogram.Client.on_message(pyrogram.Filters.photo)
32+
@Client.on_message(Filters.photo)
3133
async def save_photo(bot, update):
32-
if update.from_user.id not in Config.AUTH_USERS:
34+
if update.from_user.id not in AUTH_USERS:
3335
await bot.delete_messages(
3436
chat_id=update.chat.id,
3537
message_ids=update.message_id,
3638
revoke=True
3739
)
3840
return
3941
# received single photo
40-
download_location = Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + ".jpg"
42+
download_location = DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + ".jpg"
4143
await bot.download_media(
4244
message=update,
4345
file_name=download_location
@@ -49,16 +51,16 @@ async def save_photo(bot, update):
4951
)
5052

5153

52-
@pyrogram.Client.on_message(pyrogram.Filters.command(["deletethumbnail"]))
54+
@Client.on_message(Filters.command(["deletethumbnail"]))
5355
async def delete_thumbnail(bot, update):
54-
if update.from_user.id not in Config.AUTH_USERS:
56+
if update.from_user.id not in AUTH_USERS:
5557
await bot.delete_messages(
5658
chat_id=update.chat.id,
5759
message_ids=update.message_id,
5860
revoke=True
5961
)
6062
return
61-
download_location = Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id)
63+
download_location = DOWNLOAD_LOCATION + "/" + str(update.from_user.id)
6264
try:
6365
os.remove(download_location + ".jpg")
6466
# os.remove(download_location + ".json")

plugins/dl_button.py anydlbot/plugins/dl_button.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@
1919
import time
2020
from datetime import datetime
2121

22-
# the secret configuration specific things
23-
if bool(os.environ.get("ENV", False)):
24-
from sample_config import Config
25-
else:
26-
from config import Config
22+
from anydlbot import(
23+
DOWNLOAD_LOCATION,
24+
TG_MAX_FILE_SIZE,
25+
PROCESS_MAX_TIMEOUT,
26+
CHUNK_SIZE
27+
)
2728

2829
# the Strings used for this "thing"
2930
from translation import Translation
3031

3132
import pyrogram
3233
logging.getLogger("pyrogram").setLevel(logging.WARNING)
3334

34-
from helper_funcs.display_progress import progress_for_pyrogram, humanbytes, TimeFormatter
35+
from anydlbot.helper_funcs.display_progress import progress_for_pyrogram, humanbytes, TimeFormatter
3536
from hachoir.metadata import extractMetadata
3637
from hachoir.parser import createParser
3738
# https://stackoverflow.com/a/37631799/4723940
@@ -43,7 +44,7 @@ async def ddl_call_back(bot, update):
4344
cb_data = update.data
4445
# youtube_dl extractors
4546
tg_send_type, youtube_dl_format, youtube_dl_ext = cb_data.split("=")
46-
thumb_image_path = Config.DOWNLOAD_LOCATION + \
47+
thumb_image_path = DOWNLOAD_LOCATION + \
4748
"/" + str(update.from_user.id) + ".jpg"
4849
youtube_dl_url = update.message.reply_to_message.text
4950
custom_file_name = os.path.basename(youtube_dl_url)
@@ -82,7 +83,7 @@ async def ddl_call_back(bot, update):
8283
chat_id=update.message.chat.id,
8384
message_id=update.message.message_id
8485
)
85-
tmp_directory_for_each_user = Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id)
86+
tmp_directory_for_each_user = DOWNLOAD_LOCATION + "/" + str(update.from_user.id)
8687
if not os.path.isdir(tmp_directory_for_each_user):
8788
os.makedirs(tmp_directory_for_each_user)
8889
download_directory = tmp_directory_for_each_user + "/" + custom_file_name
@@ -113,14 +114,14 @@ async def ddl_call_back(bot, update):
113114
chat_id=update.message.chat.id,
114115
message_id=update.message.message_id
115116
)
116-
file_size = Config.TG_MAX_FILE_SIZE + 1
117+
file_size = TG_MAX_FILE_SIZE + 1
117118
try:
118119
file_size = os.stat(download_directory).st_size
119120
except FileNotFoundError as exc:
120121
download_directory = os.path.splitext(download_directory)[0] + "." + "mkv"
121122
# https://stackoverflow.com/a/678242/4723940
122123
file_size = os.stat(download_directory).st_size
123-
if file_size > Config.TG_MAX_FILE_SIZE:
124+
if file_size > TG_MAX_FILE_SIZE:
124125
await bot.edit_message_text(
125126
chat_id=update.message.chat.id,
126127
text=Translation.RCHD_TG_API_LIMIT,
@@ -261,7 +262,7 @@ async def ddl_call_back(bot, update):
261262
async def download_coroutine(bot, session, url, file_name, chat_id, message_id, start):
262263
downloaded = 0
263264
display_message = ""
264-
async with session.get(url, timeout=Config.PROCESS_MAX_TIMEOUT) as response:
265+
async with session.get(url, timeout=PROCESS_MAX_TIMEOUT) as response:
265266
total_length = int(response.headers["Content-Length"])
266267
content_type = response.headers["Content-Type"]
267268
if "text" in content_type and total_length < 500:
@@ -275,11 +276,11 @@ async def download_coroutine(bot, session, url, file_name, chat_id, message_id,
275276
)
276277
with open(file_name, "wb") as f_handle:
277278
while True:
278-
chunk = await response.content.read(Config.CHUNK_SIZE)
279+
chunk = await response.content.read(CHUNK_SIZE)
279280
if not chunk:
280281
break
281282
f_handle.write(chunk)
282-
downloaded += Config.CHUNK_SIZE
283+
downloaded += CHUNK_SIZE
283284
now = time.time()
284285
diff = now - start
285286
if round(diff % 5.00) == 0 or downloaded == total_length:

plugins/download_stickers.py anydlbot/plugins/download_stickers.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,31 @@
1313
import os
1414
import time
1515

16-
# the secret configuration specific things
17-
if bool(os.environ.get("ENV", False)):
18-
from sample_config import Config
19-
else:
20-
from config import Config
16+
from anydlbot import(
17+
AUTH_USERS,
18+
DOWNLOAD_LOCATION
19+
)
2120

2221
# the Strings used for this "thing"
2322
from translation import Translation
2423

25-
import pyrogram
24+
from pyrogram import(
25+
Client,
26+
Filters
27+
)
2628
logging.getLogger("pyrogram").setLevel(logging.WARNING)
2729

28-
from helper_funcs.display_progress import progress_for_pyrogram
30+
from anydlbot.helper_funcs.display_progress import progress_for_pyrogram
2931

3032

31-
@pyrogram.Client.on_message(pyrogram.Filters.sticker)
33+
@Client.on_message(Filters.sticker)
3234
async def DownloadStickersBot(bot, update):
33-
if update.from_user.id not in Config.AUTH_USERS:
35+
if update.from_user.id not in AUTH_USERS:
3436
await update.delete()
3537
return
3638

3739
LOGGER.info(update.from_user)
38-
download_location = Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + "_DownloadStickersBot_" + str(update.from_user.id) + ".png"
40+
download_location = DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + "_DownloadStickersBot_" + str(update.from_user.id) + ".png"
3941
a = await bot.send_message(
4042
chat_id=update.chat.id,
4143
text=Translation.DOWNLOAD_START,

0 commit comments

Comments
 (0)