tradingview-scraper
tradingview-scraper copied to clipboard
auth_token
def get_auth_token():
sign_in_url = 'https://www.tradingview.com/accounts/signin/'
username = 'username'
password = 'password'
data = {"username": username, "password": password, "remember": "on"}
headers = {
'Referer': 'https://www.tradingview.com'
}
response = requests.post(url=sign_in_url, data=data, headers=headers)
auth_token = response.json()['user']['auth_token']
return auth_token
Then send a message through the tunnel sendMessage(ws, "set_auth_token", ["auth_token"])
ws = create_connection( 'wss://data.tradingview.com/socket.io/websocket?from=chart/XXyour_chartXX/&date=XXXX_XX_XX-XX_XX',headers=headers)
result = ws.recv()
if re.search('~m~\\d+~m~~h~\\d+', result):
#Send ping message
ws.send(result)
Thanks @euvgub !! :)
@euvgub @0xrushi Hi, I tried using this function and it keeps on giving me either File c:\users\asus\trading\data fetcher.py:38 in get_auth_token auth_token = response.json()['user']['auth_token']
KeyError: 'user'
or ~m~45~m~{"m":"protocol_error","p":["bad auth token"]}
I don't know why this is happening though. Could you please help me out.
P.S.: Turns out there is a reCAPTCHA verification there which is failing. Please let me know how we can bypass the verification.
Thanks
Hi, I also have same debug error as above,
auth_token = response.json()['user']['auth_token'] ~~~~~~~~~~~~~~~^^^^^^^^ KeyError: 'user'
i'm not using CAPTCHA when i logging on,
It would be very helpful someone who rescue me.... Thanks
@daltas Hey, there is a discussion about the same in the create study issue. I was able to resolve it thanks to @slukin1. You can check the same solution out. :)
No recapcha if use Chrome DevTools Protocol for receiving auth_token I`m in progress how to get cookie value sessionid or auth_token from received page or headers after log in
import json
import time
import subprocess
import requests
import websocket
from websocket import create_connection
websocket.enableTrace(True)
def start_browser(browser_path, debugging_port):
options = [#'--headless',
f'--remote-debugging-port={debugging_port}',
#'--remote-allow-origins=http://127.0.0.1:{debugging_port}']
'--remote-allow-origins=*']
browser_proc = subprocess.Popen([browser_path] + options)
wait_seconds = 10.0
sleep_step = 0.25
while wait_seconds > 0:
try:
url = f'http://127.0.0.1:{debugging_port}/json'
time.sleep(5)
resp = requests.get(url).json()
ws_url = resp[0]['webSocketDebuggerUrl']
return browser_proc, create_connection(ws_url)
except requests.exceptions.ConnectionError:
time.sleep(sleep_step)
wait_seconds -= sleep_step
raise Exception('Unable to connect to chrome')
request_id = 0
def run_command(conn, method, **kwargs):
global request_id
request_id += 1
command = {'method': method,
'id': request_id,
'params': kwargs}
conn.send(json.dumps(command))
while True:
msg = json.loads(conn.recv())
if msg.get('id') == request_id:
return msg
tv_url = 'https://tradingview.com'
#browser_path = '/usr/bin/google-chrome'
#browser_path = '/usr/bin/google-chrome-beta'
browser_path = '/usr/bin/google-chrome-unstable'
#browser_path = '/usr/bin/microsoft-edge'
#browser_path = '/snap/bin/chromium'
#browser_path = '/usr/bin/firefox'
browser, conn = start_browser(browser_path, 9222)
run_command(conn, 'Page.navigate', url=tv_url)
time.sleep(5) # let it load
js = """
var step1 = document.evaluate('//button[@aria-label="Open user menu"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
step1.click();
var step2 = document.evaluate('//button[@data-name="header-user-menu-sign-in"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
step2.click();
setTimeout(function(){
document.getElementById("id_username").focus();
document.getElementById("id_username").value = "your_login";
document.getElementById("id_password").focus();
document.getElementById("id_password").value = "your_password";
var signin = document.evaluate('//button[contains(@class,"submitButton")]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
signin.focus();
signin.click();
},500);
"""
result = run_command(conn, 'Runtime.evaluate', expression=js)
print(result)
browser.terminate()
@daltas Hey, there is a discussion about the same in the create study issue. I was able to resolve it thanks to @slukin1. You can check the same solution out. :)
Hi, Congraci the success frend!! I'm still in the deep err sea :)
@slukin1 Thanks a lot! i got a handshake 400 error, can you guess why?
status, resp = _get_resp_headers(sock)
raise WebSocketBadStatusException("Handshake status %d %s", status, status_message, resp_headers) websocket._exceptions.WebSocketBadStatusException: Handshake status 400 Bad Request
import websocket
websocket.enableTrace(True)
headers = json.dumps({'Origin': 'https://data.tradingview.com'})
ws = websocket.create_connection('wss://data.tradingview.com/socket.io/websocket?&type=chart',headers=headers)
#result = ws.recv()
#print(result)
while True:
ws.recv()
hi @slukin1 Great Thanx! maybe there is a problem in an unexpected place.... ;)
websocket.enableTrace(True) and You`ll receive more information about the reason for the failure raise WebSocketBadStatusException("Handshake status {status} {message} -+-+- {headers} -+-+- {body}".format(status=status, message=status_message, headers=resp_headers, body=response_body), status, status_message, resp_headers, response_body)