Compare commits

..

No commits in common. "e869ec8373160519099f19c2ab4c20e4c7a02b33" and "98599a5092b44aa0b94b2704b03e0d527a59a9c3" have entirely different histories.

1 changed files with 5 additions and 47 deletions

View File

@ -9,7 +9,6 @@ import tempfile
import functools
import mimetypes
import traceback
from decimal import Decimal
from itertools import zip_longest
from urllib.parse import urlparse, urlunparse, urljoin
import yaml
@ -19,7 +18,6 @@ import aiocron
from bs4 import BeautifulSoup
from telethon import TelegramClient, events
from telethon.utils import chunks, is_list_like
from telethon.tl.types import DocumentAttributeVideo
mimetypes.init(['mime.types'])
with open('config.yaml') as file:
@ -195,8 +193,11 @@ async def main():
await add_chat(chat, send_to_chats[chat])
async def _start_broadcast(text, file, chats):
uploaded_files = []
for i in file or []:
uploaded_files.append(await client.upload_file(i))
for chat in chats:
for i in chunks(zip_longest(text, file or []), 10):
for i in chunks(zip_longest(text, uploaded_files), 10):
j, k = zip(*i)
if not any(k):
k = None
@ -206,37 +207,7 @@ async def main():
if len(j) == 1 and len(k) == 1:
j = j[0]
k = k[0]
attributes = []
mimetype = await _get_file_mimetype(k)
if mimetype.startswith('video/'):
try:
data = await _get_video_data(k)
duration = int(Decimal(data['format']['duration']))
w = h = None
for l in data['streams']:
if l['codec_type'] != 'video':
continue
w = l['width']
h = l['height']
break
except Exception:
logging.exception('Exception when getting video data')
else:
attributes.append(DocumentAttributeVideo(duration, w, h, supports_streaming=mimetype == 'video/mp4'))
await client.send_message(chat, j, file=k, link_preview=False, attributes=attributes)
async def _get_video_data(filename):
proc = await asyncio.create_subprocess_exec('ffprobe', '-show_format', '-show_streams', '-print_format', 'json', filename, stdout=asyncio.subprocess.PIPE)
stdout, _ = await proc.communicate()
data = json.loads(stdout)
if data.get('format') and 'duration' not in data['format']:
with tempfile.NamedTemporaryFile() as tempf:
proc = await asyncio.create_subprocess_exec('ffmpeg', '-an', '-sn', '-i', filename, '-c', 'copy', '-f', 'matroska', tempf.name)
await proc.communicate()
ndata = await _get_video_data(tempf.name)
if ndata.get('format') and 'duration' in ndata['format']:
data['format']['duration'] = ndata['format']['duration']
return data
await client.send_message(chat, j, file=k, link_preview=False)
async def _download_file(filename, url):
print(url)
@ -416,19 +387,6 @@ async def main():
if pmedia:
pmedia = urljoin(url, pmedia)
await _download_file(filename, pmedia)
if await _get_file_mimetype(filename) == 'video/x-m4v':
ofilename = filename + '.oc'
os.rename(filename, ofilename)
proc = await asyncio.create_subprocess_exec('ffmpeg', '-nostdin', '-y', '-i', ofilename, '-c', 'copy', '-f', 'mp4', filename)
await proc.communicate()
if not proc.returncode:
os.remove(ofilename)
else:
os.rename(ofilename, filename)
try:
os.remove(filename)
except FileNotFoundError:
pass
files.append(filename)
if pdesc:
caplength = 1023 if pmedia else 4095