Skip to content

Commit

Permalink
add txt downlaods and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bipinkrish committed Jun 15, 2024
1 parent 221445d commit 4a84a62
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 65 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ tgd
tgd.exe
tgd.spec
build/*
dist/*
dist/*
downloads
6 changes: 4 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pip install -r requirements.txt
pip install pyinstaller
#! /usr/bin/env bash

pip3 install -r requirements.txt
pip3 install pyinstaller
pyinstaller --noconfirm --onefile --console tgd.py
mv dist/tgd tgd
chmod 777 tgd
Expand Down
195 changes: 133 additions & 62 deletions tgd.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,162 @@
from pyrogram import Client
from pyrogram.types import User, Message
from os.path import exists
from os import remove
from os import system, name as osname
from sys import stdout, exit

configfile = "tgd.txt"
TGD = """
▄██████▄ ▄██████▄ ████████▄
██▀▀███▀▀██ ███ ███ ███ ▀███
███ ███ █▀ ███ ███
███ ▄███ ███ ███
███ ▀▀███ ████▄ ███ ███
███ ███ ███ ███ ███
███ ███ ███ ███ ▄███
▄████ ████████▀ ████████▀
"""

if not exists(configfile):
api_id = input("\nAPI ID: ")
api_hash = input("API HASH: ")
check = input("Do you have String Session? (y/n): ")
if check.lower() == "y":
ss = input("SESSION STRING: ")
else:
print()
with Client("TGD", api_id=api_id, api_hash=api_hash, in_memory=True) as temp:
ss = temp.export_session_string()
print()
with open(configfile,"w") as file:
file.write(api_id + "\n" + api_hash + "\n" + ss)
else:
with open(configfile, "r") as file:
data = file.readlines()
system("cls" if osname == "nt" else "clear")
print(TGD)
print(" TeleGram Downloader\n")


def wait():
try:
api_id, api_hash, ss = data
except:
remove(configfile)
print("Retry...")
input("Press enter to exit...")
except KeyboardInterrupt:
pass
finally:
exit(0)


acc = Client("myacc" ,api_id=api_id, api_hash=api_hash, session_string=ss)
try:
with acc:
me = acc.get_me()
print("\nLogged in as:", me.id)
except:
remove(configfile)
print("\nMaybe Wrong Crenditals...")
exit(0)


def progress(current, total, length=50):


def progress(current, total, length=100):
progress_percent = current * 100 / total
completed = int(length * current / total)
bar = f"[{'#' * completed}{' ' * (length - completed)}] {progress_percent:.1f}%"
stdout.write(f"\r{bar}")
stdout.flush()


print("""
Examples:
def preProcess():
if not exists(configfile):
try:
login = input(
f"{configfile} not found, Do you wish to login? (y/n): ")
if login.lower() != "y":
wait()

https://t.me/xxxx/1423
https://t.me/c/xxxx/10
https://t.me/xxxx/1001-1010
https://t.me/c/xxxx/101 - 120\n\n""")
api_id = input("\nAPI ID: ")
api_hash = input("API HASH: ")
check = input(
"Do you have already created Session? selecting 'no' will trigger session login and 'yes' will ask for Session String (y/n): ")
if check.lower() == "y":
ss = input("SESSION STRING: ")
else:
print()
temp = Client("temp", api_id=api_id,
api_hash=api_hash, in_memory=True)
with temp:
ss = temp.export_session_string()
print()
with open(configfile, "w") as file:
file.write(api_id + "\n" + api_hash + "\n" + ss)
except KeyboardInterrupt:
print("\nKeyboard interrupt detected. Exiting...")
wait()
except Exception as e:
print(e)
wait()
else:
with open(configfile, "r") as file:
data = file.readlines()
try:
api_id, api_hash, ss = data
return api_id, api_hash, ss
except:
print("Retry... by deleting", configfile)
wait()

link = input("Enter the link: ")
print()

def handleEverything():
link = input("\nEnter the post link: ")
print()

if link.startswith("https://t.me/"):
datas = link.split("/")
temp = datas[-1].replace("?single","").split("-")
fromID = int(temp[0].strip())
try: toID = int(temp[1].strip())
except: toID = fromID
################

if link.startswith("https://t.me/"):
datas = link.split("/")
temp = datas[-1].replace("?single", "").split("-")
fromID = int(temp[0].strip())
try:
toID = int(temp[1].strip())
except:
toID = fromID

if link.startswith("https://t.me/c/"):
chatid = int("-100" + datas[4])
if link.startswith("https://t.me/c/"):
chatid = int("-100" + datas[4])
else:
chatid = datas[3]
else:
chatid = datas[3]
print("Not a Telegram Link")
wait()

else:
print("Not a Telegram Link")
exit(0)
################

with acc:
total = toID+1 - fromID
total = toID + 1 - fromID
for msgid in range(fromID, toID+1):
msg = acc.get_messages(chatid, msgid)

msg: Message = acc.get_messages(chatid, msgid)
if msg.empty:
print("Message not found:", chatid, "/", msgid, "Skipping...\n")
continue

print("Downloding:", msgid, f"({(msgid - fromID + 1)}/{total})")
try:
file = acc.download_media(msg, progress=progress)
print("\nSaved at", file, "\n")
except ValueError as e:
print(e, "\n")

input("Press enter to exit...")
if str(e) == "This message doesn't contain any downloadable media":
txtfile = f"downloads/{str(msg.chat.id)[-10:]}-{msg.id}.txt"
with open(txtfile, "w", encoding="utf-8") as file:
file.write(str(msg.text))
print("Saved at", txtfile, "\n")
else:
print(e, "\n")


def main():
try:
print("Logging in...")
with acc:
me: User = acc.get_me()
print(
f"Logged in as: {me.first_name}{(' ' + me.last_name) if me.last_name else ''}{(' - @' + me.username) if me.username else ''} ({me.id})")

print("""
Examples:
https://t.me/xxxx/1423
https://t.me/c/xxxx/10
https://t.me/xxxx/1001-1010
https://t.me/c/xxxx/101 - 120""")

while True:
handleEverything()
cont = input("Do you wish to continue? (y/n): ")
if cont.lower() != "y":
break
except KeyboardInterrupt:
print("\nKeyboard interrupt detected. Exiting...")
wait()
except Exception as e:
print("\n", e, "\nAn error occured. Exiting..")
print("Retry... by deleting", configfile)
wait()


if __name__ == "__main__":
api_id, api_hash, ss = preProcess()
acc = Client("TGD", api_id=api_id, api_hash=api_hash,
session_string=ss, in_memory=True)
main()
wait()

0 comments on commit 4a84a62

Please sign in to comment.