[PYTHON] Automatic data migration from yahoo root lab to Strava

Automatic data migration from yahoo root lab to Strava

Yahoo's Route Lab will end in March 2020. Download the gpx file from Root Lab, upload it to Strava, and save the route you've taken to strava by the end of the service! However, it is troublesome to upload to Strava manually one by one, so let's upload automatically using the following method

2020 / 04.01 postscript The root lab is over, so if you just want to upload your .gpx file to Strava, see here (https://qiita.com/memento10/items/fe1a74ab0f4f284167fe).

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
pip install requests
pip install selenium

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 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 read_urls():
    with open("./urls.txt", "r") as f:
        for row in f:
            yield row.replace("\n", "")

def download_gpx(urls):
    
    for url in urls:
        response = requests.get(url)
        
        html = response.text
        
        soup = BeautifulSoup(html, "html.parser")

        gpx_tag = soup.find_all("a", href=re.compile("gpx"))
        
        route_name = soup.find(id="subtitle").string

        download_url = gpx_tag[0].get('href')
        
        res = requests.get("https://latlonglab.yahoo.co.jp/route/" + download_url.lstrip("."))

        save_name = route_name + ".gpx"

        with open(save_name, "wb") as f:
            f.write(res.content)
    
        yield save_name
        
        


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__":
    urls = list(read_urls())
    gpx_names = download_gpx(urls)
    upload_gpx_to_strava(gpx_names)

5. Place urls.txt with the following link to the root lab in the same directory as gpx.py

urls.txt


https://latlonglab.yahoo.co.jp/route/hoge1
https://latlonglab.yahoo.co.jp/route/hoge2
https://latlonglab.yahoo.co.jp/route/hoge3
...

6. Do the following

python gpx.py



  

  


Recommended Posts

Automatic data migration from yahoo root lab to Strava
Data retrieval from MacNote3 and migration to Write
From Elasticsearch installation to data entry
SIGNATE Quest ① From data reading to preprocessing
Easy script migration from CentOS to Oracle Linux
[Python] How to read data from CIFAR-10 and CIFAR-100
Data preprocessing (2) Data is changed from Categorical to Numerical.
[Python] Flow from web scraping to data analysis
How to Git from GCP's Jupyter Lab to GSR
[AWS] Migrate data from DynamoDB to Aurora MySQL