From f85f7b2efe234248f7fd60dd7982721bef6e00b9 Mon Sep 17 00:00:00 2001 From: AAA <35992542+TNTcraftHIM@users.noreply.github.com> Date: Sat, 5 Sep 2020 22:10:05 +0800 Subject: [PATCH] =?UTF-8?q?VideoDL=E6=B5=8B=E8=AF=95=E7=89=88=E6=94=BE?= =?UTF-8?q?=E5=87=BA=EF=BC=8C=E5=8F=AF=E4=B8=8B=E8=BD=BDYTB/B=E7=AB=99?= =?UTF-8?q?=E8=A7=86=E9=A2=91=EF=BC=88=E4=BD=86=E6=98=AF=E4=B8=8D=E5=A4=AA?= =?UTF-8?q?=E5=A5=BD=E7=94=A8=20:P=EF=BC=89=20(#56)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: TNTcraftHIM --- list.json | 10 ++++ videodl.py | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 videodl.py diff --git a/list.json b/list.json index 79a0090..4f6f206 100644 --- a/list.json +++ b/list.json @@ -299,6 +299,16 @@ "supported": true, "des-short": "一键扬了群", "des": "⚠⚠慎用! 一件扬了群内所有成员⚠⚠" + }, + { + "name": "videodl", + "version": "0.1", + "section": "daily", + "maintainer": "TNTcraftHIM", + "size": "5.3", + "supported": true, + "des-short": "下载YTB/B站视频", + "des": "随手写的youtube/B站下载视频插件,B站的似乎因为TG编码问题不支持在线播放,先这样吧(我懒)。命令:vdl。" } ] } diff --git a/videodl.py b/videodl.py new file mode 100644 index 0000000..29bc661 --- /dev/null +++ b/videodl.py @@ -0,0 +1,136 @@ +# -*- coding: utf-8 -*- +import requests +import json +from os import remove, mkdir +from os.path import exists +from re import compile as regex_compile +from pagermaid import bot, log +from pagermaid.listener import listener +from telethon.tl.types import DocumentAttributeVideo +from time import sleep +from re import search + + +@listener(outgoing=True, command="vdl", + description="下载 YouTube/bilibili 视频并上传", + parameters="") +async def vdl(context): + url = context.arguments + reply = await context.get_reply_message() + reply_id = None + await context.edit("视频获取中 . . .") + if reply: + reply_id = reply.id + if url is None: + await context.edit("出错了呜呜呜 ~ 无效的参数。") + return + + bilibili_pattern = regex_compile( + r"^(http(s)?://)?((w){3}.)?bilibili(\.com)?/.+") + youtube_pattern = regex_compile( + r"^(http(s)?://)?((w){3}.)?youtu(be|.be)?(\.com)?/.+") + if youtube_pattern.match(url): + try: + from pytube import YouTube + except ImportError: + await context.edit('(`pytube3`支持库未安装,YouTube视频无法下载\n请使用 `-sh` `pip3` `install` `pytube3` 安装,或自行ssh安装)') + return + url = url.replace('www.youtube.com/watch?v=', 'youtu.be/') + if not await youtube_dl(url, context, reply_id): + await context.edit("出错了呜呜呜 ~ 视频下载失败。") + sleep(3) + await log(f"已拉取 YouTube 视频,地址: {url}.") + await context.delete() + elif bilibili_pattern.match(url): + if not await bilibili_dl(url, context, reply_id): + await context.edit("出错了呜呜呜 ~ 视频下载失败。") + sleep(3) + await log(f"已拉取 bilibili 视频,地址: {url}.") + await context.delete() + else: + await context.edit("出错了呜呜呜 ~ 无效的网址。") + + +async def youtube_dl(url, context, reply_id): + from pytube import YouTube + video = YouTube(url) + title = video.title + filename = title + ".mp4" + duration = video.length + await context.edit("视频下载中 . . .") + YouTube(url).streams.filter( + file_extension='mp4').get_highest_resolution().download() + if not exists(filename): + return False + await upload(False, filename, context, reply_id, title, duration) + return True + + +async def bilibili_dl(url, context, reply_id): + url = 'https://tenapi.cn/bilivideo/?url=' + url + headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063', "X-Real-IP": "223.252.199.66"} + for _ in range(20): # 最多尝试20次 + status = False + try: + req = requests.request('GET', url, headers=headers) + if req.status_code == 200: + req = json.loads(req.content) + if req['title']: + title = req['title'] + url = req['url'] + status=True + break + else: + return False + except: + continue + if status is False: + await context.client.send_message(context.chat_id, "出错了呜呜呜 ~ 试了好多好多次都无法访问到 API 服务器 。") + return False + filename = title + ".flv" + video = requests.get(url, headers=headers) + await context.edit("视频下载中 . . .") + with open(filename, 'wb') as f: + f.write(video.content) + await upload(True, filename, context, reply_id, title) + return True + + +async def upload(as_file, filename, context, reply_id, caption, duration=0): + if not exists("plugins/VideoDL/FastTelethon.py"): + if not exists("plugins/VideoDLExtra"): + mkdir("plugins/VideoDLExtra") + faster = requests.request( + "GET", "https://gist.githubusercontent.com/TNTcraftHIM/ca2e6066ed5892f67947eb2289dd6439/raw/86244b02c7824a3ca32ce01b2649f5d9badd2e49/FastTelethon.py") + for _ in range(6): # 最多尝试6次 + if faster.status_code == 200: + with open("plugins/VideoDLExtra/FastTelethon.py", "wb") as f: + f.write(faster.content) + break + try: + from VideoDLExtra.FastTelethon import upload_file + file = await upload_file(context.client, open(filename, 'rb'), filename) + except: + file = filename + await context.client.send_message(context.chat_id, '(`FastTelethon`支持文件导入失败,上传速度可能受到影响)') + await context.edit("视频上传中 . . .") + if as_file is True: + await context.client.send_file( + context.chat_id, + file, + caption=caption, + link_preview=False, + force_document=True, + reply_to=reply_id + ) + else: + await context.client.send_file( + context.chat_id, + file, + caption=caption, + link_preview=False, + force_document=False, + attributes=(DocumentAttributeVideo(duration, 0, 0),), + reply_to=reply_id + ) + remove(filename)