├── requirements.txt ├── .github └── workflows │ └── give_kudos.yml ├── LICENSE ├── README.md ├── .gitignore └── give_kudos.py /requirements.txt: -------------------------------------------------------------------------------- 1 | playwright==1.30.0 2 | -------------------------------------------------------------------------------- /.github/workflows/give_kudos.yml: -------------------------------------------------------------------------------- 1 | name: Give Strava Kudos 2 | 3 | on: 4 | schedule: 5 | - cron: "30 5-23/6 * * *" 6 | workflow_dispatch: 7 | 8 | env: 9 | STRAVA_EMAIL: ${{ secrets.STRAVA_EMAIL }} 10 | STRAVA_PASSWORD: ${{ secrets.STRAVA_PASSWORD }} 11 | jobs: 12 | run-kudos-cron: 13 | runs-on: ubuntu-20.04 14 | timeout-minutes: 10 15 | steps: 16 | - uses: actions/checkout@v3 17 | - uses: actions/setup-python@v4 18 | with: 19 | python-version: '3.9.10' 20 | cache: 'pip' 21 | - run: pip install -r requirements.txt 22 | - run: playwright install 23 | - run: python give_kudos.py 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Isaac Chung 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Strava Kudos Giver 👍👍👍 2 | 3 | [![Python 3.9](https://img.shields.io/badge/python-3.9-blue.svg)](https://www.python.org/downloads/release/python-370/) ![Github Actions](https://github.com/isaac-chung/strava-kudos/actions/workflows/give_kudos.yml/badge.svg) 4 | 5 | A Python tool to automatically give [Strava](https://www.strava.com) Kudos to recent activities on your feed. There are a few repos that uses JavaScript like [strava-kudos-lambda](https://github.com/mjad-org/strava-kudos-lambda) and [strava-kudos](https://github.com/rnvo/strava-kudos). 6 | 7 | The repo is set up so that the script runs on a set schedule via Github Actions. Github suggests in their [docs](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule) to not run cron jobs at the start of every hour to avoid delays so minute30 was chosen here. Feel free to change it to whenever you want. There is also a `max_run_duration` parameter which is 9 minutes by default so that we don't exceed the [monthly Github Action free tier minutes](https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions#included-storage-and-minutes) when the action is triggered a few times a day. 8 | 9 | ## 🏃 Usage 10 | 1. Fork the repo 11 | 2. Setup the environment variables in secrets 12 | 3. Give kudos automatically! 13 | 14 | Alternatively, you can run the script manually with 15 | ``` 16 | python3 give_kudos.py 17 | ``` 18 | 19 | ## 🛠️Setup 20 | 21 | ### Playwright 22 | [Playwright](https://github.com/microsoft/playwright-python) is used, so be sure to follow instructions to install it properly. 23 | 24 | ### Environment Variables 25 | 26 | Set the environment variables for your email and password as follows: 27 | ``` 28 | export STRAVA_EMAIL=YOUR_EMAIL 29 | export STRAVA_PASSWORD=YOUR_PASSWORD 30 | ``` 31 | 32 | ### Github Actions 33 | To add secrets for GH actions, navigate to Settings -> Security -> Secrets and Variables -> Actions. Enter your email and password within `Repository Secrets`. 34 | 35 | ## 🔬Testing 36 | Manual testing was done in Python 3.9.10 on Ubuntu 20.04.6. 37 | 38 | ## Contributions 39 | Let me know if you wish to add anything or if there are any issues! 40 | 41 | [![ForTheBadge built-with-love](http://ForTheBadge.com/images/badges/built-with-love.svg)](https://GitHub.com/Naereen/) 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # Images 156 | *png 157 | 158 | .DS_Store 159 | -------------------------------------------------------------------------------- /give_kudos.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | from playwright.sync_api import sync_playwright 5 | 6 | BASE_URL = "https://www.strava.com/" 7 | 8 | class KudosGiver: 9 | """ 10 | Logins into Strava and gives kudos to all activities under 11 | Following. 12 | """ 13 | def __init__(self, max_run_duration=540) -> None: 14 | self.EMAIL = os.environ.get('STRAVA_EMAIL') 15 | self.PASSWORD = os.environ.get('STRAVA_PASSWORD') 16 | 17 | if self.EMAIL is None or self.PASSWORD is None: 18 | raise Exception("Must set environ variables EMAIL AND PASSWORD. \ 19 | e.g. run export STRAVA_EMAIL=YOUR_EMAIL") 20 | 21 | self.max_run_duration = max_run_duration 22 | self.start_time = time.time() 23 | self.num_entries = 100 24 | self.web_feed_entry_pattern = '[data-testid=web-feed-entry]' 25 | 26 | p = sync_playwright().start() 27 | self.browser = p.firefox.launch() # does not work in chrome 28 | self.page = self.browser.new_page() 29 | 30 | 31 | def email_login(self): 32 | """ 33 | Login using email and password 34 | """ 35 | self.page.goto(os.path.join(BASE_URL, 'login')) 36 | try: 37 | self.page.get_by_role("button", name="Reject").click(timeout=5000) 38 | except Exception as _: 39 | pass 40 | self.page.get_by_role("textbox", name='email').fill(self.EMAIL) 41 | self.page.get_by_role("textbox", name="password").fill(self.PASSWORD) 42 | self.page.get_by_role("button", name="Log In").click() 43 | print("---Logged in!!---") 44 | self._run_with_retries(func=self._get_page_and_own_profile) 45 | 46 | def _run_with_retries(self, func, retries=3): 47 | """ 48 | Retry logic with sleep in between tries. 49 | """ 50 | for i in range(retries): 51 | if i == retries - 1: 52 | raise Exception(f"Retries {retries} times failed.") 53 | try: 54 | func() 55 | return 56 | except Exception as _: 57 | time.sleep(1) 58 | 59 | def _get_page_and_own_profile(self): 60 | """ 61 | Limit activities count by GET parameter and get own profile ID. 62 | """ 63 | self.page.goto(os.path.join(BASE_URL, f"dashboard?num_entries={self.num_entries}")) 64 | 65 | ## Scrolling for lazy loading elements. 66 | for _ in range(5): 67 | self.page.keyboard.press('PageDown') 68 | time.sleep(0.5) 69 | self.page.keyboard.press('PageUp') 70 | 71 | try: 72 | self.own_profile_id = self.page.locator(".user-menu > a").get_attribute('href').split("/athletes/")[1] 73 | print("id", self.own_profile_id) 74 | except Exception as _: 75 | print("can't find own profile ID") 76 | 77 | def locate_kudos_buttons_and_maybe_give_kudos(self, web_feed_entry_locator) -> int: 78 | """ 79 | input: playwright.locator class 80 | Returns count of kudos given. 81 | """ 82 | w_count = web_feed_entry_locator.count() 83 | given_count = 0 84 | print(f"web feeds found: {w_count}") 85 | for i in range(w_count): 86 | # run condition check 87 | curr_duration = time.time() - self.start_time 88 | if curr_duration > self.max_run_duration: 89 | print("Max run duration reached.") 90 | break 91 | 92 | web_feed = web_feed_entry_locator.nth(i) 93 | p_count = web_feed.get_by_test_id("entry-header").count() 94 | 95 | # check if feed item is a club post 96 | if self.is_club_post(web_feed): 97 | print('c', end='') 98 | continue 99 | 100 | # check if activity has multiple participants 101 | if p_count > 1: 102 | for j in range(p_count): 103 | participant = web_feed.get_by_test_id("entry-header").nth(j) 104 | # ignore own activities 105 | if not self.is_participant_me(participant): 106 | kudos_container = web_feed.get_by_test_id("kudos_comments_container").nth(j) 107 | button = self.find_unfilled_kudos_button(kudos_container) 108 | given_count += self.click_kudos_button(unfilled_kudos_container=button) 109 | else: 110 | # ignore own activities 111 | if not self.is_participant_me(web_feed): 112 | button = self.find_unfilled_kudos_button(web_feed) 113 | given_count += self.click_kudos_button(unfilled_kudos_container=button) 114 | print(f"\nKudos given: {given_count}") 115 | return given_count 116 | 117 | def is_club_post(self, container) -> bool: 118 | """ 119 | Returns true if the container is a club post 120 | """ 121 | if(container.get_by_test_id("group-header").count() > 0): 122 | return True 123 | 124 | if(container.locator(".clubMemberPostHeaderLinks").count() > 0): 125 | return True 126 | 127 | return False 128 | 129 | def is_participant_me(self, container) -> bool: 130 | """ 131 | Returns true is the container's owner is logged-in user. 132 | """ 133 | owner = self.own_profile_id 134 | try: 135 | h = container.get_by_test_id("owners-name").get_attribute('href') 136 | hl = h.split("/athletes/") 137 | owner = hl[1] 138 | except Exception as _: 139 | print("Some issue with getting owners-name container.") 140 | return owner == self.own_profile_id 141 | 142 | def find_unfilled_kudos_button(self, container): 143 | """ 144 | Returns button as a playwright.locator class 145 | """ 146 | button = None 147 | try: 148 | button = container.get_by_test_id("unfilled_kudos") 149 | except Exception as _: 150 | print("Some issue with finding the unfilled_kudos container.") 151 | return button 152 | 153 | def click_kudos_button(self, unfilled_kudos_container) -> int: 154 | """ 155 | input: playwright.locator class 156 | Returns 1 if kudos button was clicked else 0 157 | """ 158 | if unfilled_kudos_container.count() == 1: 159 | unfilled_kudos_container.click(timeout=0, no_wait_after=True) 160 | print('=', end='') 161 | time.sleep(1) 162 | return 1 163 | return 0 164 | 165 | def give_kudos(self): 166 | """ 167 | Interate over web feed entries 168 | """ 169 | ## Give Kudos on loaded page ## 170 | try: 171 | self.page.get_by_role("button", name="Accept").click(timeout=5000) 172 | print("Accepting updated terms.") 173 | except Exception as _: 174 | pass 175 | web_feed_entry_locator = self.page.locator(self.web_feed_entry_pattern) 176 | self.locate_kudos_buttons_and_maybe_give_kudos(web_feed_entry_locator=web_feed_entry_locator) 177 | self.browser.close() 178 | 179 | 180 | def main(): 181 | kg = KudosGiver() 182 | kg.email_login() 183 | kg.give_kudos() 184 | 185 | 186 | if __name__ == "__main__": 187 | main() 188 | --------------------------------------------------------------------------------