2020-03-21 14:43:32 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
|
|
|
# Copyright (C) 2017-2020 Dan <https://github.com/delivrance>
|
2018-05-09 11:36:33 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# This file is part of Pyrogram.
|
2018-05-09 11:36:33 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# Pyrogram is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2018-05-09 11:36:33 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# Pyrogram is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
2018-05-09 11:36:33 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
2018-05-09 11:36:33 +00:00
|
|
|
|
2020-08-21 05:28:27 +00:00
|
|
|
import asyncio
|
2019-06-15 21:02:31 +00:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
from datetime import datetime
|
2020-12-20 16:05:17 +00:00
|
|
|
from typing import Union, Optional
|
2018-05-09 11:36:33 +00:00
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
from pyrogram import types
|
2020-11-27 21:09:17 +00:00
|
|
|
from pyrogram.file_id import FileId, FileType, PHOTO_TYPES
|
2020-08-22 06:05:05 +00:00
|
|
|
from pyrogram.scaffold import Scaffold
|
2018-05-09 11:36:33 +00:00
|
|
|
|
2019-06-15 21:02:31 +00:00
|
|
|
DEFAULT_DOWNLOAD_DIR = "downloads/"
|
|
|
|
|
2018-05-09 11:36:33 +00:00
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
class DownloadMedia(Scaffold):
|
2020-08-21 05:28:27 +00:00
|
|
|
async def download_media(
|
2019-03-16 18:23:23 +00:00
|
|
|
self,
|
2020-11-28 16:37:39 +00:00
|
|
|
message: Union["types.Message", str],
|
2019-06-15 21:02:31 +00:00
|
|
|
file_name: str = DEFAULT_DOWNLOAD_DIR,
|
2019-03-16 18:23:23 +00:00
|
|
|
block: bool = True,
|
|
|
|
progress: callable = None,
|
|
|
|
progress_args: tuple = ()
|
2020-12-20 16:05:17 +00:00
|
|
|
) -> Optional[str]:
|
2019-05-12 17:49:06 +00:00
|
|
|
"""Download the media from a message.
|
2018-05-09 11:36:33 +00:00
|
|
|
|
2019-05-09 02:28:46 +00:00
|
|
|
Parameters:
|
2020-11-28 16:37:39 +00:00
|
|
|
message (:obj:`~pyrogram.types.Message` | ``str``):
|
2020-11-27 21:09:17 +00:00
|
|
|
Pass a Message containing the media, the media itself (message.audio, message.video, ...) or a file id
|
|
|
|
as string.
|
2019-09-21 19:12:11 +00:00
|
|
|
|
2018-05-09 11:36:33 +00:00
|
|
|
file_name (``str``, *optional*):
|
|
|
|
A custom *file_name* to be used instead of the one provided by Telegram.
|
|
|
|
By default, all files are downloaded in the *downloads* folder in your working directory.
|
|
|
|
You can also specify a path for downloading files in a custom location: paths that end with "/"
|
|
|
|
are considered directories. All non-existent folders will be created automatically.
|
|
|
|
|
|
|
|
block (``bool``, *optional*):
|
|
|
|
Blocks the code execution until the file has been downloaded.
|
|
|
|
Defaults to True.
|
|
|
|
|
2019-07-25 09:22:14 +00:00
|
|
|
progress (``callable``, *optional*):
|
|
|
|
Pass a callback function to view the file transmission progress.
|
|
|
|
The function must take *(current, total)* as positional arguments (look at Other Parameters below for a
|
|
|
|
detailed description) and will be called back each time a new file chunk has been successfully
|
|
|
|
transmitted.
|
2018-05-09 11:36:33 +00:00
|
|
|
|
2019-07-25 09:22:14 +00:00
|
|
|
progress_args (``tuple``, *optional*):
|
|
|
|
Extra custom arguments for the progress callback function.
|
|
|
|
You can pass anything you need to be available in the progress callback scope; for example, a Message
|
|
|
|
object or a Client instance in order to edit the message with the updated progress status.
|
2018-05-09 11:36:33 +00:00
|
|
|
|
|
|
|
Other Parameters:
|
|
|
|
current (``int``):
|
2019-07-25 09:22:14 +00:00
|
|
|
The amount of bytes transmitted so far.
|
2018-05-09 11:36:33 +00:00
|
|
|
|
|
|
|
total (``int``):
|
2019-07-25 09:22:14 +00:00
|
|
|
The total size of the file.
|
2018-05-09 11:36:33 +00:00
|
|
|
|
2018-05-12 09:21:02 +00:00
|
|
|
*args (``tuple``, *optional*):
|
2020-08-26 07:07:26 +00:00
|
|
|
Extra custom arguments as defined in the ``progress_args`` parameter.
|
|
|
|
You can either keep ``*args`` or add every single extra argument in your function signature.
|
2018-05-09 11:36:33 +00:00
|
|
|
|
|
|
|
Returns:
|
2019-05-17 11:44:44 +00:00
|
|
|
``str`` | ``None``: On success, the absolute path of the downloaded file is returned, otherwise, in case
|
2020-08-22 06:05:05 +00:00
|
|
|
the download failed or was deliberately stopped with :meth:`~pyrogram.Client.stop_transmission`, None is
|
|
|
|
returned.
|
2018-05-09 11:36:33 +00:00
|
|
|
|
|
|
|
Raises:
|
2019-07-09 17:03:15 +00:00
|
|
|
ValueError: if the message doesn't contain any downloadable media
|
2019-07-25 09:22:14 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Download from Message
|
|
|
|
app.download_media(message)
|
|
|
|
|
|
|
|
# Download from file id
|
2020-07-27 13:12:24 +00:00
|
|
|
app.download_media("CAADBAADzg4AAvLQYAEz_x2EOgdRwBYE")
|
2020-03-30 12:39:16 +00:00
|
|
|
|
|
|
|
# Keep track of the progress while downloading
|
|
|
|
def progress(current, total):
|
2020-08-22 06:05:05 +00:00
|
|
|
print(f"{current * 100 / total:.1f}%")
|
2020-03-30 12:39:16 +00:00
|
|
|
|
|
|
|
app.download_media(message, progress=progress)
|
2018-05-09 11:36:33 +00:00
|
|
|
"""
|
2020-11-27 21:09:17 +00:00
|
|
|
available_media = ("audio", "document", "photo", "sticker", "animation", "video", "voice", "video_note",
|
|
|
|
"new_chat_photo")
|
2019-05-29 08:40:37 +00:00
|
|
|
|
2020-11-28 16:37:39 +00:00
|
|
|
if isinstance(message, types.Message):
|
2019-05-29 08:40:37 +00:00
|
|
|
for kind in available_media:
|
2020-11-28 16:37:39 +00:00
|
|
|
media = getattr(message, kind, None)
|
2019-05-29 08:40:37 +00:00
|
|
|
|
|
|
|
if media is not None:
|
|
|
|
break
|
2018-05-09 11:36:33 +00:00
|
|
|
else:
|
2020-11-27 21:09:17 +00:00
|
|
|
raise ValueError("This message doesn't contain any downloadable media")
|
2020-11-28 16:37:39 +00:00
|
|
|
else:
|
|
|
|
media = message
|
2019-05-29 08:40:37 +00:00
|
|
|
|
|
|
|
if isinstance(media, str):
|
|
|
|
file_id_str = media
|
|
|
|
else:
|
|
|
|
file_id_str = media.file_id
|
2020-11-27 21:09:17 +00:00
|
|
|
|
|
|
|
file_id_obj = FileId.decode(file_id_str)
|
|
|
|
|
|
|
|
file_type = file_id_obj.file_type
|
|
|
|
media_file_name = getattr(media, "file_name", "")
|
|
|
|
file_size = getattr(media, "file_size", 0)
|
|
|
|
mime_type = getattr(media, "mime_type", None)
|
|
|
|
date = getattr(media, "date", 0)
|
2018-05-09 11:36:33 +00:00
|
|
|
|
2019-06-15 21:02:31 +00:00
|
|
|
directory, file_name = os.path.split(file_name)
|
2020-11-27 21:09:17 +00:00
|
|
|
file_name = file_name or media_file_name or ""
|
2019-06-15 21:02:31 +00:00
|
|
|
|
|
|
|
if not os.path.isabs(file_name):
|
|
|
|
directory = self.PARENT_DIR / (directory or DEFAULT_DOWNLOAD_DIR)
|
|
|
|
|
|
|
|
if not file_name:
|
2020-11-27 21:09:17 +00:00
|
|
|
guessed_extension = self.guess_extension(mime_type)
|
2019-06-15 21:02:31 +00:00
|
|
|
|
2020-11-27 21:09:17 +00:00
|
|
|
if file_type in PHOTO_TYPES:
|
2019-06-15 21:02:31 +00:00
|
|
|
extension = ".jpg"
|
2020-11-27 21:09:17 +00:00
|
|
|
elif file_type == FileType.VOICE:
|
2019-06-15 21:02:31 +00:00
|
|
|
extension = guessed_extension or ".ogg"
|
2020-11-27 21:09:17 +00:00
|
|
|
elif file_type in (FileType.VIDEO, FileType.ANIMATION, FileType.VIDEO_NOTE):
|
2019-06-15 21:02:31 +00:00
|
|
|
extension = guessed_extension or ".mp4"
|
2020-11-27 21:09:17 +00:00
|
|
|
elif file_type == FileType.DOCUMENT:
|
2019-06-15 21:02:31 +00:00
|
|
|
extension = guessed_extension or ".zip"
|
2020-11-27 21:09:17 +00:00
|
|
|
elif file_type == FileType.STICKER:
|
2019-06-15 21:02:31 +00:00
|
|
|
extension = guessed_extension or ".webp"
|
2020-11-27 21:09:17 +00:00
|
|
|
elif file_type == FileType.AUDIO:
|
2019-06-15 21:02:31 +00:00
|
|
|
extension = guessed_extension or ".mp3"
|
|
|
|
else:
|
|
|
|
extension = ".unknown"
|
|
|
|
|
|
|
|
file_name = "{}_{}_{}{}".format(
|
2020-11-27 21:09:17 +00:00
|
|
|
FileType(file_id_obj.file_type).name.lower(),
|
|
|
|
datetime.fromtimestamp(date or time.time()).strftime("%Y-%m-%d_%H-%M-%S"),
|
2019-06-15 21:02:31 +00:00
|
|
|
self.rnd_id(),
|
|
|
|
extension
|
|
|
|
)
|
|
|
|
|
2020-11-27 21:09:17 +00:00
|
|
|
downloader = self.handle_download((file_id_obj, directory, file_name, file_size, progress, progress_args))
|
2018-05-09 11:36:33 +00:00
|
|
|
|
|
|
|
if block:
|
2020-08-22 06:05:05 +00:00
|
|
|
return await downloader
|
|
|
|
else:
|
|
|
|
asyncio.get_event_loop().create_task(downloader)
|