Skip to content

[suggestion] i wanted an option to specify a date #19

Description

@luffah

A little suggestion to specify a start date (instead of now).
Too, in python, code is clear enough not to comment every lines.

ヽ(´ー`)┌
--- trainline_cli.py	2021-02-17 18:40:51.312990416 +0100
+++ trainline_cli.py	2021-02-17 19:17:46.396430200 +0100
@@ -4,9 +4,16 @@
 """CLI tool for trainline."""
 import click
 import trainline
+import re
 from datetime import datetime, timedelta
 
 # Usage : trainline_cli.py --help
+DATE_FORMAT="%d/%m/%Y %H:%M"
+KNOWN_DATE_FORMATS=list(set([
+    DATE_FORMAT,
+    "%d/%m/%Y", "%d/%m/%Y %H:%M",
+    '%Y-%m-%d', '%Y-%m-%d %H:%M'
+]))
 
 
 @click.command()
@@ -24,9 +31,16 @@
     required=True,
 )
 @click.option(
+    '--start', '-s',
+    type=click.DateTime(formats=KNOWN_DATE_FORMATS),
+    help='start date',
+    default=datetime.now().strftime(DATE_FORMAT),
+    show_default=True,
+)
+@click.option(
     '--next', '-n',
     type=str,
-    help='period of search from now \
+    help='period of search from start date \
 (example : 1day, 2days, 3d, 1hour, 2hours, 3h)',
     default='3hours',
     show_default=True,
@@ -43,21 +57,16 @@
     is_flag=True,
     help='verbose mode',
 )
-def main(departure, arrival, next, transport, verbose):
+def main(departure, arrival, start, next, transport, verbose):
     """ Search trips with Trainline and returns it in csv """
 
-    # Get current datetime > from_date
-    from_date_obj = datetime.now()
-
-    # Decode duration (ex : 1day => timedelta(days=1))
+    from_date = start
     delta = _decode_next_param(next)
-
-    # Calculate the end date > to_date
-    to_date_obj = from_date_obj + delta
+    to_date = from_date + delta
 
     # Convert the datetime objects to strings
-    from_date = from_date_obj.strftime("%d/%m/%Y %H:%M")
-    to_date = to_date_obj.strftime("%d/%m/%Y %H:%M")
+    from_date = from_date.strftime(DATE_FORMAT)
+    to_date = to_date.strftime(DATE_FORMAT)
 
     if transport == "any":
         transport = None

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions