-
-
Save Xeroday/6468146 to your computer and use it in GitHub Desktop.
import requests | |
import subprocess | |
import json | |
import sys | |
import threading | |
import time | |
from Queue import Queue | |
numberOfViewers = int(sys.argv[1]) | |
builderThreads = int(sys.argv[2]) | |
startTime = time.time() | |
numberOfSockets = 0 | |
concurrent = 25 | |
urls = [] | |
urlsUsed = [] | |
def getURL(): # Get tokens | |
output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"], stdout=subprocess.PIPE).communicate()[0] | |
return json.loads(output)['streams']['worst']['url'] # Parse json and return the URL parameter | |
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) | |
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) |
Means that json.loads can't even begin to find something that looks like JSON at the start of the string
Means that json.loads can't even begin to find something that looks like JSON at the start of the string
To get this to work what you need to do is remove one indent from line 19. Line 18 and 19 has to be indented the same.
From there I got the script to work without any errors. Although I am guessing Twitch is blocking the traffic. I see the traffic being sent, but I do not see the increase in viewers on my testing stream
I seem to have to update the code a little to get it running. I THINK its correct:
import requests
import subprocess
import json
import sys
import threading
import time
from multiprocessing import Queue
numberOfViewers = int(sys.argv[1])
builderThreads = int(sys.argv[2])
startTime = time.time()
numberOfSockets = 0
concurrent = 25
urls = []
urlsUsed = []
def getURL(): # Get tokens
output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"], stdout=subprocess.PIPE).communicate()[0]
return json.loads(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)
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)
However, I appear to always get this error:
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 "twitch.py", line 28, in build
urls.append(getURL())
File "twitch.py", line 18, in getURL
output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"], stdout=subprocess.PIPE).communicate()[0]
File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Traceback (most recent call last):
File "twitch.py", line 56, 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 "twitch.py", line 58, in <module>
MemoryError
When running python twitch.py 1 1
. Any ideas what I am missing here?
I seem to have to update the code a little to get it running. I THINK its correct:
import requests import subprocess import json import sys import threading import time from multiprocessing import Queue numberOfViewers = int(sys.argv[1]) builderThreads = int(sys.argv[2]) startTime = time.time() numberOfSockets = 0 concurrent = 25 urls = [] urlsUsed = [] def getURL(): # Get tokens output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"], stdout=subprocess.PIPE).communicate()[0] return json.loads(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) 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)
However, I appear to always get this error:
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 "twitch.py", line 28, in build urls.append(getURL()) File "twitch.py", line 18, in getURL output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"], stdout=subprocess.PIPE).communicate()[0] File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 997, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified Traceback (most recent call last): File "twitch.py", line 56, 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 "twitch.py", line 58, in <module> MemoryError
When running
python twitch.py 1 1
. Any ideas what I am missing here?
There's for sure a memory leak in there somewhere. I just ran your code and my memory maxes out after a bit. That would be the cause.
After doing some debugging, using that url will not work as it returns error 400 client error. Likely the root cause. If you open the api url it performs the request to, the error given by twitch is 'No client id specified'.
I seem to have to update the code a little to get it running. I THINK its correct:
import requests import subprocess import json import sys import threading import time from multiprocessing import Queue numberOfViewers = int(sys.argv[1]) builderThreads = int(sys.argv[2]) startTime = time.time() numberOfSockets = 0 concurrent = 25 urls = [] urlsUsed = [] def getURL(): # Get tokens output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"], stdout=subprocess.PIPE).communicate()[0] return json.loads(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) 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)
However, I appear to always get this error:
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 "twitch.py", line 28, in build urls.append(getURL()) File "twitch.py", line 18, in getURL output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"], stdout=subprocess.PIPE).communicate()[0] File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 997, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified Traceback (most recent call last): File "twitch.py", line 56, 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 "twitch.py", line 58, in <module> MemoryError
When running
python twitch.py 1 1
. Any ideas what I am missing here?There's for sure a memory leak in there somewhere. I just ran your code and my memory maxes out after a bit. That would be the cause.
After doing some debugging, using that url will not work as it returns error 400 client error. Likely the root cause. If you open the api url it performs the request to, the error given by twitch is 'No client id specified'.
Maybe i missed something too and im probably a bit too late. But in your preview it says CHANNEL_NAME and not an actual channel. That might be why it says code 400 cause there arent any twitch.tv/CHANNEL_NAME at the time you ran the program. Try it with an actual channelname instead
EDIT: Wait nevermind seems to get the same error as you are describing
unfortunately the script does not work. can someone help me?
error:
Traceback (most recent call last):
File "C:\Users\Pik\PycharmProjects\pythonProject\klickbot\gdh.py", line 9, in
numberOfViewers = int(sys.argv[1])
IndexError: list index out of range
Did you manage to get this to work? I ended up with the error: ValueError: No JSON object could be decoded