7
7
import webbrowser
8
8
import csv
9
9
import requests
10
+ import sys
11
+ import streamtologger
10
12
11
13
12
14
# noinspection PyAttributeOutsideInit
13
15
class Nse :
14
- def __init__ (self , window ):
16
+ def __init__ (self , window : Tk ):
15
17
self .seconds = 60
18
+ self .stdout = sys .stdout
19
+ self .stderr = sys .stderr
16
20
self .previous_date = None
17
21
self .previous_time = None
18
22
self .first_run = True
19
23
self .stop = False
24
+ self .logging = False
20
25
self .dates = ["" ]
21
26
self .indices = ["NIFTY" , "BANKNIFTY" , "NIFTYIT" ]
22
27
self .headers = {'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
23
28
'like Gecko) '
24
29
'Chrome/80.0.3987.149 Safari/537.36' ,
25
30
'accept-language' : 'en,gu;q=0.9,hi;q=0.8' , 'accept-encoding' : 'gzip, deflate, br' }
31
+ self .url_oc = "https://www.nseindia.com/option-chain"
26
32
self .session = requests .Session ()
27
33
self .cookies = {}
28
34
self .login_win (window )
@@ -33,14 +39,13 @@ def get_data(self, event=None):
33
39
if self .first_run :
34
40
self .index = self .index_var .get ()
35
41
try :
36
- url_oc = "https://www.nseindia.com/option-chain"
37
42
url = f"https://www.nseindia.com/api/option-chain-indices?symbol={ self .index } "
38
- request = self .session .get (url_oc , headers = self .headers , timeout = 5 )
43
+ request = self .session .get (self . url_oc , headers = self .headers , timeout = 5 )
39
44
self .cookies = dict (request .cookies )
40
45
response = self .session .get (url , headers = self .headers , timeout = 5 , cookies = self .cookies )
41
46
except Exception as err :
42
- print (request . status_code )
43
- print (response . status_code )
47
+ print (request )
48
+ print (response )
44
49
print (err , "1" )
45
50
messagebox .showerror (title = "Error" , message = "Error in fetching dates.\n Please retry." )
46
51
self .dates .clear ()
@@ -52,7 +57,16 @@ def get_data(self, event=None):
52
57
try :
53
58
url = f"https://www.nseindia.com/api/option-chain-indices?symbol={ self .index } "
54
59
response = self .session .get (url , headers = self .headers , timeout = 5 , cookies = self .cookies )
60
+ if response .status_code == 401 :
61
+ self .session .close ()
62
+ self .session = requests .Session ()
63
+ url = f"https://www.nseindia.com/api/option-chain-indices?symbol={ self .index } "
64
+ request = self .session .get (self .url_oc , headers = self .headers , timeout = 5 )
65
+ self .cookies = dict (request .cookies )
66
+ response = self .session .get (url , headers = self .headers , timeout = 5 , cookies = self .cookies )
67
+ print ("reset cookies" )
55
68
except Exception as err :
69
+ print (request )
56
70
print (response )
57
71
print (err , "2" )
58
72
return
@@ -92,7 +106,7 @@ def get_data(self, event=None):
92
106
93
107
return response , json_data
94
108
95
- def login_win (self , window ):
109
+ def login_win (self , window : Tk ):
96
110
self .login = window
97
111
self .login .title ("NSE" )
98
112
window_width = self .login .winfo_reqwidth ()
@@ -127,7 +141,7 @@ def login_win(self, window):
127
141
self .sp_entry .focus_set ()
128
142
self .get_data ()
129
143
130
- def focus_widget (event , mode ):
144
+ def focus_widget (event , mode : int ):
131
145
if mode == 1 :
132
146
self .get_data ()
133
147
self .date_menu .focus_set ()
@@ -182,7 +196,21 @@ def export(self, event=None):
182
196
messagebox .showerror (title = "Export Failed" ,
183
197
message = "An error occurred while exporting the data." )
184
198
185
- def links (self , link , event = None ):
199
+ def log (self , event = None ):
200
+ if not self .logging :
201
+ streamtologger .redirect (target = "nse.log" , header_format = "[{timestamp:%Y-%m-%d %H:%M:%S} - {level:5}] " )
202
+ self .logging = True
203
+ self .options .entryconfig (self .options .index (2 ), label = "Logging: On (Ctrl+L)" )
204
+ messagebox .showinfo (title = "Started" , message = "Debug Logging has been enabled." )
205
+ elif self .logging :
206
+ sys .stdout = self .stdout
207
+ sys .stderr = self .stderr
208
+ streamtologger ._is_redirected = False
209
+ self .logging = False
210
+ self .options .entryconfig (self .options .index (2 ), label = "Logging: Off (Ctrl+L)" )
211
+ messagebox .showinfo (title = "Stopped" , message = "Debug Logging has been disabled." )
212
+
213
+ def links (self , link : str , event = None ):
186
214
187
215
if link == "developer" :
188
216
webbrowser .open_new ("https://github.com/VarunS2002/" )
@@ -197,7 +225,7 @@ def links(self, link, event=None):
197
225
198
226
self .info .attributes ('-topmost' , False )
199
227
200
- def about_window (self ):
228
+ def about_window (self ) -> Toplevel :
201
229
self .info = Toplevel ()
202
230
self .info .title ("About" )
203
231
window_width = self .info .winfo_reqwidth ()
@@ -225,7 +253,7 @@ def about(self, event=None):
225
253
heading .grid (row = 0 , column = 0 , columnspan = 2 , sticky = N + S + W + E )
226
254
version_label = Label (self .info , text = "Version:" , relief = RIDGE )
227
255
version_label .grid (row = 1 , column = 0 , sticky = N + S + W + E )
228
- version_val = Label (self .info , text = "3.3 " , relief = RIDGE )
256
+ version_val = Label (self .info , text = "3.4 " , relief = RIDGE )
229
257
version_val .grid (row = 1 , column = 1 , sticky = N + S + W + E )
230
258
dev_label = Label (self .info , text = "Developer:" , relief = RIDGE )
231
259
dev_label .grid (row = 2 , column = 0 , sticky = N + S + W + E )
@@ -253,7 +281,7 @@ def close(self, event=None):
253
281
if ask_quit :
254
282
self .session .close ()
255
283
self .root .destroy ()
256
- quit ()
284
+ sys . exit ()
257
285
elif not ask_quit :
258
286
pass
259
287
@@ -274,13 +302,15 @@ def main_win(self):
274
302
self .options = Menu (menubar , tearoff = 0 )
275
303
self .options .add_command (label = "Stop (Ctrl+X)" , command = self .change_state )
276
304
self .options .add_command (label = "Export to CSV (Ctrl+S)" , command = self .export )
305
+ self .options .add_command (label = "Logging: Off (Ctrl+L)" , command = self .log )
277
306
self .options .add_separator ()
278
307
self .options .add_command (label = "About (Ctrl+M)" , command = self .about )
279
308
self .options .add_command (label = "Quit (Ctrl+Q)" , command = self .close )
280
309
menubar .add_cascade (label = "Menu" , menu = self .options )
281
310
self .root .config (menu = menubar )
282
311
283
312
self .root .bind ('<Control-s>' , self .export )
313
+ self .root .bind ('<Control-l>' , self .log )
284
314
self .root .bind ('<Control-x>' , self .change_state )
285
315
self .root .bind ('<Control-m>' , self .about )
286
316
self .root .bind ('<Control-q>' , self .close )
@@ -435,7 +465,7 @@ def set_values(self):
435
465
else :
436
466
self .pcr_val .config (text = self .put_call_ratio , bg = red )
437
467
438
- def set_itm_labels (call_change , put_change ) :
468
+ def set_itm_labels (call_change : float , put_change : float ) -> str :
439
469
label = "No"
440
470
if put_change > call_change :
441
471
if put_change >= 0 :
0 commit comments