Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit e2562cd9 authored by freezed's avatar freezed :mountain: Committed by Freezed
Browse files

:twisted_rightwards_arrows: Merge branch '1-create-manual-activity'

* c8d063a4 - :sparkles: Add activity creation #1
* 611ee897 - :goal: Catch and print errors in API response #1
* 7dba5ee6 - :pencil: Update feature list #1

Closes #1

Resolve "Create manual activity". See merge request fcode/geostrapy!2
parents 76042194 7dba5ee6
No related branches found
No related tags found
No related merge requests found
Pipeline #4301 passed
......@@ -6,10 +6,10 @@ help: # Print help on Makefile
expand -t20
open_all: # Open all projects files
${EDITOR} ${VIRTUAL_ENV}/bin/activate
test -n ${VIRTUAL_ENV} && ${EDITOR} ${VIRTUAL_ENV}/bin/activate
${EDITOR} .gitignore .gitlab-ci.yml Makefile README.md requirements.txt requirements-dev.txt setup.cfg
${EDITOR} .git/hooks/p*-commit
${EDITOR} cli/run.py
${EDITOR} cli/*.py
pre_commit: # Run the pre-commit hook
.git/hooks/pre-commit
......
......@@ -12,19 +12,19 @@ A python CLI to share data related to physical activities over an API (geo-local
1. Respect python scripting guidelines proposed by _Vincent Bernat_ ( [🇫🇷](https://vincent.bernat.ch/fr/blog/2019-script-python-durable) | [🇬🇧](https://vincent.bernat.ch/en/blog/2019-sustainable-python-script) ).
(planed) Features
--------------------
✨ Features
------------
These are imagined with _[Strava](https://developers.strava.com/docs/reference/) in mind_, but any other service will be nice too :
* [Create manual activity](https://lab.frogg.it/fcode/geostrapy/-/issues/1)
* From a GPX track: [get characteristics](https://lab.frogg.it/fcode/geostrapy/-/issues/2)
* and [create an activity](https://lab.frogg.it/fcode/geostrapy/-/issues/3)
* Sanitise a GPX track:
* [crop start, end & stop points](https://lab.frogg.it/fcode/geostrapy/-/issues/4)
* [change start-time](https://lab.frogg.it/fcode/geostrapy/-/issues/5)
* [Post GPX file as activity](https://lab.frogg.it/fcode/geostrapy/-/issues/6)
* [Process GPX multi-track file](https://lab.frogg.it/fcode/geostrapy/-/issues/7)
* [x] [Create activity](https://lab.frogg.it/fcode/geostrapy/-/issues/1)
* [ ] From a GPX track: [get characteristics](https://lab.frogg.it/fcode/geostrapy/-/issues/2)
* [ ] and [create an activity](https://lab.frogg.it/fcode/geostrapy/-/issues/3)
* Sanitize a GPX track:
* [ ] [crop start, end & stop points](https://lab.frogg.it/fcode/geostrapy/-/issues/4)
* [ ] [change start-time](https://lab.frogg.it/fcode/geostrapy/-/issues/5)
* [ ] [Post modified GPX file as activity](https://lab.frogg.it/fcode/geostrapy/-/issues/6)
* [ ] [Process GPX multi-track file](https://lab.frogg.it/fcode/geostrapy/-/issues/7)
* and [maybe more](https://lab.frogg.it/fcode/geostrapy/-/boards)
......
"""
API utilities
"""
from strava.api._helpers import client, url, json
def post_activity(xargs):
"""API call to create an activity"""
response = client.post(url=url("/activities"), data=xargs)
if response.ok:
result = json(response)
else:
result = response.json()
return result
"""
CLI commands
"""
import datetime
import click
from strava.decorators import (
output_option,
login_required,
format_result,
TableFormat,
OutputType,
)
from strava.formatters import (
humanize,
apply_formatters,
)
from api import post_activity
_ACTIVITY_COLUMNS = ("key", "value")
@click.option(
"-n",
"--name",
prompt=True,
type=str,
required=True,
default="Activity",
help="The name of the activity",
)
@click.option(
"-a",
"--activity_type",
prompt=True,
type=str,
required=True,
default="run",
help="Type of activity",
)
@click.option(
"-s",
"--start",
prompt=True,
type=str,
required=True,
default=datetime.datetime.now().isoformat(),
help="ISO 8601 formatted date time",
)
@click.option("-t", "--time", prompt=True, type=int, required=True, help="In seconds")
@click.option("-d", "--desc", prompt=True, type=str, help="Description of the activity")
@click.option("-D", "--dist", prompt=True, type=int, help="In meters")
@click.option(
"-c",
"--commute",
prompt=True,
type=int,
default="0",
prompt_required=False,
help="Set to 1 to mark as commute",
)
@click.option(
"-T",
"--trainer",
prompt=True,
type=int,
default="0",
prompt_required=False,
help="Set to 1 to mark as a trainer activity",
)
@click.option(
"-h",
"--hide",
prompt=True,
type=int,
default="0",
prompt_required=False,
help="Set to true to mute activity",
)
@click.command("create")
@output_option()
@login_required
def post_create(**kwargs):
"""Create an activity"""
xargs = {
"name": kwargs["name"],
"type": kwargs["activity_type"],
"trainer": kwargs["trainer"],
"commute": kwargs["commute"],
"distance": kwargs["dist"],
"description": kwargs["desc"],
"elapsed_time": kwargs["time"],
"hide_from_home": kwargs["hide"],
"start_date_local": kwargs["start"],
}
result = post_activity(xargs)
_format_upload(result, output=kwargs["output"])
@format_result(
table_columns=_ACTIVITY_COLUMNS,
show_table_headers=False,
table_format=TableFormat.PLAIN,
)
def _format_upload(result, output=None):
return result if output == OutputType.JSON.value else _as_table(result)
def _as_table(upload_result):
def format_id(upload_id):
return f"{upload_id}"
def format_error(upload_error):
return f"{upload_error}"
def format_property(name):
return click.style(f"{humanize(name)}:", bold=True)
formatters = {
"id": format_id,
"errors": format_error,
}
basic_data = [
{"key": format_property(k), "value": v}
for k, v in apply_formatters(upload_result, formatters).items()
]
return [*basic_data]
......@@ -15,6 +15,8 @@ from strava.commands import (
set_config,
)
from commands import post_create
@click.group()
def cli():
......@@ -26,6 +28,7 @@ def cli():
cli.add_command(login)
cli.add_command(logout)
cli.add_command(post_create)
cli.add_command(set_config)
if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment