@@ -118,7 +118,6 @@ def login(self, force=False):
118118 return False
119119
120120 def two_factor (self ):
121- print ("Two factor required" )
122121 # verification_method': 0 works for sms and TOTP. why? ¯\_ಠ_ಠ_/¯
123122 verification_code = input ('Enter verification code: ' )
124123 data = {
@@ -131,8 +130,7 @@ def two_factor(self):
131130 'device_id' : self .device_id ,
132131 'guid' : self .uuid ,
133132 }
134- if self .send_request ('accounts/two_factor_login/' , self .generate_signature (json .dumps (data )),
135- login = True ):
133+ if self .send_request ('accounts/two_factor_login/' , self .generate_signature (json .dumps (data )), login = True ):
136134 return True
137135 else :
138136 return False
@@ -153,27 +151,29 @@ def send_request(self, endpoint, post=None, login=False):
153151 while True :
154152 try :
155153 if post is not None :
156- response = self .s .post (self .API_URL + endpoint , data = post , verify = verify )
154+ self . LastResponse = self .s .post (self .API_URL + endpoint , data = post , verify = verify )
157155 else :
158- response = self .s .get (self .API_URL + endpoint , verify = verify )
156+ self .LastResponse = self .s .get (self .API_URL + endpoint , verify = verify )
157+
158+ self .LastJson = json .loads (self .LastResponse .text )
159+
159160 break
160161 except Exception as e :
161- print ('Except on SendRequest (wait 60 sec and resend): {}' .format (str (e )))
162+ print ('* Except on SendRequest (wait 60 sec and resend): {}' .format (str (e )))
162163 time .sleep (60 )
163164
164- if response .status_code in [200 , 400 ]:
165- self .LastResponse = response
166- self .LastJson = json .loads (response .text )
165+ if self .LastResponse .status_code == 200 :
167166 return True
167+ elif 'two_factor_required' in self .LastJson and self .LastResponse .status_code == 400 :
168+ # even the status code isn't 200 return True if the 2FA is required
169+ if self .LastJson ['two_factor_required' ]:
170+ print ("Two factor required" )
171+ return True
168172 else :
169- print ("Request return " + str (response .status_code ) + " error!" )
170- # for debugging
171- try :
172- self .LastResponse = response
173- self .LastJson = json .loads (response .text )
174- print (self .LastJson )
175- except :
176- pass
173+ error_message = " - "
174+ if "message" in self .LastJson :
175+ error_message = self .LastJson ['message' ]
176+ print ('* ERROR({}): {}' .format (self .LastResponse .status_code , error_message ))
177177 return False
178178
179179 def set_proxy (self , proxy = None ):
@@ -193,16 +193,18 @@ def generate_signature(self, data, skip_quote=False):
193193 self .IG_SIG_KEY .encode ('utf-8' ), data .encode ('utf-8' ), hashlib .sha256 ).hexdigest () + '.' + parsed_data
194194
195195 def start (self ):
196- print ('logging in...' )
196+ print ("Let's do it!" )
197197 if self .login ():
198- print (' logged in!!' )
199- print ( 'Generating stream upload_url and keys... \n ' )
198+ print ("You'r logged in" )
199+
200200 if self .create_broadcast ():
201- print ("Broadcast ID: {}" .format (self .broadcast_id ))
202- print ("Server URL: {}" .format (self .stream_server ))
203- print ("Server Key: {}" .format (self .stream_key ))
201+ print ("Broadcast ID: {}" )
202+ print ("* Broadcast ID: {}" .format (self .broadcast_id ))
203+ print ("* Server URL: {}" .format (self .stream_server ))
204+ print ("* Server Key: {}" .format (self .stream_key ))
205+ print ("The stream key was automatically copied to your clipboard" )
204206
205- input ( ' Press Enter after your setting your streaming software.' )
207+ print ( " Press Enter after your setting your streaming software." )
206208 if self .start_broadcast ():
207209 self .is_running = True
208210
@@ -396,4 +398,4 @@ def stop(self):
396398 def get_comments (self ):
397399 if self .send_request ("live/{}/get_comment/" .format (self .broadcast_id )):
398400 for comment in self .LastJson ['comments' ]:
399- print (f"{ comment ['user' ]['username' ]} has posted a new comment: { comment ['text' ]} " )
401+ print (f"{ comment ['user' ]['username' ]} has posted a new comment: { comment ['text' ]} " )
0 commit comments