【问题标题】:Python 2.7.13 Livestreamer Error drving me crazyPython 2.7.13 直播错误让我发疯
【发布时间】:2018-02-18 02:07:00
【问题描述】:

我在 python 方面相对较新,但我正在努力学习。最近几天试图解决这个脚本的一个错误:

import requests
import subprocess
import json
import sys
import multiprocessing
import time
import random

channel_url = "gaming.youtube.com/game/"
processes = [5]


def get_channel():
    # Reading the channel name - passed as an argument to this script
    if len(sys.argv) >= 2:
        global channel_url
        channel_url += sys.argv[1]
    else:
        print "An error has occurred while trying to read arguments. Did you specify the channel?"
        sys.exit(1)


def get_proxies():
    # Reading the list of proxies
    try:
        lines = [line.rstrip("\n") for line in open("proxylist.txt")]
    except IOError as e:
        print "An error has occurred while trying to read the list of proxies: %s" % e.strerror
        sys.exit(1)

    return lines


def get_url():
    # Getting the json with all data regarding the stream
    try:
        response = subprocess.Popen(
            ["livestreamer.exe", "--http-header", "Client-ID=ewvlchtxgqq88ru9gmfp1gmyt6h2b93", 
            channel_url, "-j"], stdout=subprocess.PIPE).communicate()[0]
    except subprocess.CalledProcessError:
        print "An error has occurred while trying to get the stream data. Is the channel online? Is the channel name correct?"
        sys.exit(1)
    except OSError:
        print "An error has occurred while trying to use livestreamer package. Is it installed? Do you have Python in your PATH variable?"

    # Decoding the url to the worst quality of the stream
    try:
        url = json.loads(response)['streams']['audio_only']['url']
    except:
        try:
            url = json.loads(response)['streams']['worst']['url']
        except (ValueError, KeyError):
            print "An error has occurred while trying to get the stream data. Is the channel online? Is the channel name correct?"
            sys.exit(1)

    return url


def open_url(url, proxy):
    # Sending HEAD requests
    while True:
        try:
            with requests.Session() as s:
                response = s.head(url, proxies=proxy)
            print "Sent HEAD request with %s" % proxy["http"]
            time.sleep(20)
        except requests.exceptions.Timeout:
            print "  Timeout error for %s" % proxy["http"]
        except requests.exceptions.ConnectionError:
            print "  Connection error for %s" % proxy["http"]


def prepare_processes():
    global processes
    proxies = get_proxies()
    n = 0

    if len(proxies) < 1:
        print "An error has occurred while preparing the process: Not enough proxy servers. Need at least 1 to function."
        sys.exit(1)

    for proxy in proxies:
        # Preparing the process and giving it its own proxy
        processes.append(
            multiprocessing.Process(
                target=open_url, kwargs={
                    "url": get_url(), "proxy": {
                        "http": proxy}}))

        print '.',

    print ''

if __name__ == "__main__":
    print "Obtaining the channel..."
    get_channel()
    print "Obtained the channel"
    print "Preparing the processes..."
    prepare_processes()
    print "Prepared the processes"
    print "Booting up the processes..."

    # Timer multiplier
    n = 8

    # Starting up the processes
    for process in processes:
        time.sleep(random.randint(1, 5) * n)
        process.daemon = True
        process.start()
        if n > 1:
            n -= 1

    # Running infinitely
    while True:
        time.sleep(1)

错误:

python test999.py UCbadKBJT1bE14AtfBnsw27g
Obtaining the channel...
Obtained the channel
Preparing the processes...
An error has occurred while trying to use livestreamer package. Is it installed? Do you have Python in your PATH variable?
Traceback (most recent call last):
  File "test999.py", line 115, in <module>
    prepare_processes()
  File "test999.py", line 103, in prepare_processes
    "url": get_url(), "proxy": {
  File "test999.py", line 67, in get_url
    url = json.loads(response)['streams']['worst']['url']
UnboundLocalError: local variable 'response' referenced before assignment
  1. 我曾在 Windows 上尝试过,安装了所有模块(并更新了它们)livestreamer、rtmdump、dll 和其他需要的二进制文件。
  2. 在 linux 上:安装 livestreamer、requests、json 和所有需要的模块。还是解决不了。请帮忙!

【问题讨论】:

  • 这与您的操作系统或库无关。您在 try 块内有 response = subprocess.Popen()。如果Popen() 失败,那么response 永远不会存在。随后尝试引用response 的所有代码都将失败。您需要在第一个 except 块内将 response 定义为某个有意义的值 (None?) 并配置后续代码来处理这种情况。
  • 嗯...让我们看看
  • 这正是UnboundLocalError: local variable 'response' referenced before assignment 告诉你的。您正在尝试使用不存在的“变量”。此错误来自 Python 本身,而不是库。
  • 试图在 linux mint 上执行 EXE 文件?它应该如何工作?
  • 您可能希望在第二个除块 (except OSError:) 中使用 sys.exit,就像在第一个中一样。没有它,考虑到堆栈跟踪中显示的程序流程,这个异常非常有意义。

标签: python linux-mint youtube-livestreaming-api livestreamer


【解决方案1】:

response = subprocess.Popen() 在 try 子句中。当它失败时,没有responselkocal 变量。您应该在尝试之前分配响应,并处理 subporcess.Popen() 可能返回的 None 。

【讨论】:

  • 恐怕这没有多大意义。请清理格式,最好举个例子。
  • 已解决。这很简单。我失明了,打电话给 livestreamer.exe :)))
猜你喜欢
  • 2015-03-23
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 2012-11-02
  • 2020-11-13
  • 1970-01-01
相关资源
最近更新 更多