The database is locked, can't even start the program
how to make it so that a new session is not created, but connects to the old session?
I got banned because of too many connections. I don't know what to do.
import logging
import os
import download
import yt_dlp
import list
from dotenv import load_dotenv
import time
load_dotenv()
chats = list.chats
api_id = os.getenv("api_id")
api_hash = os.getenv("api_hash")
logging.basicConfig(
format="[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s", level=logging.WARNING
)
def downloadvideo(link):
with yt_dlp.YoutubeDL() as ydl:
ydl.download(link)
from telethon import TelegramClient, events
client = TelegramClient('anon', api_id, api_hash)
@client.on(
events.NewMessage(
chats=chats,
pattern=r"(?:https?://)?(?:www\.)?(?:youtube\.com|youtu\.be|tiktok\.com)",
)
)
async def my_event_handler(event):
if event.chat_id in chats:
message_id = event.id
video = download.downloadvideo(event.message.message)
message = f"[link]({event.message.message})"
await client.send_file(
-1001673012624,
reply_to=message_id,
file="telegramvideo.mp4",
video_note=True,
caption=message,
)
os.remove("telegramvideo.mp4")
phone = os.getenv("phone")
# -1001919227306 maratozpanov
client.start(phone=phone)
Delete session file. I thought it would work after I deleted it.
You are posting a lot of different problems in a different question.
how to make it so that a new session is not created, but connects to the old session?
This is done by simply using the same session name as the first parameter to the TelegramClient
constructor.
I got banned because of too many connections
Telegram decides when the bans occur. This is not something the library can help with. VoIP numbers in particular are very prone to being banned.
with yt_dlp.YoutubeDL() ...
I'm not familiar with yt_dlp
, but if the library is threaded, be mindful that Telethon uses asyncio
. If the asyncio
event loop cannot run (because the thread is blocked), things may misbehave.
I recommend you read the asyncio
documentation and learn about running blocking functions in executors.
sqlite3.OperationalError: database is locked
This likely means the session file is being used by multiple processes. Make sure there is only one process accessing the same session file at a time. Use multiple sessions if you need multiple concurrent processes.
telethon.errors.rpcerrorlist.FloodWaitError
This means you've attempted to make that request far too many times and Telegram is rate-limitting you. You can only wait, and avoid making that request so often (likely related to the problems you have reusing sessions, addressed in other points).
UserWarning: the session already had an authorized user so it did not login to the user account using the provided phone (it may not be using the user you expect)
This means the session you are trying to reuse was already logged-in previously, but now, you've provided a different phone. So the library warns you that it ignored the new phone because the account was already logged-in.