mirror of
https://github.com/TeamPGM/PagerMaid_Plugins.git
synced 2024-11-22 04:29:16 +00:00
VideoDL测试版放出,可下载YTB/B站视频(但是不太好用 :P) (#56)
Co-authored-by: TNTcraftHIM <TNTcraftHIM@users.noreply.github.com>
This commit is contained in:
parent
72922d3bc0
commit
f85f7b2efe
10
list.json
10
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。"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
136
videodl.py
Normal file
136
videodl.py
Normal file
@ -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="<url>")
|
||||
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)
|
Loading…
Reference in New Issue
Block a user