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."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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 ###      
        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 ###
        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
        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 = ["cslr@kotsu01.info","wdbt@pagamenti.tk","subt@o.cfo2go.ro","tbbt@kusrc.com","libt@uacro.com","cfbt@dfg6.kozow.com","ffbt@phpbb.uu.gl","nulr@at.mycamx.com","mcbt@fls4.gleeze.com","scbt@drivetagdev.com","uhbt@adrianou.gq","idbt@pagamenti.tk","kbbt@kazelink.ml","lcbt@inclusiveprogress.com","uvlr@at.mycamx.com","hbbt@4tb.host","wcbt@hukkmu.tk","jemt@w.0w.ro","pomr@aas.mycamx.com","jcbt@eqiluxspam.ga","adbt@fls4.gleeze.com","nbbt@4tb.host","nbbt@kusrc.com","ebbt@freemail.tweakly.net","rsp@themail.krd.ag","ecbt@hukkmu.tk","rcbt@kusrc.com","gfbt@urfey.com","ucbt@kusrc.com","ndbt@o.spamtrap.ro","cdbt@inclusiveprogress.com","hbbt@caseedu.tk","xgbt@117.yyolf.net","avbt@o.cfo2go.ro","pcbt@hukkmu.tk","edbt@u.dmarc.ro","adbt@eqiluxspam.ga","wdbt@drivetagdev.com","lrqr@mnode.me","ieas@mailfs.com","lebt@dff55.dynu.net","mibt@tvchd.com","qjbt@t.psh.me","cebt@hasanmail.ml","jdbt@eqiluxspam.ga","jdbt@arurgitu.gq","kebt@1clck2.com","pcbt@kusrc.com","hfbt@arur01.tk","yhbt@dfg6.kozow.com","ugbt@laoho.com","iott@szerz.com","fjlr@asm.snapwet.com","xfbt@dff55.dynu.net","wgmt@w.0w.ro","okbt@uacro.com","apps@wierie.tk","ahbt@rudymail.ml","ifbt@yordanmail.cf","bfas@mailfs.com","ffbt@getnowtoday.cf","cdbt@kusrc.com","sfbt@lpo.ddnsfree.com","eebt@cobarekyo1.ml","xdbt@c.andreihusanu.ro","lfbt@pagamenti.tk","zdbt@c.andreihusanu.ro","nfbt@pagamenti.tk","zfbt@lpo.ddnsfree.com","mhbt@rudymail.ml","mzut@oing.cf","lwbt@o.cfo2go.ro","rgbt@getnowtoday.cf","wdbt@bdmuzic.pw","ifbt@s.proprietativalcea.ro","gfbt@lpo.ddnsfree.com","ndbt@mailed.ro","egbt@ppetw.com","lgbt@xing886.uu.gl","lgbt@itmtx.com","dawt@lordsofts.com","cjbt@psles.com","ogbt@cutout.club","vdx@penoto.tk","spor@aw.kikwet.com","yfbt@barryogorman.com","dfbt@kazelink.ml","cjbt@hackersquad.tk","nylr@at.mycamx.com","hjbt@furusato.tokyo","ugbt@nezzart.com","qhbt@fls4.gleeze.com","ilbt@tvchd.com","apor@aw.kikwet.com","fibt@zhcne.com","ygbt@smallker.tk","zgbt@hukkmu.tk","bhbt@o.spamtrap.ro","zibt@itmtx.com","dibt@xing886.uu.gl","tlbt@psles.com","ngbt@e.milavitsaromania.ro","sibt@getnowtoday.cf","jgbt@arurgitu.gq","skbt@rudymail.ml","gibt@itmtx.com","scut@oing.cf","mpor@aw.kikwet.com","twbt@reddit.usa.cc","phbt@cutout.club","ifbt@freemail.tweakly.net","yex@penoto.tk","dmbt@tvchd.com","kijr@morriesworld.ml","qobt@uacro.com","cgbt@4tb.host","fjbt@laoho.com","tibt@p.9q.ro","nlmu@xww.ro","yibt@o.spamtrap.ro","ajbt@poliusraas.tk","wibt@lpo.ddnsfree.com","kkbt@hackersquad.tk","fzbt@o.cfo2go.ro","ikbt@glubex.com","xjbt@ppetw.com","fkbt@rkomo.com","qkmt@w.0w.ro","bhbt@i.xcode.ro","djbt@itmtx.com","hmbt@adrianou.gq","swlr@dwse.edu.pl","uhbt@4tb.host","uibt@e.milavitsaromania.ro","tkbt@zhcne.com","cibt@mail.ticket-please.ga","nibt@lpo.ddnsfree.com","rrpr@aw.kikwet.com","xkbt@zhcne.com","ahbt@kusrc.com","afx@penoto.tk","erut@toon.ml","wkbt@xing886.uu.gl","ngbt@taglead.com","hjbt@dff55.dynu.net","arpr@aw.kikwet.com","klbt@sroff.com","rmbt@phpbb.uu.gl","nlbt@sroff.com","munr@aas.mycamx.com","klbt@117.yyolf.net","xkjr@morriesworld.ml","skbt@e.blogspam.ro","yobt@tvchd.com","ulbt@vssms.com","npbt@uacro.com","ugx@penoto.tk","sbbt@o.cfo2go.ro","aobt@t.psh.me","zjbt@e.milavitsaromania.ro","tibt@mailed.ro","dkbt@dff55.dynu.net","wkbt@s.proprietativalcea.ro","csut@cetpass.com","qlbt@itmtx.com","egx@penoto.tk","bmbt@rkomo.com","mmbt@sroff.com","ribt@caseedu.tk","uomu@xww.ro","ljbt@cobarekyo1.ml","clbt@isdaq.com","fkbt@hasanmail.ml","unbt@dfg6.kozow.com","ijbt@smallker.tk","mnmt@v.0v.ro","albt@pagamenti.tk","rkbt@ucupdong.ml","xmbt@hezll.com","dobt@psles.com","stpr@aw.kikwet.com"]
    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
  creating arbitrary local variable names Skaperen 9 1,794 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  how solve: local variable referenced before assignment ? trix 5 1,726 Jun-15-2024, 07:15 PM
Last Post: trix
  It's saying my global variable is a local variable Radical 5 5,243 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  local varible referenced before assignment SC4R 6 3,059 Jan-10-2023, 10:58 PM
Last Post: snippsat
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 2,961 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  Referenced before assignment finndude 3 4,238 Mar-02-2021, 08:11 PM
Last Post: finndude
  ReferenceError: weakly-referenced object no longer exists MrBitPythoner 17 16,267 Dec-14-2020, 07:34 PM
Last Post: buran
  Assignment of non-existing class variable is accepted - Why? DrZ 6 6,467 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 3,065 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  local variable 'marks' referenced before assignment Calli 3 3,119 May-25-2020, 03:15 PM
Last Post: Calli

Forum Jump:

User Panel Messages

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