Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I scrape data 24/7? I found such a solution, but it did not seem reliable. #24

Open
MuhammedEminK opened this issue Jan 14, 2024 · 0 comments

Comments

@MuhammedEminK
Copy link

MuhammedEminK commented Jan 14, 2024

import websocket
import time
import threading
import json

SOCKET = "wss://data.tradingview.com/socket.io/websocket"

headers = {
    "Accept-Encoding": "gzip, deflate, br",
    # "Accept-Language": "en-US,en;q=0.9",
    # "Cache-Control": "no-cache",
    # "Connection": "Upgrade",
    "Host": "data.tradingview.com",
    "Origin": "https://www.tradingview.com",
    # "Pragma": "no-cache",
    # "Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
    # "Sec-WebSocket-Key": "Qf9IDRKqcgNBrNs7X4FK9w==",
    # "Sec-WebSocket-Version": 13,
    # "Upgrade": "websocket",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
}

"""
~m~55~m~{"m":"chart_create_session","p":["cs_zEcm9GqyQdK0",""]}
'~m~52~m~{"m":"quote_create_session","p":["qs_dl0OygXkO4uu"]}'
~m~98~m~{"m":"quote_add_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}
~m~68~m~{"m":"quote_fast_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT"]}
~m~6~m~~h~157
~m~411~m~{"m":"quote_fast_symbols","p":["qs_x72fChUYomPp","MCX:GOLDGUINEAN2020","NSE:NIFTY","MCX:GOLDPETALN2020","NSE:BAJFINANCE","MCX:SILVERMQ2020","NSE:SBIN","NSE:BANKNIFTY","NSE:RBLBANK","NSE:INDIAVIX","NSE:INDUSINDBK","NSE:CIPLA","NSE:SUNTV","MCX:NATURALGAS1!","MCX:SILVERMIC1!","MCX:CRUDEOIL1!","MCX:GOLDM1!","NSE:BANKNIFTY1!","SGX:IN1!","OANDA:USDINR","NSE:DABUR","NSE:BERGEPAINT","NASDAQ:TSLA","BINANCE:BTCUSDT"]}
~m~68~m~{"m":"quote_fast_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT"]}
~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}

"""



def on_open(ws):
    print('opened connection')
    # def run(*args):
    #     for i in range(30):
    #         time.sleep(1)
    #         ws.send("Hello %d" % i)
    #     time.sleep(1)
    #     ws.close()
    #     print("thread terminating...")
    # threading.start_new_thread(run, ())
    time.sleep(2)
    # ws.send('~m~524~m~{"m":"set_auth_token","p":[""]}')
    ws.send('~m~54~m~{"m":"set_auth_token","p":["unauthorized_user_token"]}')
    ws.send('~m~55~m~{"m":"chart_create_session","p":["cs_zEcm9GqyQdK0",""]}')
    ws.send('~m~52~m~{"m":"quote_create_session","p":["qs_x72fChUYomPp"]}')
    ws.send('~m~344~m~{"m":"quote_set_fields","p":["qs_x72fChUYomPp","ch","chp","current_session","description","local_description","language","exchange","fractional","is_tradable","lp","lp_time","minmov","minmove2","original_name","pricescale","pro_name","short_name","type","update_mode","volume","currency_code","logoid","currency-logoid","base-currency-logoid"]}')

    ws.send('~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}')
    ws.send('~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BNBUSDT",{"flags":["force_permission"]}]}')
    ws.send('~m~91~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","NSE:SBIN",{"flags":["force_permission"]}]}')
    ws.send('~m~98~m~{"m":"quote_fast_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT","BINANCE:BNBUSDT", "NSE:SBIN"]}')


def on_close(ws):
    print('closed connection')


def on_message(ws, message):
    # p = message.split('~', -1)[4]
    # data = json.loads(p)
    # print(data)
    # print(f'received message :: {message}')
    if 'lp' in message:
        p = message.split('~', -1)[4]
        data = json.loads(p)
        # print(data)
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        symbol = data['p'][1]['n']
        ltp = data['p'][1]['v']['lp']
        volume = data['p'][1]['v']['volume']
        if symbol.upper() == "BINANCE:BTCUSDT":
            print(f'tick :: {timestamp} :: {symbol} :: {ltp} :: {volume}')



def create_websocket_connection():
    websocket.enableTrace(False)
    ws = websocket.WebSocketApp(
        SOCKET, on_message=on_message, on_open=on_open, on_close=on_close)
    wst = threading.Thread(target=ws.run_forever)
    wst.daemon = True
    wst.start()

    conn_timeout = 60
    while not ws.sock.connected and conn_timeout:
        time.sleep(1)
        conn_timeout -= 1

    while ws.sock is not None:
        time.sleep(10)
    print("buraya geldi")

if __name__ == "__main__":
    while True:
        create_websocket_connection()
        print("buraya geldi2")
        
        
        
        
        
        
        
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant