ohyou / twitch-viewer

A viewer bot for twitch.tv
Apache License 2.0
199 stars 103 forks source link

Unbound Local Error: local variable 'response' referenced before assignment #54

Closed pythonnoob2 closed 4 years ago

pythonnoob2 commented 4 years ago

Does anyone know a fix for this error? I've tried looking around the web, but can't figure out how to fix it. I am new to python still.

Thanks

ghost commented 4 years ago

You need to assign response as None at the beginning of your program, like this: response = None Then you have to put in the first line of the functions which use the variable: global response

ghost commented 4 years ago

here is the code https://pastebin.com/5wWC1SRs

pythonnoob2 commented 4 years ago

Thank you for the fix! That corrected the error I was having, but I am not presented with another error.

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 "twitch-viewer.py", line 119, in <module>     prepare_processes()   File "twitch-viewer.py", line 106, in prepare_processes     "url": get_url(), "proxy": {   File "twitch-viewer.py", line 69, in get_url     url = json.loads(response)['streams']['worst']['url']   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads     return _default_decoder.decode(s)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode     obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer

Any ideas? I notice the errors are about livestreamer, which I have installed and can provide screenshot if needed, and the PATH error I'm not to sure about. I googled it but from what I found I think I have that done properly as well.

Is it just not working because livestreamer isn't working?

ghost commented 4 years ago

did you pip2 install livestreamer or pip3 install livestreamer?

pythonnoob2 commented 4 years ago

I did. Here is everything that is installed for the project

https://imgur.com/a/niMmGea

pythonnoob2 commented 4 years ago

I started a fresh project and I only have livestreamer and requests installed. I no longer get the above error, but now I get "An error has occurred while trying to get the stream data. Is the channel online? Is the channel name correct?" the channel is 100% live.

Edit: just to be clear. I am setting the channel name when running the script, not within the code. ex: python twitch-viewer.py CHANNELNAME

Do I need to add a viewer number after the channel name?

ghost commented 4 years ago

which OS are you using? send a screenshot what you do and the log.

pythonnoob2 commented 4 years ago

There has been more development since my last update. I've switch to windows, was previously using Mac. I've removed livestreamer.exe and started using streamlink, since I found out livestreamer is dead and streamlink was the replacement. I am able to get passed the error I was having, but now when it gets to preparing process it just keeps showing period after period and nothing happens. No views, no errors, nothing. Just gets hung on the periods. See screenshot for more info.

Here is the code: https://pastebin.com/jxpRLYcE

Here is the screenshot: https://imgur.com/a/6uNrISt

ghost commented 4 years ago

how many lines do you have in proxylist.txt?

pythonnoob2 commented 4 years ago

I wasn't sure what proxies to use (HTTP, Socks4 or Socks5) but I've tested with all 3. Each have over 1,000 lines. HTTP is 1,500. Socks4 is about 1200 and Socks5 is 8,600.

Should I limit those?

ghost commented 4 years ago

I wasn't sure what proxies to use (HTTP, Socks4 or Socks5) but I've tested with all 3. Each have over 1,000 lines. HTTP is 1,500. Socks4 is about 1200 and Socks5 is 8,600.

Should I limit those?

yes, the program will print "." for every line in the txt file Check line 4 in proxylist.txt

pythonnoob2 commented 4 years ago

Ok, I will limit them to something much much smaller.

Line 4 looks like a normal IP to me. Something wrong with it? I am just using free poxies I've found on the web. Could also be my problem?

pythonnoob2 commented 4 years ago

Ok, the bot is sending head requests now. I limited the lines to only 50. All the requests sent successfully but no views were added to the channel.

Edit: same results using the first 3 lines only. Maybe I need new proxies?

ghost commented 4 years ago

try only http

pythonnoob2 commented 4 years ago

Ok, I can try searching for some reliable free http. I will update once I find some and test!

Thank you for all the help

ghost commented 4 years ago

does streamlink have the same arguments of livestreamer?

pythonnoob2 commented 4 years ago

Honestly, no idea lol. I just saw that livestreamer was no longer supported and that streamlink was the new thing to use. I am a big noob when it comes to this stuff.

ghost commented 4 years ago

try to use "streamerlink.exe" on cmd.

pythonnoob2 commented 4 years ago

I'm sorry, but how do I do that? I am very new to python. I do appreciate everything you've been helping with.

ghost commented 4 years ago

windows key + r then write cmd and in cmd write streamlink.exe

pythonnoob2 commented 4 years ago

oh you meant windows cmd, sorry. Nothing runs when i type streamlink.exe. "not recognized"

ghost commented 4 years ago

You need to have the exe file in your twitch-viewer folder. You can create a shortcut

pythonnoob2 commented 4 years ago

Ok, I will try and add that to the folder. See if it does anything. So installing streamlink with pip in python does nothing?

ghost commented 4 years ago

try https://github.com/streamlink/streamlink here is for windows: https://github.com/streamlink/streamlink/releases/tag/1.3.1

pythonnoob2 commented 4 years ago

Ok, I have it downloaded now. my twitch viewer folder is only in python. So i have to add the exe in there? or I am stupid and its actually located somewhere in my users folder. So find it in my users folder and add the exe there?

Edit: found the users folder. No idea where the .exe for streamlink is. Found streamlink in C: Program Files, but I don't see an exe in there that i can copy and put in there or make a shortcut.

ghost commented 4 years ago

do you have windows 10?

pythonnoob2 commented 4 years ago

I do. I made a shortcut to the streamlink folder in my twitch-viewer folder. Try rerunning code now?

ghost commented 4 years ago

I should be the exe. Search streamlink (using windows key)... right click and make shortcut

pythonnoob2 commented 4 years ago

Ok, exe shortcut placed in the folder.

edit: rerun code in pycharm?

ghost commented 4 years ago

now try the python program. if not working try writing streamlink on cmd

pythonnoob2 commented 4 years ago

same result. Send head requests but no views are going to channel. :(

ghost commented 4 years ago

but did streamlink work on cmd?

pythonnoob2 commented 4 years ago

Yeah, it opens up and says this:

usage: streamlink [OPTIONS] [STREAM]

Use -h/--help to see the available options or read the manual at https://streamlink.github.io

ghost commented 4 years ago

ok we probably have only to change arguments in py file :) I hope...

pythonnoob2 commented 4 years ago

I hope so too! So, how exactly do we change the arguments? I am sorry I am such a noob. Hence the name lol

ghost commented 4 years ago

here https://pastebin.com/eejTTTcH :)

pythonnoob2 commented 4 years ago

I also might need to find new list of proxies. The ones I have might suck and not work

edit: ill trythat now!

ghost commented 4 years ago

oops I made a mistake.

ghost commented 4 years ago

https://pastebin.com/bcYHUaM6 :)

pythonnoob2 commented 4 years ago

I tried your updated code you just posted, but for some reason I am getting this error:

An error has occurred while trying to get the stream data. Is the channel online? Is the channel name correct?

Haven't had that happen in a while. The channel is still online

ghost commented 4 years ago

I have to edit more i think

pythonnoob2 commented 4 years ago

i noticed there is no client ID in your new code following streamlink. Do we not need that? I assumed that is how we were making the connection to twitch.

ghost commented 4 years ago

but with streamlink?

pythonnoob2 commented 4 years ago

In the old code it looked like this:

response = subprocess.Popen( ["livestreamer.exe", "--http-header", "Client-ID=ewvlchtxgqq88ru9gmfp1gmyt6h2b93", channel_url, "-j"], stdout=subprocess.PIPE).communicate()[0]

When I made edits to that I changed livestreamer.exe to streamlink.exe and changed the client ID to the new Twitch API ID (According to google).

Wasn't sure if that was needed or not and I noticed it's not in the new code you shared. I was just looking for differences lol no idea if its need at all.

ghost commented 4 years ago

I didnt read the whole doc, but it has to be similar to livestreamer https://pastebin.com/xGQemjW8

ghost commented 4 years ago

if not working you will have to add a print but first tell me the error and... yes I did put a print

pythonnoob2 commented 4 years ago

Ok, so it throws a really big error, BUT the script keeps working and eventually sends the head requests. Doesn't add viewers though. Had to put this error in pastbin because it was massive lol

https://pastebin.com/pL6QGbb8

ghost commented 4 years ago

it wasnt an error, it was just a print() xD

ghost commented 4 years ago

it wasnt an error, it was just a print xD

pythonnoob2 commented 4 years ago

oh haha ok. Never saw that before! Wasn't sure what it was lol

Any idea on why the views aren't actually being sent? Could it be the proxies or is this way possibly patched by twitch?