Skip to content

Commit 0dda119

Browse files
committed
Video upload replaced with passing a URL; add VK clips
1 parent 0372010 commit 0dda119

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

downloadbot/__main__.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
welcome_text = """
1818
Hello there 👋
1919
20-
I can download videos from X/Twitter 🐦, Pinterest 📍, Instagram 📸 and TikTok ♪. Just send me a link
20+
I can download videos from X/Twitter 🐦, Pinterest 📍, Instagram 📸, TikTok ♪ and VK. Just send me a link
2121
2222
Also you can add me to a group or use me in any chat with `@QuartzMediaBot <your link>`
2323
"""
@@ -32,46 +32,45 @@ async def start(message: types.Message) -> None:
3232
pinterest_pattern = r"(?:https?://)?(?:www\.)?(?:\w+\.)?(?:pin\.it|pinterest\.com)/\S+"
3333
instagram_pattern = r"(?:https?://)?(?:www\.)?instagram\.com/\S+"
3434
tiktok_pattern = r"(?:https?://)?(?:www\.)?(?:\w+\.)?tiktok\.com/\S+"
35+
vk_pattern = r"(?:https?://)?(?:www\.)?(?:\w+\.)?vk\.(?:com|ru)/clip-\S+"
3536

36-
combined_pattern = "|".join([twitter_pattern, pinterest_pattern, instagram_pattern, tiktok_pattern])
37+
combined_pattern = "|".join([
38+
twitter_pattern,
39+
pinterest_pattern,
40+
instagram_pattern,
41+
tiktok_pattern,
42+
vk_pattern
43+
])
3744

3845

3946
@bot.message_handler(regexp=combined_pattern)
4047
async def download_video(message: types.Message) -> None:
4148
progress_msg = await bot.reply_to(message, "🔎")
4249
try:
4350
url = re.findall(combined_pattern, message.text)[0]
44-
video = await get_video(url)
51+
video = await get_video(url, download=True)
52+
msg = None
4553

46-
if video.buffer:
47-
if not video.is_image and video.has_audio:
54+
match video.content_type:
55+
case "video/mp4":
4856
telebot.logger.debug("Sending video")
49-
await bot.send_video(
57+
msg = await bot.send_video(
5058
message.chat.id,
51-
video=video.buffer,
52-
height=video.height,
53-
width=video.width,
54-
supports_streaming=True,
59+
video=video.url,
5560
reply_parameters=types.ReplyParameters(message_id=message.id)
5661
)
57-
elif video.is_image:
62+
case _ if video.buffer:
5863
telebot.logger.debug("Sending photo")
59-
await bot.send_photo(
64+
msg = await bot.send_photo(
6065
message.chat.id,
6166
photo=video.buffer,
6267
reply_parameters=types.ReplyParameters(message_id=message.id)
6368
)
64-
elif not video.has_audio:
65-
telebot.logger.debug("Sending gif")
66-
await bot.send_animation(
67-
message.chat.id,
68-
animation=video.buffer,
69-
height=video.height,
70-
width=video.width,
71-
reply_parameters=types.ReplyParameters(message_id=message.id)
72-
)
73-
elif message.chat.type == "private":
74-
await bot.reply_to(message, text="It seems not to be a link to video 😔")
69+
case _ if message.chat.type == "private":
70+
await bot.reply_to(message, text="It seems not to be a link to video 😔")
71+
72+
if msg:
73+
telebot.logger.info(f"{msg.video=} {msg.photo=} {msg.animation=} {msg.document=}")
7574

7675
except Exception as e:
7776
telebot.logger.exception(e)

downloadbot/download.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,33 @@ class Video:
2424
width: int | None
2525
is_image: bool
2626
has_audio: bool | None
27+
skipped_download: bool
28+
content_type: str | None
2729

2830

2931
HEADERS = {"Content-Type": "application/json", "Accept": "application/json"}
3032
DEFAULT_THUMBNAIL = "https://avatars.mds.yandex.net/i?id=f8e7cca4d77040af7b7a642f2ee39c81_l-4902542-images-thumbs&n=13"
3133

3234

3335
async def download_video(session: aiohttp.ClientSession, video_data: Video) -> Video:
34-
async with session.get(video_data.url) as video_response:
36+
async with session.get(video_data.url, headers={"Range": "bytes=0-"}) as video_response:
37+
logger.info(f"Response headers: {video_response.headers}")
38+
video_data.content_type = video_response.headers.get("Content-Type")
39+
logger.info(f"Content type: {video_data.content_type}")
40+
if video_data.content_type and video_data.content_type.split("/")[0] == "video":
41+
video_data.skipped_download = True
42+
return video_data
43+
3544
video_buffer = BytesIO()
36-
video_buffer.write(await video_response.read())
45+
while True:
46+
chunk = None
47+
try:
48+
chunk = await video_response.content.readany()
49+
except:
50+
break
51+
if not chunk:
52+
break
53+
video_buffer.write(chunk)
3754
video_buffer.seek(0)
3855

3956
video_data.buffer = video_buffer
@@ -63,19 +80,6 @@ async def download_video(session: aiohttp.ClientSession, video_data: Video) -> V
6380
info = json.loads(result.stdout or r"{}")
6481
video_data.has_audio = bool(info.get("streams"))
6582

66-
if not video_data.has_audio and not video_data.is_image:
67-
gif_path = tmp.name + ".gif"
68-
convert_cmd = [
69-
"ffmpeg", "-y",
70-
"-i", tmp.name,
71-
"-f", "gif",
72-
gif_path
73-
]
74-
subprocess.run(convert_cmd, check=True)
75-
with open(gif_path, "rb") as f:
76-
video_buffer.write(f.read())
77-
video_buffer.seek(0)
78-
7983
logger.info(f"{video_data=}")
8084
return video_data
8185

@@ -90,7 +94,9 @@ async def get_video(post_url: str, download: bool = True) -> Video:
9094
height=None,
9195
width=None,
9296
is_image=False,
93-
has_audio=None
97+
has_audio=None,
98+
skipped_download=False,
99+
content_type=None
94100
)
95101

96102
async with aiohttp.ClientSession() as session:

0 commit comments

Comments
 (0)