我有一个目标,做python脚本检查新的视频在yt频道,然后下载并上传为音频到tg频道。我已经完成了检查/下载/转换(youtube_dl库)的第一部分,不知道如何做上传部分。(有telegram-upload, python-telegram-bot, telethlibraries,但我不知道是哪一个,也不知道如何申请上传文件到频道)
# importing module
import youtube_dl
import urllib.request
import re
html = urllib.request.urlopen("https://www.youtube.com/c/peterschiff/videos")
#all videos ids from yt page
video_ids = re.findall(r"watch?v=(S{11})", html.read().decode())
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}]
}
#write all videos ids to local file
txt_file = open('outfile.txt', 'r')
file_content = txt_file.read()
content_list = file_content.split()
txt_file.close()
x = video_ids
y = content_list
#get only new videos by comparing with local file
result = set(x) - set(y)
with open('outfile.txt', 'a') as outfile:
outfile.write('n'.join(result))
#download new videos and convert to audio
def dwl_vid():
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([zxt])
for item in result:
video_one = 'https://www.youtube.com/watch?v=' + item
zxt = video_one.strip()
dwl_vid()
python-telegram-bot
是一个为Telegram Bot API提供包装的库。telethon
和telegram-upload
使用Telegram API(也称为MTProto)来控制用户(以及Bot帐户)。
如果您想使用bot向通道发送文件,您必须创建一个bot并将其设置为该通道中的管理员。然后,您可以使用api方法sendDocument
或sendAudio
来发送文件。为此,您可以手动向bot api发出请求(例如通过urllib
或其他库,如requests
或httpx
)或使用python-telegram-bot
,pyTelegramBotAPI
,aiogram
,botogram
等库,为bot api提供包装。你也可以使用用户bot库,如telethon
或pyrogram
,因为它们也可以用于bot。
如果你不想使用bot,你必须使用user-bot,即Telegram API,这样音频是由你的个人帐户发送的。为此,您可以使用telethon
,telegram-upload
或pyrogram
等库。
如果您选择使用Telethon,请务必阅读文档的第一步来开始,然后使用client.send_file
:
await client.send_file(chat, '/my/videos/video.mp4', caption="Test video")
注意telethon
使用asyncio
,所以如果你在其他地方使用threading
要小心。