openambitproject / openambit

openambit
277 stars 82 forks source link

strava_uploader.py #269

Open fmoerl-real opened 3 years ago

fmoerl-real commented 3 years ago

Hi there,

today -- seems Movescount is already down and don't accepts new logs -- I tried a bit with the script. At last, I figured out, how to use it, but as mentioned in the description, it is not that easy. Most important, to use the script, an App has to be registered at Strava. Assume, around 1000 people will use the script, so 1000 different apps will be registered at Strava, but it is all the same one.

Moreover, to write data to Strava, a write-authorization must be given. In a usual user case, Strava then attempts to get access to the localhost, where the files are stored, that is the computer of a single user. To my understanding, Strava apps are thought to communicate with a server, in the case of single users storing and providing their logs, a port must be opened for the communication.

All this is described only scarce, I had to figure it out by trial and error.

How can we improve this? Is there a possibility to provide a server for the upload? Otherwise, how would be the workaround to give Strava the authorization to look at the own machine?

Greetings from Germany

Falk

gustavo-iniguez-goya commented 3 years ago

A "proxy2Strava" web app can be used for this. Openambit sends the authorization token request to a web app (for example openambit2strava.appspot.com), and this web app forwards the request to Strava. This web app is used only to get the auth token to talk with Strava, and to refresh it when needed. The moves are sent drectly from your computer to Strava with the token you have received. But of course, the token travels through the web app.

You only have to create one app on Strava, and the credentials are stored on that wep app.

A working example of this concept is here, if someone wants to take a look to get some ideas: https://github.com/gustavo-iniguez-goya/openambit/tree/main/src/openambit/socialnetworks

The uploaded format can be either GPX or TCX.

fmoerl-real commented 3 years ago

In principle, I understand what you're doing. But again, the web app (openambit2strava.appspot.com) is personalized on your space here on github. Would every user have to provide such a space for his own uploads for the authorisation by strava? If so, creating such an own space should be described in the description of the strava_uploader.py readme.

gustavo-iniguez-goya commented 3 years ago

Would every user have to provide such a space for his own uploads for the authorisation by strava?

no, The Openambit Community could have a server to get a token from Strava transparently to the user, so the users don't have to register a new app on Strava and enter the credentials on their own. You could just use the webapp I created, but I understand that many people would be concerned about privacy and how those tokens are treated.

It's just one way of doing it. Another way could be to embed in openambit the credentials of a Strava app.

Gerold-R commented 3 years ago

@fmoerl-real

What about using a webdriver to upload to Strava? This worked for me:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

# Firefox selenium drivers: https://github.com/mozilla/geckodriver/releases
# Replace with your full filename to geckodriver:
driver = webdriver.Firefox(executable_path='C:/Suunto/geckodriver-win64/geckodriver.exe')

def strava_login(user_email, user_pwd):
    driver.get('http://www.strava.com/login')
    ele_email = driver.find_element_by_id('email')
    ele_email.send_keys(user_email)
    ele_pwd = driver.find_element_by_id('password')
    ele_pwd.send_keys(user_pwd)
    ele_btn_login = driver.find_element_by_id('login-button')
    ele_btn_login.click()

def strava_upload_file(filename):
    driver.get('http://www.strava.com/upload/select')
    try:
        element = driver.find_element_by_name('files[]')
        element.send_keys(filename)
        return True
    except NoSuchElementException:
        return False

# Login to Strava (maybe not required if already logged in)
# Replace with your credentials:
strava_login('<your email>', '<your password>')

# Replace with the filename to upload:
gpx_fn = 'C:\\Suunto\\sdsClient\\test.gpx'
if not strava_upload_file(gpx_fn):
    print('You are probably not logged in')

On Linux, you must use a driver (Firefox, Chrome) for Linux. See link above. Could be integrated into the other Python script on my Google drive.

Gruss Gerold

Gerold-R commented 3 years ago

Comments in the Python script are creating problems here?

marguslt commented 3 years ago

@Gerold-R ,

Comments in the Python script are creating problems here?

Markdown kicks in if you skip code fencing / code blocks :

```python
 # Firefox selenium drivers: https://github.com/mozilla/geckodriver/releases
 # Replace with your full filename to geckodriver:
 driver = webdriver.Firefox(executable_path='C:/Suunto/geckodriver-win64/geckodriver.exe')
 =>

 ```python
 # Firefox selenium drivers: https://github.com/mozilla/geckodriver/releases
 # Replace with your full filename to geckodriver:
 driver = webdriver.Firefox(executable_path='C:/Suunto/geckodriver-win64/geckodriver.exe')
 ...