将本地文件上传到电报频道

发布于 2025-02-09 05:40:45 字数 1236 浏览 1 评论 0原文

我的目标是进行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅冕 2025-02-16 05:40:45

python-telegram-bot是一个库,它为telethontelegram-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-botpytelegrambotapiaiogram ,botogram等,为BOT API提供包装。您还可以使用telethonProragram之类的用户库库,因为它们也可以用于bot。

如果您不想使用bot,则必须使用用户bot,即电报API,以便您的个人帐户发送音频。为此,您可以使用telethontelegram-uploadpyrogram之类的库。

python-telegram-bot is a library that provides a wrapper for the Telegram Bot API. telethon and telegram-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 or sendAudio to send the file. For that you can either manually make requests to the bot api (e.g. via the urllib or other libraries like requests or httpx) or use a library like python-telegram-bot, pyTelegramBotAPI, aiogram, botogram etc that provide wrappers for the bot api. You can also use user-bot libraries like telethon or pyrogram 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 or pyrogram.

長街聽風 2025-02-16 05:40:45

如果您选择使用Telethon,请确保阅读文档的第一个步骤您开始,然后使用 client.send_file

await client.send_file(chat, '/my/videos/video.mp4', caption="Test video")

请注意,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:

await client.send_file(chat, '/my/videos/video.mp4', caption="Test video")

Note that telethon uses asyncio, so be wary if you use threading elsewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文