【问题标题】:a python-telegram-bot Unsupported url protocol一个 python-telegram-bot 不支持的 url 协议
【发布时间】:2021-11-05 19:26:57
【问题描述】:

所以,我试图创建一个 Telegram 机器人,它使用 PyTube Moudle 将视频下载到我的计算机,然后通过 send_document 函数将其发送给用户。

我的问题是当我需要发送视频时,出现此错误:

telegram.error.BadRequest: Invalid file http url specified: unsupported url protocol

我已经在常规文本文件上尝试过,得到了相同的结果。

我认为这是因为文件的 URL 违反了一些关于它应该是什么样子的规则,但我不知道如何修复它......

这也是我的代码:

import telegram.ext
import telegram
from telegram.ext.messagehandler import MessageHandler
from telegram.ext.commandhandler import CommandHandler
from telegram.ext.filters import Filters
import time
import pytube
import os

with open('C:/Users/EvilTwin/Desktop/stuff/Python/API/Telegram/Download Youtube Videos Bot/token.txt')as file:
    API_KEY = file.read()


updater = telegram.ext.Updater(token=API_KEY, use_context=True)


def start(update, context):
    update.message.reply_text(f'Hello {update.effective_user.first_name}, I\'m Roee\'s Video Downloader!')
    time.sleep(1)
    update.message.reply_text(    
    f'''
To Download Videos Please Enter:
/download + Video URL  + Format
    
    ''')

def download(update, context):
    URL = context.args[0]
    FORMAT = context.args[1]
    VIDEO = pytube.YouTube(URL)
    FILE = VIDEO.streams.filter(progressive=True, file_extension=FORMAT).order_by('resolution').desc().first() 
    VD = 'C:/Users/EvilTwin/Desktop/stuff/Python/API/Telegram/Download Youtube Videos Bot/Videos'
    FILE.download(VD)
    banned = ['/', '/-\\', ':', '?', '!', '*', '>', '<', '"', '|']
    for ban in  banned:
        VIDEO.title = VIDEO.title.replace(ban, '')
    time.sleep(1)
    DOC = f'{VD}/{VIDEO.title}.{FORMAT}'
    chat_id=update.effective_chat.id
    context.bot.send_document(chat_id = chat_id ,document=DOC, filename = f'video.{FORMAT}')
    os.remove(DOC)



updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('download', download))

updater.start_polling()
updater.idle()

如果有人知道如何修复它,请帮助我....

【问题讨论】:

  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是截图,不是链接到外部门户)有问题(不是评论)。还有其他有用的信息。
  • url 表示以https://http:// 和本地文件开头的字符串file:// - 此值描述"protocol"。而你的错误表明你使用了unsupported url protocol
  • 你没有显示完整的错误信息,所以很难说是什么问题。并且不要期望我们会运行代码来查看错误。此外,它可以在我们的计算机上正常工作。您必须添加所有有问题的详细信息(而不是在 cmets 中)。错误应该告诉你你在哪一行有问题,首先你可以使用print()查看你在这一行的变量中有什么——它可以解释什么会造成问题。

标签: python python-telegram-bot pytube


【解决方案1】:

对于本地文件,您必须使用 file object (file handler) 而不是 file name
所以你必须打开它document=open(DOC, 'rb')

context.bot.send_document(chat_id=chat_id, document=open(DOC, 'rb'), filename=f'video.{FORMAT}')

文档还显示您可以使用pathlib.Pathbytes(因此需要阅读它document=open(DOC, 'rb').read())但我没有测试它。

见文档:send_document()

【讨论】:

    猜你喜欢
    • 2018-12-02
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多