Twitch Viewer机器人脚本中的Python线程错误

时间:2018-12-06 11:18:14

标签: python twitch viewer

我当前正在使用:

Twitch Viewer Bot

并且一直在调整代码以使其正常运行。这是我目前拥有的:

import requests
import subprocess
import json
import sys
import threading
import time
from multiprocessing import Queue
import urllib3
import urllib3.contrib.pyopenssl

urllib3.contrib.pyopenssl.inject_into_urllib3()
#urllib3.disable_warnings() unverified HTTPS requests
numberOfViewers = int(sys.argv[1])
builderThreads = int(sys.argv[2])
startTime = time.time()
numberOfSockets = 0
concurrent = 25
urls = []
urlsUsed = []
proxies = {
    'http': 'http://173.201.183.172:8000',
    'http': 'http://94.181.34.64:81',
}


# def getURL(): # Get tokens
#   output = subprocess.Popen(["livestreamer", "twitch.tv/swagvyper", "-j"], stdout=subprocess.PIPE).communicate()[0]
#   return json.loads(output)['streams']['worst']['url'] # Parse json and return the URL parameter

def getURL():
    output = urlopen('http://www.twitch.tv/CHANNEL') # Any URL
    return json.load(output)['streams']['worst']['url']

def build(): # Builds a set of tokens, aka viewers
    global numberOfSockets
    global numberOfViewers
    while True:
        if numberOfSockets < numberOfViewers:
            numberOfSockets += 1
            print("Building viewers " + str(numberOfSockets) + "/" + str(numberOfViewers))
            urls.append(getURL())

def view(): # Opens connections to send views
    global numberOfSockets
    while True:
        url=q.get()
        # requests.head(url, proxies=proxies)
        requests.head(url) # Sending a HEAD request is enough to count as a view
        if (url in urlsUsed):
            urls.remove(url)
            urlsUsed.remove(url)
            numberOfSockets -= 1
        else:
            urlsUsed.append(url)
        q.task_done()

if __name__ == '__main__':
    for i in range(0, builderThreads):
        threading.Thread(target = build).start()

    while True:
        while (numberOfViewers != numberOfSockets): # Wait until sockets are built
            time.sleep(1)

        q=Queue(concurrent*2)
        for i in range(concurrent):
            try:
                t=threading.Thread(target=view)
                t.daemon=True
                t.start()
            except:
                print('thread error')
        try:
            for url in urls:
                print(url)
                q.put(url.strip())
                q.join()
        except KeyboardInterrupt:
            sys.exit(1)

但是,在运行时,我似乎收到了此错误,并且我不能100%确定从这里出发的地方:

thread errorException in thread Thread-1:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
File "C:\Program Files (x86)\Python36-32\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
File "bot.py", line 41, in build
    urls.append(getURL())
File "bot.py", line 31, in getURL
    output = urlopen('http://www.twitch.tv/dsboywonder25') # Any URL
NameError: name 'urlopen' is not defined

Traceback (most recent call last):
File "bot.py", line 70, in <module>
File "C:\Program Files (x86)\Python36-32\lib\threading.py", line 846, in start
MemoryError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "bot.py", line 72, in <module>
MemoryError

我想知道是否有人以前见过这个,或者甚至指向我正确的方向。提前谢谢您:)

1 个答案:

答案 0 :(得分:0)

  

getURL中的文件“ bot.py”,第31行       output = urlopen('http://www.twitch.tv/dsboywonder25')#任何网址   NameError:名称'urlopen'未定义

urlopen未定义:您没有从任何地方声明或导入CALL apoc.periodic.iterate('call apoc.load.jdbc("connection string", "select distinct SRC_SYS_KEY_TXT, ASSIGNED_FR_RFO_SRC_KEY_TXT from neo4j_extract where ASSIGNED_FR_RFO_SRC_KEY_TXT in ('0383')")', 'merge (R:Rfo {rfo_id:row.ASSIGNED_FR_RFO_SRC_KEY_TXT}) with row,R merge (P:Person {person_id:toint(row.SRC_SYS_KEY_TXT)}) with R,P merge (R)-[:PROVIDES_SERVICES_TO]->(P)', {batchSize:1000, parallel:true}); ,因此脚本不知道您在说什么。

urlopenurlopen / urllib的方法。您已经导入了urllib2requests,这是另外两个用于发出http请求的模块。您需要确定哪个模块对给定应用程序最有用,并使用适当的方法打开连接/发出所需的请求。