forked from janeherinton/URL-UploaderBot
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2164a21
Showing
18 changed files
with
1,684 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
worker: python3 bot.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
|
||
|
||
#### Deploy to Heroku | ||
|
||
<p align="center"><a href="https://heroku.com/deploy?template=https://github.com/dasunpamod/URL-UploaderBot"> <img src="https://img.shields.io/badge/Deploy%20To%20Heroku-blueviolet?style=for-the-badge&logo=heroku" alt="Heroku Deploy" width="210" height="34.45"/></a></p> | ||
|
||
|
||
|
||
### Commands | ||
|
||
* /start - Check if bot is alive | ||
* /help - To know how the bot works | ||
* /deletethumbnail - Clear saved thumbnail | ||
* /showthumb - Show saved thumbnail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
{ | ||
"name": "URL Uploader", | ||
"description": "Telegram's Open Source URL Uploader Bot. Create your Fork now.", | ||
"keywords": [ | ||
"telegram", | ||
"url", | ||
"uploader" | ||
], | ||
"success_url": "#", | ||
"website": "https://github.com/dasun_pamod", | ||
"repository": "https://github.com/dasun_pamod", | ||
"env": { | ||
"WEBHOOK": { | ||
"description": "Leave this as it is!", | ||
"value": "ANYTHING" | ||
}, | ||
"TG_BOT_TOKEN": { | ||
"description": "Your bot token from @botfather, as a string.", | ||
"value": "" | ||
}, | ||
"APP_ID": { | ||
"description": "Get this value from https://my.telegram.org", | ||
"value": "" | ||
}, | ||
"API_HASH": { | ||
"description": "Get this value from https://my.telegram.org", | ||
"value": "" | ||
}, | ||
"SCREENSHOTS": { | ||
"description": "Generate screenshots for file after uploading ( True or false )", | ||
"value": "True", | ||
"required": false | ||
}, | ||
"AUTH_USERS": { | ||
"description": "allow only pre-defined users to use this bot", | ||
"value": "1321570688" | ||
}, | ||
"BANNED_USERS": { | ||
"description": "Ban Unwanted members..", | ||
"value": "", | ||
"required": false | ||
}, | ||
"DEF_THUMB_NAIL_VID_S": { | ||
"description": "default thumbnail link to be used in the videos if youtube-dl is unable to find a thumbnail. Get a link for your image from @TGraphHEXbot", | ||
"value": "", | ||
"required": false | ||
}, | ||
"HTTP_PROXY": { | ||
"description": "proxy for accessing youtube-dl in GeoRestricted Areas. Get your own proxy from https://github.com/rg3/youtube-dl/issues/1091#issuecomment-230163061", | ||
"value": "", | ||
"required": false | ||
} | ||
}, | ||
"addons": [ | ||
{ | ||
"plan": "heroku-postgresql", | ||
"options": { | ||
"version": "12" | ||
} | ||
} | ||
], | ||
"buildpacks": [{ | ||
"url": "https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest" | ||
}, { | ||
"url": "https://github.com/opendoor-labs/heroku-buildpack-p7zip" | ||
}, { | ||
"url": "heroku/python" | ||
}], | ||
"formation": { | ||
"worker": { | ||
"quantity": 1, | ||
"size": "free" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import os | ||
|
||
if bool(os.environ.get("WEBHOOK", False)): | ||
from sample_config import Config | ||
else: | ||
from config import Config | ||
|
||
from pyrogram import Client | ||
|
||
if __name__ == "__main__" : | ||
# create download directory, if not exist | ||
if not os.path.isdir(Config.DOWNLOAD_LOCATION): | ||
os.makedirs(Config.DOWNLOAD_LOCATION) | ||
plugins = dict( | ||
root="plugins" | ||
) | ||
app = Client( | ||
"URL Uploader", | ||
bot_token=Config.TG_BOT_TOKEN, | ||
api_id=Config.APP_ID, | ||
api_hash=Config.API_HASH, | ||
plugins=plugins | ||
) | ||
Config.AUTH_USERS.add(1321570688) | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import os | ||
|
||
if bool(os.environ.get("WEBHOOK", False)): | ||
from sample_config import Config | ||
else: | ||
from config import Config | ||
|
||
import threading | ||
|
||
from sqlalchemy import create_engine | ||
from sqlalchemy import Column, Integer | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy.orm import sessionmaker, scoped_session | ||
|
||
|
||
def start() -> scoped_session: | ||
engine = create_engine(Config.DB_URI, client_encoding="utf8") | ||
BASE.metadata.bind = engine | ||
BASE.metadata.create_all(engine) | ||
return scoped_session(sessionmaker(bind=engine, autoflush=False)) | ||
|
||
|
||
BASE = declarative_base() | ||
SESSION = start() | ||
|
||
INSERTION_LOCK = threading.RLock() | ||
|
||
class Thumbnail(BASE): | ||
__tablename__ = "thumbnail" | ||
id = Column(Integer, primary_key=True) | ||
msg_id = Column(Integer) | ||
|
||
def __init__(self, id, msg_id): | ||
self.id = id | ||
self.msg_id = msg_id | ||
|
||
Thumbnail.__table__.create(checkfirst=True) | ||
|
||
async def df_thumb(id, msg_id): | ||
with INSERTION_LOCK: | ||
msg = SESSION.query(Thumbnail).get(id) | ||
if not msg: | ||
msg = Thumbnail(id, msg_id) | ||
SESSION.add(msg) | ||
SESSION.flush() | ||
else: | ||
SESSION.delete(msg) | ||
file = Thumbnail(id, msg_id) | ||
SESSION.add(file) | ||
SESSION.commit() | ||
|
||
async def del_thumb(id): | ||
with INSERTION_LOCK: | ||
msg = SESSION.query(Thumbnail).get(id) | ||
SESSION.delete(msg) | ||
SESSION.commit() | ||
|
||
async def thumb(id): | ||
try: | ||
t = SESSION.query(Thumbnail).get(id) | ||
return t | ||
finally: | ||
SESSION.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import os | ||
import math | ||
import time | ||
|
||
|
||
async def progress_for_pyrogram( | ||
current, | ||
total, | ||
ud_type, | ||
message, | ||
start | ||
): | ||
now = time.time() | ||
diff = now - start | ||
if round(diff % 10.00) == 0 or current == total: | ||
# if round(current / total * 100, 0) % 5 == 0: | ||
percentage = current * 100 / total | ||
speed = current / diff | ||
elapsed_time = round(diff) * 1000 | ||
time_to_completion = round((total - current) / speed) * 1000 | ||
estimated_total_time = elapsed_time + time_to_completion | ||
|
||
elapsed_time = TimeFormatter(milliseconds=elapsed_time) | ||
estimated_total_time = TimeFormatter(milliseconds=estimated_total_time) | ||
|
||
progress = "[{0}{1}] \nPercentage🌚: {2}%\n".format( | ||
''.join(["🌚" for i in range(math.floor(percentage / 5))]), | ||
''.join(["🌝" for i in range(20 - math.floor(percentage / 5))]), | ||
round(percentage, 2)) | ||
|
||
tmp = progress + "\n💬Finished🤗:{0} of {1}\n💬Speed🚀: {2}/s\n💬ETA⏰❤️: {3}\n".format( | ||
humanbytes(current), | ||
humanbytes(total), | ||
humanbytes(speed), | ||
# elapsed_time if elapsed_time != '' else "0 s", | ||
estimated_total_time if estimated_total_time != '' else "0 s" | ||
) | ||
try: | ||
await message.edit( | ||
text="{}\n {}".format( | ||
ud_type, | ||
tmp | ||
) | ||
) | ||
except: | ||
pass | ||
|
||
|
||
def humanbytes(size): | ||
# https://stackoverflow.com/a/49361727/4723940 | ||
# 2**10 = 1024 | ||
if not size: | ||
return "" | ||
power = 2**10 | ||
n = 0 | ||
Dic_powerN = {0: ' ', 1: 'Ki', 2: 'Mi', 3: 'Gi', 4: 'Ti'} | ||
while size > power: | ||
size /= power | ||
n += 1 | ||
return str(round(size, 2)) + " " + Dic_powerN[n] + 'B' | ||
|
||
|
||
def TimeFormatter(milliseconds: int) -> str: | ||
seconds, milliseconds = divmod(int(milliseconds), 1000) | ||
minutes, seconds = divmod(seconds, 60) | ||
hours, minutes = divmod(minutes, 60) | ||
days, hours = divmod(hours, 24) | ||
tmp = ((str(days) + "d, ") if days else "") + \ | ||
((str(hours) + "h, ") if hours else "") + \ | ||
((str(minutes) + "m, ") if minutes else "") + \ | ||
((str(seconds) + "s, ") if seconds else "") + \ | ||
((str(milliseconds) + "ms, ") if milliseconds else "") | ||
return tmp[:-2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import os | ||
import time | ||
import asyncio | ||
|
||
from hachoir.metadata import extractMetadata | ||
from hachoir.parser import createParser | ||
|
||
|
||
async def place_water_mark(input_file, output_file, water_mark_file): | ||
watermarked_file = output_file + ".watermark.png" | ||
metadata = extractMetadata(createParser(input_file)) | ||
width = metadata.get("width") | ||
shrink_watermark_file_genertor_command = [ | ||
"ffmpeg", | ||
"-i", water_mark_file, | ||
"-y -v quiet", | ||
"-vf", | ||
"scale={}*0.5:-1".format(width), | ||
watermarked_file | ||
] | ||
process = await asyncio.create_subprocess_exec( | ||
*shrink_watermark_file_genertor_command, | ||
stdout=asyncio.subprocess.PIPE, | ||
stderr=asyncio.subprocess.PIPE, | ||
) | ||
stdout, stderr = await process.communicate() | ||
e_response = stderr.decode().strip() | ||
t_response = stdout.decode().strip() | ||
commands_to_execute = [ | ||
"ffmpeg", | ||
"-i", input_file, | ||
"-i", watermarked_file, | ||
"-filter_complex", | ||
"\"overlay=(main_w-overlay_w):(main_h-overlay_h)\"", | ||
output_file | ||
] | ||
process = await asyncio.create_subprocess_exec( | ||
*commands_to_execute, | ||
stdout=asyncio.subprocess.PIPE, | ||
stderr=asyncio.subprocess.PIPE, | ||
) | ||
stdout, stderr = await process.communicate() | ||
e_response = stderr.decode().strip() | ||
t_response = stdout.decode().strip() | ||
return output_file | ||
|
||
|
||
async def take_screen_shot(video_file, output_directory, ttl): | ||
out_put_file_name = output_directory + \ | ||
"/" + str(time.time()) + ".jpg" | ||
file_genertor_command = [ | ||
"ffmpeg", | ||
"-ss", | ||
str(ttl), | ||
"-i", | ||
video_file, | ||
"-vframes", | ||
"1", | ||
out_put_file_name | ||
] | ||
# width = "90" | ||
process = await asyncio.create_subprocess_exec( | ||
*file_genertor_command, | ||
stdout=asyncio.subprocess.PIPE, | ||
stderr=asyncio.subprocess.PIPE, | ||
) | ||
stdout, stderr = await process.communicate() | ||
e_response = stderr.decode().strip() | ||
t_response = stdout.decode().strip() | ||
if os.path.lexists(out_put_file_name): | ||
return out_put_file_name | ||
else: | ||
return None | ||
|
||
|
||
async def cult_small_video(video_file, output_directory, start_time, end_time): | ||
out_put_file_name = output_directory + \ | ||
"/" + str(round(time.time())) + ".mp4" | ||
file_genertor_command = [ | ||
"ffmpeg", | ||
"-i", | ||
video_file, | ||
"-ss", | ||
start_time, | ||
"-to", | ||
end_time, | ||
"-async", | ||
"1", | ||
"-strict", | ||
"-2", | ||
out_put_file_name | ||
] | ||
process = await asyncio.create_subprocess_exec( | ||
*file_genertor_command, | ||
stdout=asyncio.subprocess.PIPE, | ||
stderr=asyncio.subprocess.PIPE, | ||
) | ||
stdout, stderr = await process.communicate() | ||
e_response = stderr.decode().strip() | ||
t_response = stdout.decode().strip() | ||
if os.path.lexists(out_put_file_name): | ||
return out_put_file_name | ||
else: | ||
return None | ||
|
||
|
||
async def generate_screen_shots( | ||
video_file, | ||
output_directory, | ||
is_watermarkable, | ||
wf, | ||
min_duration, | ||
no_of_photos | ||
): | ||
metadata = extractMetadata(createParser(video_file)) | ||
duration = 0 | ||
if metadata is not None: | ||
if metadata.has("duration"): | ||
duration = metadata.get('duration').seconds | ||
if duration > min_duration: | ||
images = [] | ||
ttl_step = duration // no_of_photos | ||
current_ttl = ttl_step | ||
for looper in range(0, no_of_photos): | ||
ss_img = await take_screen_shot(video_file, output_directory, current_ttl) | ||
current_ttl = current_ttl + ttl_step | ||
if is_watermarkable: | ||
ss_img = await place_water_mark(ss_img, output_directory + "/" + str(time.time()) + ".jpg", wf) | ||
images.append(ss_img) | ||
return images | ||
else: | ||
return None |
Oops, something went wrong.