将本地文件上传到电报频道
我的目标是进行Python脚本,用于在YT频道上检查新视频,然后下载并将其作为音频上传到TG频道。 我已经完成了检查/下载/转换(youtube_dl库)的第一部分,但看不到如何上传零件。 (有Telegram-upload,Python-Telegram-bot,Telethon库,但我不明白哪一个以及如何申请上传文件以频道)
# 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()
I have a goal to do python script for checking new videos at yt channel then download and upload as audio to tg channel.
I've done first part with checking/downloading/converting (youtube_dl library) and don't see how to do upload part. (there are telegram-upload, python-telegram-bot, telethon libraries but i don't get which one and how I can apply for uploading files to channel)
# 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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
python-telegram-bot
是一个库,它为。telethon
和telegram-upload
而是使用 Telegram API (也称为MTPROTO),它控制用户(以及bot帐户)。如果要使用机器人将文件发送到频道,则必须创建一个机器人并将其作为该频道中的管理员。然后,您可以使用API方法 href =“ https://core.telegram.org/bots/api#sendaudio” rel =“ nofollow noreferrer”>
sendaudio
发送文件。为此,您可以手动向Bot API提出请求(例如urllib
或其他库,例如request
httpx
)或使用库像Python-Telegram-bot
,pytelegrambotapi
,aiogram ,
botogram
等,为BOT API提供包装。您还可以使用telethon
或Proragram
之类的用户库库,因为它们也可以用于bot。如果您不想使用bot,则必须使用用户bot,即电报API,以便您的个人帐户发送音频。为此,您可以使用
telethon
,telegram-upload
或pyrogram
之类的库。python-telegram-bot
is a library that provides a wrapper for the Telegram Bot API.telethon
andtelegram-upload
instead use the Telegram API (also called MTProto), which controls user (and also Bot accounts).If you want to use a bot to send files to the channel, you'll have to create a bot and make it an admin in that channel. Then you can use the api methods
sendDocument
orsendAudio
to send the file. For that you can either manually make requests to the bot api (e.g. via theurllib
or other libraries likerequests
orhttpx
) or use a library likepython-telegram-bot
,pyTelegramBotAPI
,aiogram
,botogram
etc that provide wrappers for the bot api. You can also use user-bot libraries liketelethon
orpyrogram
for that, as they can be used for bots as well.If you don't want to use a bot, you'll have to use a user-bot, i.e. the Telegram API such that the audio is sent by your personal account. For that, you can use libraries like
telethon
,telegram-upload
orpyrogram
.如果您选择使用Telethon,请确保阅读文档的第一个步骤您开始,然后使用 client.send_file :
请注意,
telethon
使用asyncio
,因此,如果您在其他地方使用headering
,请保持警惕。If you choose to use Telethon, be sure to read the First Steps of the documentation to get you started, and then use
client.send_file
:Note that
telethon
usesasyncio
, so be wary if you usethreading
elsewhere.