Skip to content

Instantly share code, notes, and snippets.

@axju
Last active August 18, 2021 10:15
Show Gist options
  • Save axju/5beaef32c0c6817cdb760aa72263b827 to your computer and use it in GitHub Desktop.
Save axju/5beaef32c0c6817cdb760aa72263b827 to your computer and use it in GitHub Desktop.
This is the source to my video: https://youtu.be/QSvZnbEiXBc
import logging
from time import sleep
from datetime import datetime, timedelta
from argparse import ArgumentParser
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from chromedriver_autoinstaller import install as chrome_install
logger = logging.getLogger(__name__)
def get_browser():
chrome_install()
options = Options()
options.add_argument('user-data-dir=selenuim-data')
browser = webdriver.Chrome(options=options)
return browser
def runalyze_login(browser):
browser.get('https://runalyze.com/dashboard')
while True:
if browser.current_url.find('dashboard') > 0:
logger.info('login to runalyze')
return
sleep(2)
def runalyze_search(browser):
browser.get('https://runalyze.com/my/search')
sleep(2)
element = browser.find_element_by_xpath('//*[@id="date-from"]')
element.clear()
element.send_keys((datetime.now() - timedelta(days=10)).strftime('%d.%m.%Y'))
browser.find_element_by_xpath('//*[@id="search_legend_0"]/div[50]/input').click()
sleep(2)
table = browser.find_element_by_xpath('//*[@id="searchResult"]/table')
for row in table.find_elements_by_xpath(".//tr"):
row_data = [td.text for td in row.find_elements_by_xpath(".//td")]
if row_data[0] == '':
continue
minutes, secends = row_data[6].split(':')
yield [
datetime.strptime(row_data[0] + row_data[16], '%d.%m.%Y%H:%M'),
float(row_data[3].split()[0].replace(',', '.')),
int(minutes) * 60 + int(secends)
]
def data():
logger.info('run data func')
browser = get_browser()
runalyze_login(browser)
items = []
for item in runalyze_search(browser):
items.append(item)
logger.info('add item %s', item[0])
logger.info('run data func end')
return items
def video():
logger.info('run video func')
def setup_logger(level=0):
logger = logging.getLogger()
levels = [logging.WARNING, logging.INFO, logging.DEBUG]
level = levels[level or 0]
logger.setLevel(level)
ch = logging.StreamHandler()
ch.setFormatter(logging.Formatter('%(levelname)s - %(message)s'))
logger.addHandler(ch)
def main():
funcs = {
'data': data,
'video': video,
}
parser = ArgumentParser()
parser.add_argument('-v', '--verbose', action='count')
parser.add_argument('action', nargs='?', choices=funcs.keys())
args = parser.parse_args()
setup_logger(args.verbose)
funcs.get(args.action, parser.print_help)()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment