Python Forum
local variable referenced before assignment.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
local variable referenced before assignment.
#1
I have been trying to make m3u files from ustvnow, but every time i test the code with my creds
it keeps saying "local variable 'csrf' referenced before assignment."

import sys
import requests
import cookiejar
import re
import json
import os
import sys
import random
debug = False

# 22/03/18 - updated file to work with slightly altered api of ustvnow. - MikeC
##################################################################################################################
# CONFIG
##################################################################################################################

try:
    username = sys.argv[1]
except:
    username = ""                       # Set username manually here if not using the command line

try:
    password = sys.argv[2]
except:
    password = ""                       # Set username manually here if not using the command line

try:
    channel = sys.argv[3]
except:
    channel = "ALL"                     # Set channel manually here if not using the command line (use ALL to show all)

try:
    CreateSTRM = sys.argv[4]
except:
    CreateSTRM = False                  # Set if you would like to create the STRM file

try:
    hideTitle = sys.argv[5]             
except:
    hideTitle = True                    # For those of you who would like to hide the showing now show title
    
##################################################################################################################
## FUNCTIONS
##################################################################################################################

###########
# Decoder #
###########
def robust_decode(bs):
    '''Takes a byte string as param and convert it into a unicode one. First tries UTF8, and fallback to Latin1 if it fails'''
    bs.decode(errors='replace')
    cr = None
    try:
        cr = bs.decode('utf8')
    except UnicodeDecodeError:
        cr = bs.decode('latin1')
    return cr

###########
# Scraper #
###########
def Ustvnow(username, password, exclude=[]):
    result = ""
    with requests.Session() as s:
        ### Get CSRF Token ###       
        url="https://watch.ustvnow.com/account/signin"
        r = s.get(url)
        html = r.text
        html = ' '.join(html.split())
        ultimate_regexp = "(?i)<\/?\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>"
        for match in re.finditer(ultimate_regexp, html):
            i = repr(match.group())
            if '<input type="hidden" name="csrf_ustvnow" value="' in i:
                csrf = i.replace('<input type="hidden" name="csrf_ustvnow" value="','').replace('">','')
                csrf = str(csrf).replace("u'","").replace("'","")
        if debug:
            print(csrf)
        ### Get Token ###
        url = "https://watch.ustvnow.com/account/login"
        payload = {'csrf_ustvnow': csrf, 'signin_email': username, 'signin_password':password, 'signin_remember':'1'}
        r = s.post(url, data=payload)
        html = r.text
        html = ' '.join(html.split())
        html = html[html.find('var token = "')+len('var token = "'):]
        html = html[:html.find(';')-1]
        token = str(html)
        if debug:
            print(token)
        ### Get Stream ###
        device = "gtv"        
        url = "http://m-api.ustvnow.com/"+device+"/1/live/login"
        payload = {'username': username, 'password': password, 'device':device}
        r = s.post(url, data=payload)
        html = r.text
        j = json.loads(html)
        if debug:
            print(j)
        if j['result'] != "success":
            if debug:
                print("error")
            return False
        url = "http://m-api.ustvnow.com/gtv/1/live/playingnow?token="+token
        if debug:
            print(url)
        r = s.get(url)
        html = r.text
        j = json.loads(html)
        if debug:
            print(j)        
        n = 0
        if channel=="ALL":
            result += "#EXTM3U\n"
        while True:
            try:
                scode = j['results'][n]['scode']
            except:
                break
            stream_code = j['results'][n]['stream_code']
            title = j['results'][n]['title']
            title = u' '.join((title,"")).encode('utf-8').strip()
            title = str(title)
            url = "http://m.ustvnow.com/stream/1/live/view?scode="+scode+"&token="+token+"&br_n=Firefox&pr=ec&tr=expired&pl=vjs&pd=1&br_n=Firefox&br_v=54&br_d=desktop"
            if debug:
                print(url)
            r = s.get(url)
            html = r.text
            try:
                i = json.loads(html)
                #URL = i["stream"].replace("\n","")[:-1]
                URL = i["stream"]
                #if "&jwt=" in URL:
                #    URL = URL.split("&jwt=")[0]
            except:
                break
            # Create STRM file
            if(CreateSTRM):
                fname = stream_code+".strm"
                if os.path.isfile(fname):
                    f= open(fname,"wb")
                    f.write(URL)
                    f.close
                else:
                    f= open(fname,"wb+")
                    f.write(URL)
                    f.close
            # Print Link
            if channel=="ALL":
                if robust_decode(stream_code) not in exclude:
                    if not hideTitle:
                        result += "#EXTINF:-1,"+robust_decode(stream_code)+" - "+robust_decode(title)+"\n"
                    else:
                        result += "#EXTINF:-1,"+robust_decode(stream_code)+"\n"                        
                    result += URL+"\n"
            else:
                if debug:
                    print(stream_code)
                if channel==stream_code:
                    result += URL+"\n"
                    break
            n += 1
        return(result.strip("\n"))

##################################################################################################################
## EXCLUDE THESE CHANNELS
##
## exclude = ["ABC", "My9" "FOX"] these channels would be excluded from the result
##
##################################################################################################################

exclude = []

##################################################################################################################
## MAIN
##################################################################################################################
if(username==""):
    #no username given, try predefined username and password
    usernames = ["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"]
    random.shuffle(usernames)
    for username in usernames:
        try:
            password = username
            result = Ustvnow(username, password, exclude)
            if result != False:
                break #success
        except:
            next #try again
else:
    result = Ustvnow(username, password, exclude)

print(result)

##################################################################################################################
Reply
#2
I only quote:

Quote:Errors should never pass silently.
Unless explicitly silenced.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
this is the full traceback error:

Error:
Traceback (most recent call last): File "main.py", line 187, in <module> result = Ustvnow(username, password, exclude) File "main.py", line 79, in Ustvnow payload = {'csrf_ustvnow': csrf, 'signin_email': username, 'signin_password':password, 'signin_remember':'1'} UnboundLocalError: local variable 'csrf' referenced before assignment
keep in mind i entered my personal username and password at the top of the file
Reply
#4
obviously there is no match, so the loop body (lines 71-74) is never executed. If you run in debug mode the error will be when try to print csrf

You shouldn't be using regex to parse html. Look at BeautifulSoup. Here is our tutorial https://python-forum.io/Thread-Web-Scraping-part-1
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,166 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  local varible referenced before assignment SC4R 6 1,523 Jan-10-2023, 10:58 PM
Last Post: snippsat
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,914 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  Referenced before assignment finndude 3 3,270 Mar-02-2021, 08:11 PM
Last Post: finndude
  ReferenceError: weakly-referenced object no longer exists MrBitPythoner 17 11,540 Dec-14-2020, 07:34 PM
Last Post: buran
  Assignment of non-existing class variable is accepted - Why? DrZ 6 4,269 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 2,273 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  local variable 'marks' referenced before assignment Calli 3 2,327 May-25-2020, 03:15 PM
Last Post: Calli
  Variable assignment wierdness with Memory eoins 1 2,041 Mar-08-2020, 10:15 AM
Last Post: scidam
  UnboundLocalError: local variable referenced before assignment svr 1 3,306 Dec-27-2019, 09:08 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020