-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelegramDumpVoice.py
64 lines (49 loc) · 2.13 KB
/
TelegramDumpVoice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from pyrogram import Client
from pyrogram.errors import PeerIdInvalid
# Replace these with your own API ID and API Hash
api_id = 1111
api_hash = '---'
# Folder where the voice messages will be saved
download_folder = r'File/Path'
# Initialize Pyrogram client
app = Client("my_account", api_id=api_id, api_hash=api_hash)
# Ensure the download folder exists
if not os.path.exists(download_folder):
os.makedirs(download_folder)
# Function to download voice messages from a private channel
def download_voice_messages(channel_username):
try:
# Fetch all messages in the channel
messages = app.get_chat_history(channel_username)
# Collect voice messages into a list
voice_messages = [message for message in messages if message.voice]
# Sort the collected voice messages by date
voice_messages.sort(key=lambda msg: msg.date)
# Download the voice messages
for message in voice_messages:
sent_time = message.date.strftime("%Y-%m-%d_%H-%M-%S")
file_name = f"{message.id}_{sent_time}.ogg"
file_path = os.path.join(download_folder, file_name)
if os.path.exists(file_path):
print(f"File {file_name} already exists. Skipping...")
continue # Skip if the file already exists
print(f"Downloading {file_name}...")
app.download_media(message, file_name=file_path)
print("All voice messages have been downloaded in chronological order.")
except PeerIdInvalid:
print("Error: Invalid Peer ID. Make sure the channel is accessible and that you have joined it.")
except Exception as e:
print(f"An error occurred: {e}")
# Main function to run the voice message download
if __name__ == "__main__":
# Start the client session
app.start()
try:
# Enter the channel username or ID
channel_username = 111111
# Attempt to download messages
download_voice_messages(channel_username)
finally:
# Stop the client session when done
app.stop()