1

所以,我做了一个不和谐的机器人,我需要在播放音乐时从播放列表中提取信息,因为我的互联网速度很慢。这是我尝试过的代码。

import sys
from youtube_dl import YoutubeDL
import threading
import time

info = []

class Logger():
    def debug(self, msg):
        print(f"Debug -> {msg}")
    def error(self, msg):
        print(f"Error -> {msg}")
    def warning(self, msg):
        print(f"Warning -> {msg}")

def buffer():
    global info
    url = "playlist"

    ytdl_opts = {
        "cookiefile": "cookies.txt",
        "logger": Logger(),
    }

    ytdl = YoutubeDL(ytdl_opts)
    info = ytdl.extract_info(url, download=False)

try:
    task = threading.Thread(target=buffer)
    task.daemon = True
    task.start()

    while task.is_alive():
        time.sleep(2)
        try:
            length = len(info["entries"])
            print(f"Length -> {length}")
        except TypeError:
            print(f"No entry.")

except KeyboardInterrupt:
    sys.exit(f"Interupt.")

我想在提取 url 时也增加长度,这样我就可以从那里访问几个信息。提前致谢。

4

0 回答 0