-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.py
70 lines (59 loc) · 2 KB
/
helpers.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
65
66
67
68
69
70
#!/usr/bin/env python3
# coding: utf-8
import logging
import os
import subprocess
import zipfile
from pathlib import Path
from pyrogram.file_id import FileId, FileType
# stickers - helpers.py
# 2022-10-20 23:27
def get_target_file(src: Path):
old_ext = src.suffix
if old_ext in [".jpeg", ".jpg", ".png", ".webp"]:
return src.with_suffix(".png")
elif old_ext in [".mp4", ".webm", ".tgs"]:
return src.with_suffix(".gif")
else:
return src.with_suffix(".mp4")
def converter(src_file):
src_file = Path(src_file)
target_file = get_target_file(src_file)
logging.info(f"converting %s to %s", src_file, target_file)
if src_file.suffix == ".tgs":
subprocess.check_output(["lottie_convert.py", src_file, target_file])
else:
subprocess.check_output(["ffmpeg", "-i", src_file, target_file])
return target_file.as_posix()
def get_file_id(doc, set_id, set_hash):
return FileId(file_type=FileType.STICKER,
dc_id=doc.dc_id,
file_reference=doc.file_reference,
media_id=doc.id,
access_hash=doc.access_hash,
sticker_set_id=set_id,
sticker_set_access_hash=set_hash)
def get_ext_from_mime(mime):
if mime == "image/jpeg":
return ".jpg"
elif mime == "image/png":
return ".png"
elif mime == "image/webp":
return ".webp"
elif mime == "video/mp4":
return ".mp4"
elif mime == "video/webm":
return ".webm"
elif mime == "application/x-tgsticker":
return ".tgs"
else:
return ""
def zip_dir(dir_path, zip_filepath):
logging.info("zipping %s to %s", dir_path, zip_filepath)
zipf = zipfile.ZipFile(zip_filepath, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(dir_path):
for file in files:
file_path = Path(root).joinpath(file)
file_name = file_path.relative_to(dir_path)
zipf.write(file_path, file_name)
zipf.close()