Skip to content

Commit

Permalink
Merge pull request #153 from Dineshkarthik/146-feature-download-video…
Browse files Browse the repository at this point in the history
…-note

Enhancement -  Support for Video-Note Download
  • Loading branch information
Dineshkarthik authored Feb 11, 2022
2 parents 0226c19 + d794b35 commit 0e468f1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ document/
photo/
voice/
video/
video_note/
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ media_types:
- video
- document
- voice
- video_note
file_formats:
audio:
- all
document:
- all
video:
- all

22 changes: 12 additions & 10 deletions media_downloader.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"""Downloads media from telegram."""
import os
import asyncio
import logging
from typing import List, Tuple, Optional, Union
import os
from datetime import datetime as dt
from typing import List, Optional, Tuple, Union

import asyncio
import pyrogram
import yaml
from pyrogram.types import Audio, Document, Photo, Video, Voice
from pyrogram.types import Audio, Document, Photo, Video, VideoNote, Voice

from utils.file_management import get_next_name, manage_duplicate_file
from utils.log import LogFilter
from utils.meta import print_meta


logging.basicConfig(level=logging.INFO)
logging.getLogger("pyrogram.session.session").addFilter(LogFilter())
logging.getLogger("pyrogram.client").addFilter(LogFilter())
Expand Down Expand Up @@ -84,13 +84,14 @@ def _is_exist(file_path: str) -> bool:


async def _get_media_meta(
media_obj: Union[Audio, Document, Photo, Video, Voice], _type: str
media_obj: Union[Audio, Document, Photo, Video, VideoNote, Voice],
_type: str,
) -> Tuple[str, Optional[str]]:
"""Extract file name and file id from media object.
Parameters
----------
media_obj: Union[Audio, Document, Photo, Video, Voice]
media_obj: Union[Audio, Document, Photo, Video, VideoNote, Voice]
Media object to be extracted.
_type: str
Type of media object.
Expand All @@ -106,13 +107,14 @@ async def _get_media_meta(
else:
file_format = None

if _type == "voice":
if _type in ["voice", "video_note"]:
# pylint: disable = C0209
file_format = media_obj.mime_type.split("/")[-1] # type: ignore
file_name: str = os.path.join(
THIS_DIR,
_type,
"voice_{}.{}".format(
"{}_{}.{}".format(
_type,
dt.utcfromtimestamp(media_obj.date).isoformat(), # type: ignore
file_format,
),
Expand Down Expand Up @@ -190,7 +192,7 @@ async def download_media(
"Message[%d]: file reference expired, refetching...",
message.message_id,
)
message = await client.get_messages( # type: ignore
message = await client.get_messages( # type: ignore
chat_id=message.chat.id, # type: ignore
message_ids=message.message_id,
)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_media_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def __init__(self, **kwargs):
self.photo = kwargs.get("photo", None)
self.video = kwargs.get("video", None)
self.voice = kwargs.get("voice", None)
self.video_note = kwargs.get("video_note", None)
self.chat = Chat(kwargs.get("chat_id", None))


Expand Down Expand Up @@ -91,6 +92,12 @@ def __init__(self, **kwargs):
self.mime_type = kwargs["mime_type"]


class MockVideoNote:
def __init__(self, **kwargs):
self.mime_type = kwargs["mime_type"]
self.date = kwargs["date"]


class MockEventLoop:
def __init__(self):
pass
Expand Down Expand Up @@ -317,6 +324,28 @@ def test_get_media_meta(self):
result,
)

# Test VideoNote
message = MockMessage(
id=6,
media=True,
video_note=MockVideoNote(
mime_type="video/mp4",
date=1564066430,
),
)
result = self.loop.run_until_complete(
async_get_media_meta(message.video_note, "video_note")
)
self.assertEqual(
(
platform_generic_path(
"/root/project/video_note/video_note_2019-07-25T14:53:50.mp4"
),
"mp4",
),
result,
)

@mock.patch("media_downloader.THIS_DIR", new=MOCK_DIR)
@mock.patch("media_downloader.asyncio.sleep", return_value=None)
@mock.patch("media_downloader.logger")
Expand Down
2 changes: 1 addition & 1 deletion utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Init namespace"""

__version__ = "1.3.0"
__version__ = "1.4.0"
__license__ = "MIT License"
__copyright__ = (
"Copyright (C) 2019 Dineshkarthik <https://github.com/Dineshkarthik>"
Expand Down

0 comments on commit 0e468f1

Please sign in to comment.