generated from pavel.muhortov/template-bash
add Do.tg_routine_media()
This commit is contained in:
parent
aea4a5ba52
commit
c575e844f2
|
@ -12,7 +12,7 @@ from argparse import ArgumentParser
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from ftplib import FTP
|
from ftplib import FTP
|
||||||
from multiprocessing import Process, Queue
|
from multiprocessing import Process, Queue
|
||||||
from os import environ, makedirs, path, remove, replace, sep, walk
|
from os import environ, makedirs, path, remove, replace, sep, stat, walk
|
||||||
from subprocess import Popen, PIPE, STDOUT
|
from subprocess import Popen, PIPE, STDOUT
|
||||||
from sys import platform
|
from sys import platform
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
@ -2393,12 +2393,24 @@ class Do():
|
||||||
if Do.args_valid(locals(), Do.wp_routine_media.__annotations__):
|
if Do.args_valid(locals(), Do.wp_routine_media.__annotations__):
|
||||||
default_media_links = {
|
default_media_links = {
|
||||||
"d": {
|
"d": {
|
||||||
"point-01": "https://www.hmp.today/wp-content/uploads/2022/07/point-01_yyyy.mm_.dd_.mp4",
|
"point-01": (
|
||||||
"point-02": "https://www.hmp.today/wp-content/uploads/2022/07/point-02_yyyy.mm_.dd_.mp4",
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-01_yyyy.mm_.dd_.mp4"
|
||||||
"point-04": "https://www.hmp.today/wp-content/uploads/2022/07/point-04_yyyy.mm_.dd_.mp4",
|
),
|
||||||
"point-05": "https://www.hmp.today/wp-content/uploads/2022/07/point-05_yyyy.mm_.dd_.mp4",
|
"point-02": (
|
||||||
"point-11": "https://www.hmp.today/wp-content/uploads/2022/07/point-11_yyyy.mm_.dd_.mp4",
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-02_yyyy.mm_.dd_.mp4"
|
||||||
"point-12": "https://www.hmp.today/wp-content/uploads/2022/07/point-12_yyyy.mm_.dd_.mp4"
|
),
|
||||||
|
"point-04": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-04_yyyy.mm_.dd_.mp4"
|
||||||
|
),
|
||||||
|
"point-05": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-05_yyyy.mm_.dd_.mp4"
|
||||||
|
),
|
||||||
|
"point-11": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-11_yyyy.mm_.dd_.mp4"
|
||||||
|
),
|
||||||
|
"point-12": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-12_yyyy.mm_.dd_.mp4"
|
||||||
|
)
|
||||||
},
|
},
|
||||||
"w": {
|
"w": {
|
||||||
"point-01": "https://www.hmp.today/wp-content/uploads/2022/07/point-01_w.mp4",
|
"point-01": "https://www.hmp.today/wp-content/uploads/2022/07/point-01_w.mp4",
|
||||||
|
@ -2409,20 +2421,44 @@ class Do():
|
||||||
"point-12": "https://www.hmp.today/wp-content/uploads/2022/07/point-12_w.mp4"
|
"point-12": "https://www.hmp.today/wp-content/uploads/2022/07/point-12_w.mp4"
|
||||||
},
|
},
|
||||||
"m": {
|
"m": {
|
||||||
"point-01": "https://www.hmp.today/wp-content/uploads/2022/07/point-01_yyyy.mm_.mp4",
|
"point-01": (
|
||||||
"point-02": "https://www.hmp.today/wp-content/uploads/2022/07/point-02_yyyy.mm_.mp4",
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-01_yyyy.mm_.mp4"
|
||||||
"point-04": "https://www.hmp.today/wp-content/uploads/2022/07/point-04_yyyy.mm_.mp4",
|
),
|
||||||
"point-05": "https://www.hmp.today/wp-content/uploads/2022/07/point-05_yyyy.mm_.mp4",
|
"point-02": (
|
||||||
"point-11": "https://www.hmp.today/wp-content/uploads/2022/07/point-11_yyyy.mm_.mp4",
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-02_yyyy.mm_.mp4"
|
||||||
"point-12": "https://www.hmp.today/wp-content/uploads/2022/07/point-12_yyyy.mm_.mp4"
|
),
|
||||||
|
"point-04": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-04_yyyy.mm_.mp4"
|
||||||
|
),
|
||||||
|
"point-05": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-05_yyyy.mm_.mp4"
|
||||||
|
),
|
||||||
|
"point-11": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-11_yyyy.mm_.mp4"
|
||||||
|
),
|
||||||
|
"point-12": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-12_yyyy.mm_.mp4"
|
||||||
|
)
|
||||||
},
|
},
|
||||||
"y": {
|
"y": {
|
||||||
"point-01": "https://www.hmp.today/wp-content/uploads/2022/07/point-01_yyyy.mp4",
|
"point-01": (
|
||||||
"point-02": "https://www.hmp.today/wp-content/uploads/2022/07/point-02_yyyy.mp4",
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-01_yyyy.mp4"
|
||||||
"point-04": "https://www.hmp.today/wp-content/uploads/2022/07/point-04_yyyy.mp4",
|
),
|
||||||
"point-05": "https://www.hmp.today/wp-content/uploads/2022/07/point-05_yyyy.mp4",
|
"point-02": (
|
||||||
"point-11": "https://www.hmp.today/wp-content/uploads/2022/07/point-11_yyyy.mp4",
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-02_yyyy.mp4"
|
||||||
"point-12": "https://www.hmp.today/wp-content/uploads/2022/07/point-12_yyyy.mp4"
|
),
|
||||||
|
"point-04": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-04_yyyy.mp4"
|
||||||
|
),
|
||||||
|
"point-05": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-05_yyyy.mp4"
|
||||||
|
),
|
||||||
|
"point-11": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-11_yyyy.mp4"
|
||||||
|
),
|
||||||
|
"point-12": (
|
||||||
|
"https://www.hmp.today/wp-content/uploads/2022/07/point-12_yyyy.mp4"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
current_media_links = default_media_links[period]
|
current_media_links = default_media_links[period]
|
||||||
|
@ -2595,6 +2631,147 @@ class Do():
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
# pylint: disable=W0612,W0511
|
||||||
|
def tg_routine_media(
|
||||||
|
tg: Telegram,
|
||||||
|
targets_media_files: dict,
|
||||||
|
period: str,
|
||||||
|
amount: int,
|
||||||
|
chat: str
|
||||||
|
) -> dict:
|
||||||
|
"""Custom Telegram media routine - send mediagroup.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
tg (Telegram): telegram object
|
||||||
|
targets_media_files (dict): {'period':{'name':'local/path/to/file'}}.
|
||||||
|
period (str): 'y','m','w','d'.
|
||||||
|
amount (int): +/- periods.
|
||||||
|
chat (str): unique identifier for the target chat or username of the target channel.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: filename is not local file.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: {'success':bool,'result':response}.
|
||||||
|
"""
|
||||||
|
if Do.args_valid(locals(), Do.tg_routine_media.__annotations__):
|
||||||
|
default_caption = (""
|
||||||
|
+ "`period:` yyyy.mm.dd\n"
|
||||||
|
+ "`source:` https://www.hmp.today/media\n"
|
||||||
|
+ "`stream:` https://youtu.be/ikB20ML5oyo"
|
||||||
|
)
|
||||||
|
group = {
|
||||||
|
"cover":
|
||||||
|
{
|
||||||
|
"type": "photo",
|
||||||
|
"path": (''
|
||||||
|
+ 'https://www.hmp.today/wp-content/uploads/'
|
||||||
|
+ '2021/02/site-slider_hmp-qr_bwg-3840x1705-1.png'
|
||||||
|
),
|
||||||
|
"caption": "source: https://www.hmp.today"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmp_files = []
|
||||||
|
|
||||||
|
date = Do.date_calc(period=period, amount=amount)
|
||||||
|
if period == 'd':
|
||||||
|
caption_date = (''
|
||||||
|
+ str(date['start']['y'])
|
||||||
|
+ '.'
|
||||||
|
+ str(date['start']['m']).zfill(2)
|
||||||
|
+ '.'
|
||||||
|
+ str(date['start']['d']).zfill(2)
|
||||||
|
)
|
||||||
|
if period == 'w':
|
||||||
|
caption_date = (''
|
||||||
|
+ str(date['start']['y'])
|
||||||
|
+ '-w'
|
||||||
|
+ str(date['start']['w']).zfill(2)
|
||||||
|
)
|
||||||
|
if period == 'm':
|
||||||
|
caption_date = (''
|
||||||
|
+ str(date['start']['y'])
|
||||||
|
+ '.'
|
||||||
|
+ str(date['start']['m']).zfill(2)
|
||||||
|
)
|
||||||
|
if period == 'y':
|
||||||
|
caption_date = (''
|
||||||
|
+ str(date['start']['y'])
|
||||||
|
)
|
||||||
|
current_caption = default_caption.replace('yyyy.mm.dd', caption_date)
|
||||||
|
|
||||||
|
for media_name, media_path in targets_media_files[period].items():
|
||||||
|
if re.match("^(?:http://|https://|file_id=)", media_path):
|
||||||
|
# todo: check remote file size
|
||||||
|
raise ValueError(media_name + ' is not local file')
|
||||||
|
else:
|
||||||
|
tg_limit_video_size = 50000000
|
||||||
|
src_video_info = FFmpeg.probe(target=media_path)
|
||||||
|
if src_video_info:
|
||||||
|
cur_video_file = media_path
|
||||||
|
cur_video_duration = int(float(src_video_info['format']['duration']))
|
||||||
|
cur_video_fps = int(
|
||||||
|
src_video_info['streams'][0]['avg_frame_rate'].split('/')[0]
|
||||||
|
)
|
||||||
|
cur_video_width = int(src_video_info['streams'][0]['width'])
|
||||||
|
cur_video_height = int(src_video_info['streams'][0]['height'])
|
||||||
|
|
||||||
|
if stat(media_path).st_size >= tg_limit_video_size:
|
||||||
|
tmp_video_bitrate = int(
|
||||||
|
tg_limit_video_size*0.9/1000*8/cur_video_duration
|
||||||
|
)
|
||||||
|
tmp_video_file = media_path.replace('.mp4', '_compressed.mp4')
|
||||||
|
cur_video_width = int(cur_video_width/2)
|
||||||
|
cur_video_height = int(cur_video_height/2)
|
||||||
|
|
||||||
|
compressing_params = ' '.join(
|
||||||
|
[
|
||||||
|
'-i', media_path,
|
||||||
|
'-c:v libx264 -b:v', str(tmp_video_bitrate) + 'k',
|
||||||
|
'-vf scale='
|
||||||
|
+ str(cur_video_width) + ':' + str(cur_video_height)
|
||||||
|
+ ',fps=' + str(cur_video_fps)
|
||||||
|
+ ',format=yuv420p -preset veryslow',
|
||||||
|
tmp_video_file, '-y -loglevel quiet -stats'
|
||||||
|
]
|
||||||
|
)
|
||||||
|
if FFmpeg.run(raw=compressing_params) == 0:
|
||||||
|
logging.info(msg=media_path + " comressed to " + tmp_video_file)
|
||||||
|
tmp_files.append(tmp_video_file)
|
||||||
|
cur_video_file = tmp_video_file
|
||||||
|
|
||||||
|
response_upload = tg.send_video(
|
||||||
|
chat=chat,
|
||||||
|
video=cur_video_file,
|
||||||
|
width=cur_video_width,
|
||||||
|
height=cur_video_height,
|
||||||
|
duration=cur_video_duration
|
||||||
|
)
|
||||||
|
if response_upload:
|
||||||
|
response_delete = tg.delete_message(
|
||||||
|
chat=chat,
|
||||||
|
message_id=response_upload['result']['result']['message_id']
|
||||||
|
)
|
||||||
|
group[media_name] = {
|
||||||
|
"type": "video",
|
||||||
|
"path": (
|
||||||
|
'file_id='
|
||||||
|
+ response_upload['result']['result']['video']['file_id']
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
response_result = tg.send_mediagroup(
|
||||||
|
chat=chat,
|
||||||
|
media=group,
|
||||||
|
caption=current_caption,
|
||||||
|
parse_mode='Markdown'
|
||||||
|
)
|
||||||
|
for file in tmp_files:
|
||||||
|
if remove(file):
|
||||||
|
logging.info(msg="deleted " + file)
|
||||||
|
return response_result
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
time_start = datetime.now()
|
time_start = datetime.now()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user