Download app.py from Activegg/Token: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/spaces/Activegg/Token/resolve/main/app.py
- Command line
-
hf download hf://spaces/Activegg/Token/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Activegg/Token/resolve/main/app.py
3.67 kB
| # @title TOKEN GENERATER FOR NF,BN,FN | |
| import gradio as gr | |
| #import pyotp | |
| from api_helper import ShoonyaApiPy | |
| import threading | |
| import time | |
| import traceback | |
| from datetime import datetime, timedelta | |
| # Initialize API | |
| api = ShoonyaApiPy() | |
| # Credentials | |
| user = 'FA28777' | |
| pwd = 'Ebgc@D55' | |
| factor2 = '' #pyotp.TOTP('445KIV2355QM5266QJ557MA55P77V763').now() | |
| vc = 'FA28777_U' | |
| app_key = 'd23f2af8135baf5a67ec90cd2d8c6ebf' | |
| imei = 'abc1234' | |
| try: | |
| print("Attempting to log in...") | |
| ret = api.login(userid=user, password=pwd, twoFA=factor2, vendor_code=vc, api_secret=app_key, imei=imei) | |
| print(f"Login response: {ret}") | |
| except Exception as e: | |
| print(f"An error occurred during login: {str(e)}") | |
| traceback.print_exc() | |
| def get_next_expiry(index_name): | |
| today = datetime.now() | |
| weekday = today.weekday() | |
| if index_name == "NIFTY": | |
| expiry_day = 3 # Thursday | |
| elif index_name == "BANKNIFTY": | |
| expiry_day = 2 # Wednesday | |
| elif index_name == "FINNIFTY": | |
| expiry_day = 1 # Tuesday | |
| else: | |
| return None | |
| # Get the nearest expiry date | |
| days_until_expiry = (expiry_day - weekday) % 7 | |
| expiry_date = today + timedelta(days=days_until_expiry) | |
| # FINNIFTY uses a different format for expiry as per the example you provided | |
| if index_name == "FINNIFTY": | |
| return expiry_date.strftime('%d%b%y').upper() # Example: 10SEP24 | |
| else: | |
| return expiry_date.strftime('%d%b%y').upper() # Example: 05SEP24 | |
| def fetch_option_tokens(index_name, strike_price): | |
| try: | |
| expiry_date = get_next_expiry(index_name.upper()) | |
| if not expiry_date: | |
| return f"Invalid index name: {index_name}" | |
| # Define strike interval | |
| if index_name.upper() == "NIFTY" or index_name.upper() == "FINNIFTY": | |
| strike_interval = 50 | |
| elif index_name.upper() == "BANKNIFTY": | |
| strike_interval = 100 | |
| else: | |
| return "Invalid index. Supported: NIFTY, BANKNIFTY, FINNIFTY." | |
| # Round strike price to nearest interval | |
| nearest_strike = round(strike_price / strike_interval) * strike_interval | |
| # Construct the query with a space between the index name, expiry, and strike price | |
| ce_query = f"{index_name.upper()}{expiry_date}C{int(nearest_strike)}" | |
| pe_query = f"{index_name.upper()}{expiry_date}P{int(nearest_strike)}" | |
| print(f"CE query: {ce_query}") | |
| print(f"PE query: {pe_query}") | |
| ce_response = api.searchscrip('NFO', ce_query) | |
| pe_response = api.searchscrip('NFO', pe_query) | |
| ce_token = ce_response['values'][0]['token'] if ce_response and 'values' in ce_response else "Not Found" | |
| pe_token = pe_response['values'][0]['token'] if pe_response and 'values' in pe_response else "Not Found" | |
| print(f"CE token: {ce_token}") | |
| print(f"PE token: {pe_token}") | |
| return f"CE Token: {ce_token}\nPE Token: {pe_token}" | |
| except Exception as e: | |
| print(f"An error occurred in fetch_option_tokens: {str(e)}") | |
| traceback.print_exc() | |
| return "Error: No response from API." | |
| with gr.Blocks() as demo: | |
| index_name = gr.Textbox(label="Index Name", value="NIFTY") | |
| strike_price = gr.Number(label="Strike Price", value=25200) | |
| output = gr.Textbox(label="Tokens", interactive=False) | |
| status = gr.Textbox(label="Status", interactive=False) | |
| def update_status(index_name, strike_price): | |
| status_text = fetch_option_tokens(index_name, strike_price) | |
| return status_text | |
| button = gr.Button("Get Tokens") | |
| button.click(update_status, inputs=[index_name, strike_price], outputs=[output]) | |
| demo.launch(share=True) |