async def _send_media(self, entity, file: File, progress): entity = await self.get_input_entity(entity) supports_streaming = False # TODO fh, fm, _ = await self._file_to_media( file, supports_streaming=file, progress_callback=progress) if isinstance(fm, types.InputMediaUploadedPhoto): r = await self(functions.messages.UploadMediaRequest( entity, media=fm )) fm = utils.get_input_media(r.photo) elif isinstance(fm, types.InputMediaUploadedDocument): r = await self(functions.messages.UploadMediaRequest( entity, media=fm )) fm = utils.get_input_media( r.document, supports_streaming=supports_streaming) return types.InputSingleMedia( fm, message=file.short_name, entities=None, # random_id is autogenerated )
async def send_vid(event): chat_id = event.message.chat_id reply_msg = await event.get_reply_message() num = re.match(r'.+-\d+', reply_msg.text).group() infopage = requests.post('https://www.jav321.com/search', data={'sn': num}) cid = re.sub('https://www.jav321.com/video/', '', infopage.url) cidp = cid[0] + '/' + cid[0:3] + '/' + cid vidurl = 'https://cc3001.dmm.co.jp/litevideo/freepv/' + cidp + '/' + cid + '_mhb_w.mp4' if requests.get(vidurl).status_code == 404: vidurl = 'https://cc3001.dmm.co.jp/litevideo/freepv/' + cidp + '/' + cid + '_dmb_w.mp4' if requests.get(vidurl).status_code == 404: cid = cid[:-5] + cid[-3:] cidp = cid[0] + '/' + cid[0:3] + '/' + cid vidurl = 'https://cc3001.dmm.co.jp/litevideo/freepv/' + cidp + '/' + cid + '_mhb_w.mp4' if requests.get(vidurl).status_code == 404: vidurl = 'https://cc3001.dmm.co.jp/litevideo/freepv/' + cidp + '/' + cid + '_dmb_w.mp4' temp_dir = tempfile.TemporaryDirectory() save_path = temp_dir.name + '/' + cid + '.mp4' with urllib.request.urlopen(vidurl) as response, open(save_path, 'wb') as out_file: shutil.copyfileobj(response, out_file) metadata, mime_type = get_metadata(save_path) with open(save_path, 'rb') as f: input_file = await upload_file(bot, f) input_media = get_input_media(input_file) input_media.attributes = [ DocumentAttributeVideo(round_message=False, supports_streaming=True, **metadata), DocumentAttributeFilename(os.path.basename(save_path)), ] input_media.mime_type = mime_type await bot.send_file(chat_id, input_media) temp_dir.cleanup()
async def send_video(event): chat_id = event.message.chat_id url = re.sub(r'/ytdl\s*', '', event.message.text) logging.info("start %s", url) # if this is in a group/channel if not event.message.is_private and not event.message.text.lower( ).startswith("/ytdl"): logging.info("%s, it's annoying me...🙄️ ", event.message.text) return if not re.findall(r"^https?://", url.lower()): await event.reply( "I think you should send me a link. Don't you agree with me?") return message = await event.reply("Processing...") temp_dir = tempfile.TemporaryDirectory() async with bot.action(chat_id, 'video'): result = await ytdl_download(url, temp_dir.name, chat_id, message) if result["status"]: async with bot.action(chat_id, 'document'): video_path = result["filepath"] await bot.edit_message(chat_id, message, 'Download complete. Sending now...') metadata, mime_type = get_metadata(video_path) with open(video_path, 'rb') as f: input_file = await upload_file( bot, f, progress_callback=lambda x, y: upload_callback( x, y, chat_id, message)) input_media = get_input_media(input_file) file_name = os.path.basename(video_path) input_media.attributes = [ DocumentAttributeVideo(round_message=False, supports_streaming=True, **metadata), DocumentAttributeFilename(file_name), ] input_media.mime_type = mime_type # duration here is int - convert to timedelta metadata["duration_str"] = datetime.timedelta( seconds=metadata["duration"]) metadata["size"] = sizeof_fmt(os.stat(video_path).st_size) caption = "{name}\n{duration_str} {size} {w}*{h}".format( name=file_name, **metadata) await bot.send_file(chat_id, input_media, caption=caption) await bot.edit_message(chat_id, message, 'Download success!✅') else: async with bot.action(chat_id, 'typing'): tb = result["error"][0:4000] await bot.edit_message(chat_id, message, f"{url} download failed❌:\n```{tb}```", parse_mode='markdown') temp_dir.cleanup()
def test_game_input_media_memory_error(): large_long = 2**62 media = MessageMediaGame(Game( id=large_long, # <- key to trigger `MemoryError` access_hash=large_long, short_name='short_name', title='title', description='description', photo=PhotoEmpty(large_long), )) input_media = utils.get_input_media(media) bytes(input_media) # <- shouldn't raise `MemoryError`