[PYTHON] How to automatically upload .gpx files to Strava

1. Find out which version of Google Chrome you are using

chrome The image is from https://www.iijmio.jp/thissite/version

2. Download the software "chromedriver" for Google Chrome

-Download from here.

chromedriver

--The downloaded one is in .zip format, so please put the extracted one in the executable format in your directory.

3. Open Terminal and do the following in Terminal

pip install beautifulsoup4 selenium requests

4. Rewrite the "email address" and "password" in the source code below to match your strava account

gpx.py


import os
import re
import sys
import time
from bs4 import BeautifulSoup
import requests
from glob import glob
from selenium import webdriver
from selenium.webdriver.chrome.options import Options



options = Options()
options.binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
driver = webdriver.Chrome(os.path.join(os.getcwd(), "chromedriver"))

def get_gpx_names():
    return glob('*.gpx')
    

def upload_gpx_to_strava(gpx_names):
    
    for i, gpx_name in enumerate(gpx_names):
        driver.get('https://labs.strava.com/gpx-to-route/#12/-122.44503/37.73651')

        driver.find_element_by_id("gpxFile").send_keys(os.path.join(os.getcwd(), gpx_name))

        time.sleep(15)

        if i == 0:
            driver.find_element_by_id("oauthButton").click()

            driver.switch_to.window(driver.window_handles[-1])

            driver.find_element_by_id("email").send_keys("mail address")

            driver.find_element_by_id("password").send_keys("password")

            login_button = driver.find_element_by_id("login-button")
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            login_button.click()

            time.sleep(5)

            driver.switch_to.window(driver.window_handles[-1])

        driver.find_element_by_id("saveButton").click()
        time.sleep(5)

        driver.find_element_by_class_name("save-route").click()

        driver.find_element_by_id("name").send_keys("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"+os.path.splitext(os.path.basename(gpx_name))[0])

        driver.find_element_by_class_name("reverse").click()

        time.sleep(5)
        
if __name__ == "__main__":
    gpx_names = get_gpx_names()
    upload_gpx_to_strava(gpx_names)

5. Put the .gpx file you want to upload in the same directory as gpx.py

6. Do the following

python gpx.py




Recommended Posts

How to automatically upload .gpx files to Strava
How to upload files in Django generic class view
How to upload files to Cloud Storage with Firebase's python SDK
[Linux] How to subdivide files and folders
How to create * .spec files for pyinstaller.
Automatically update CSV files to AWS DynamoDB
How to find large files on Linux
Use boto to upload / download files to s3.
How to read CSV files in Pandas
How to upload with Heroku, Flask, Python, Git (4)
How to create large files at high speed
Summary of how to import files in Python 3
How to check / extract files in RPM package
How to get the files in the [Python] folder
Upload files to Google Drive with Lambda (Python)
How to automatically start a GCP preemptible VM
How to upload to a shared drive using pydrive
How to make AWS rekognition recognize local image files
How to upload with Heroku, Flask, Python, Git (Part 3)
How to load files in Google Drive with Google Colaboratory
How to upload with Heroku, Flask, Python, Git (Part 1)
How to download files from Selenium in Python in Chrome
How to add page numbers to PDF files (in Python)
[Python] How to split and modularize files (simple, example)
How to upload with Heroku, Flask, Python, Git (Part 2)
How to reference static files in a Django project
Upload files with Django
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use shogun
How to use Pandas 2
How to read PyPI
How to install pip
How to use Virtualenv
How to use numpy.vectorize
How to update easy_install
How to install archlinux
How to use pytest_report_header
How to restart gunicorn
How to install python
How to virtual host
How to debug selenium
How to use partial
How to use Bio.Phylo
How to read JSON
How to use SymPy
How to use x-means
How to update Spyder
How to use IPython
How to install BayesOpt
How to use virtualenv
How to use Matplotlib
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to grow dotfiles
How to use list []