Skip to content

Instantly share code, notes, and snippets.

@joaoescribano
Created May 23, 2019 22:24
Show Gist options
  • Save joaoescribano/9fe5484c9e63bf1c8644d45360a866e7 to your computer and use it in GitHub Desktop.
Save joaoescribano/9fe5484c9e63bf1c8644d45360a866e7 to your computer and use it in GitHub Desktop.
Kucoin Login and data gethering with python + selenium + OTP
#!/usr/bin/env python3
# usage: python3 kucoin_collector.py <username> <password> <otp_secret>
import datetime
import math
import os
import sys
import tempfile
import pyotp
import time
import json
# third-party imports
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from time import sleep
def get_chrome_drive(driver_path=None):
base_dir = os.path.dirname( os.path.abspath(__file__) )
log_path = os.path.join( base_dir, 'chromedriver.log' )
if driver_path is None:
driver_path = '/usr/bin/chromedriver'
pass
options = webdriver.ChromeOptions()
# options.headless = True
# options.add_argument('--hide-scrollbars')
options.add_argument('--no-sandbox')
options.add_extension(os.getcwd() + '/../plugin_editado.zip')
driver = webdriver.Chrome(
executable_path=driver_path,
chrome_options=options,
service_args=[
# '--log-path={}'.format(log_path),
# '--verbose',
]
)
return driver
def get_firefox_drive(driver_path=None):
base_dir = os.path.dirname( os.path.abspath(__file__) )
log_path = os.path.join( base_dir, 'geckodriver.log' )
if driver_path is None:
driver_path = '/usr/bin/geckodriver'
pass
options = webdriver.FirefoxOptions()
# options.add_argument('-headless')
driver = webdriver.Firefox(
executable_path=driver_path,
firefox_options=options
)
return driver
def doLogin(driver, username, password, otc_pass):
driver.get("https://www.kucoin.com/ucenter/signin")
try:
WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div[2]/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/span/div/input')))
print("Page is ready!")
except TimeoutException:
print("Loading took too much time!")
doLogin(driver, username, password, otc_pass)
return
# login
elem = driver.find_element_by_css_selector("#account input")
elem.clear()
elem.send_keys(username)
# password
elem = driver.find_element_by_css_selector("#password input")
elem.clear()
elem.send_keys(password)
# send
driver.execute_script('document.querySelector(".ant-btn").click();')
try:
element = WebDriverWait(driver, 100).until(
EC.visibility_of_element_located((By.ID, "g2af"))
)
except TimeoutException:
print("Captcha seems not to worked, reloading")
doLogin(driver, username, password, otc_pass)
return
totp = pyotp.TOTP(otc_pass)
elem = driver.find_element_by_id("g2af")
elem.clear()
elem.send_keys(totp.now())
elem = driver.find_element_by_xpath('//*[@id="__next"]/div/div[2]/div/div[2]/div[2]/div/form/div[2]/div[5]/div/div/span/button')
elem.click()
try:
WebDriverWait(driver, 3).until(EC.url_changes("https://www.kucoin.com/account"))
except TimeoutException:
print("Logout failed, trying again")
doLogin()
return
print("Login successfully")
return
def getCurrencies(driver):
driver.get("https://www.kucoin.com/assets/coin/BTC")
time.sleep(5)
currencies = driver.execute_script('var tmp = {}; document.querySelector(".ant-select-selection__rendered").click(); var elem = document.querySelectorAll("span[class*=name___]"); for (var x=0; x< elem.length; x++) { tmp[elem[x].innerHTML] = {"depositStatus": false}; } return tmp;')
for currency in currencies:
print("Retriving ", currency, " deposit address/status")
driver.get("https://www.kucoin.com/assets/coin/" + currency)
try:
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//*[@id="__next"]/div/div[2]/div/div[2]/div/div/div[1]/div/div/div[1]')))
print("Page is ready!")
try:
element = WebDriverWait(driver, 3).until(
EC.visibility_of_element_located((By.XPATH, '//*[@id="__next"]/div/div[2]/div/div[2]/div/div/div[1]/div/div/div[2]/div[1]/div[2]/div'))
)
print("Account address found, coin avaible [1]")
currencies[currency]['depositStatus'] = True
except TimeoutException:
try:
print("Trying to find coin approval...")
time.sleep(5)
has = driver.execute_script('return (document.evaluate(\'//*[@id="__next"]/div/div[2]/div/div[2]/div/div/div[1]/div/div/div[2]/div[2]/label/span[1]/input\', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue ? true : false);')
if (has):
print("Coin need approval")
message = driver.execute_script('return (document.evaluate(\'//*[@id="__next"]/div/div[2]/div/div[2]/div/div/div[1]/div/div/div[2]/div[1]/p\', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue ? document.evaluate(\'//*[@id="__next"]/div/div[2]/div/div[2]/div/div/div[1]/div/div/div[2]/div[1]/p\', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML.toLowerCase() : false);')
if (message.find("do not") == -1 and message.find("don't") == -1 and message.find("does not") == -1 and message.find("doesn't") == -1):
elem = driver.find_element_by_xpath('//*[@id="__next"]/div/div[2]/div/div[2]/div/div/div[1]/div/div/div[2]/div[2]/label/span[1]/input')
elem.click()
time.sleep(2)
elem2 = driver.find_element_by_xpath('//*[@id="__next"]/div/div[2]/div/div[2]/div/div/div[1]/div/div/div[2]/div[3]/button')
elem2.click()
try:
element = WebDriverWait(driver, 3).until(
EC.visibility_of_element_located((By.XPATH, '//*[@id="__next"]/div/div[2]/div/div[2]/div/div/div[1]/div/div/div[2]/div[1]/div[2]/div'))
)
print("Account address found, coin avaible [2]")
currencies[currency]['depositStatus'] = True
except TimeoutException:
print("Account address not found, coin not avaible [1]")
continue
continue
except TimeoutException:
print("Account address not found, coin not avaible [2]")
continue
print("Account address not found, coin not avaible [3]")
continue
continue
except TimeoutException:
print("Loading took too much time!")
continue
return currencies
def main():
username = sys.argv[1]
password = sys.argv[2]
otc_pass = sys.argv[3]
driver = get_chrome_drive()
driver.set_window_size(1280,768)
doLogin(driver, username, password, otc_pass)
currencies = getCurrencies(driver)
f = open("data.json", "w+")
f.truncate(0)
f.write(json.dumps(currencies))
f.close()
print("File updated!")
return
if __name__ == '__main__':
main()
@joaoescribano
Copy link
Author

The plugin file i used is the anti-captcha (edited to fix the api key/secrets).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment