From 9f9bda9189278d6ab394c36e7635fe1ffee23477 Mon Sep 17 00:00:00 2001
From: Hugo LEVY-FALK <klafyvel@gmail.com>
Date: Tue, 30 Jan 2018 16:03:50 +0100
Subject: [PATCH] Les options c'est bon, surtout avec des champignons.

---
 main.py          | 74 ++++++++++++++++++++++++++++++++++++------------
 requirements.txt |  1 +
 2 files changed, 57 insertions(+), 18 deletions(-)

diff --git a/main.py b/main.py
index 39bab15..52acf6a 100644
--- a/main.py
+++ b/main.py
@@ -42,6 +42,26 @@ from oauth2client import tools
 from oauth2client.file import Storage
 
 import icalendar
+import docopt
+
+__doc__ = """
+NTNOE cancer.
+
+Because NTNOE is cancer, we had to do something to synchronise our Google
+agenda on it.
+
+Usage:
+  ntnoe_cancer
+  ntnoe_cancer [--level=<l> --days-future=<df> --days-past=<dp>]
+  ntnoe_cancer -h | --help
+
+Options:
+  -h --help             Show this screen.
+  --level=<l>           Level of courses (0=all, 1=skip blue courses (! includes language courses), 2=skip everything but exams, for real man only) [default: 0]
+  --days-future=<df>    Number of days in the future to look for the calendar [default: 15]
+  --days-past=<dp>      Number of days in the past to look for the calendar, perfect for time travellers [default: 0]
+"""
+
 
 DEBUG = False
 APP_DIR = os.path.dirname(os.path.abspath(__file__))
@@ -99,13 +119,25 @@ class Event:
     """
 
     # ColorId corresponding to course code
-    EVENT_COLOR = {
-        '9': '9',  # Amphi
-        '11': '10',  # TL
-        '10': '6',  # TD
-        '13': '5',  # Autre
-        '12': '3',  # Exam
+    class Course:
+        AMPHI = '9'
+        TL = '11'
+        TD = '10'
+        AUTRE = '13'
+        EXAM = '12'
 
+    SKILL_LEVEL = {
+        '0' : {Course.AMPHI, Course.TL, Course.TD, Course.AUTRE, Course.EXAM},
+        '1' : {Course.TL, Course.TD, Course.AUTRE, Course.EXAM},
+        '2' : {Course.EXAM},
+    }
+
+    EVENT_COLOR = {
+        Course.AMPHI: '9',
+        Course.TL: '10',
+        Course.TD: '6',
+        Course.AUTRE: '5',
+        Course.EXAM: '3',
     }
 
     def __init__(self, e):
@@ -114,8 +146,8 @@ class Event:
         self.start = e.decoded('DTSTART')
         self.end = e.decoded('DTEND')
         self.location = e.decoded('LOCATION').decode('utf-8')
-        self.colorid = self.EVENT_COLOR.get(
-            e.decoded('DESCRIPTION').decode('utf-8'), '1')
+        self.type = e.decoded('DESCRIPTION').decode('utf-8')
+        self.colorid = self.EVENT_COLOR.get(self.type, '1')
 
     def __str__(self):
         return str(self.as_google())
@@ -181,7 +213,7 @@ def get_ntnoe():
     return r.content
 
 
-def main():
+def main(arguments):
     """Get the events on NTNOE the puts them on Google Calendar.
     """
 
@@ -213,7 +245,9 @@ def main():
         ntnoe_calendar_id = created['id']
 
     now = datetime.datetime.now()
-    then = now + TIMEDELTA_SYNCHRO
+    then = now + datetime.timedelta(days=int(arguments["--days-future"]))
+    before = now - datetime.timedelta(days=int(arguments["--days-past"]))
+    logger.info('Looking for events between {} and {}.'.format(before, then))
 
     # NTNOE calendar often changes. So let's delete former synchronizations.
     logger.info('Deleting former events.')
@@ -228,18 +262,22 @@ def main():
             eventId=event['id']
         ).execute()
 
-    logger.info('Adding new events.')
+    l = arguments['--level']
+    logger.info('Adding new events with skill={}.'.format(l))
+    granted_events = Event.SKILL_LEVEL[l]
     for e in ical.walk('VEVENT'):
         event = Event(e)
-        if now >= event.end or event.start >= then:
+        if before >= event.end or event.start >= then:
             continue
 
-        event = service.events().insert(
-            calendarId=ntnoe_calendar_id,
-            body=event.as_google()
-        ).execute()
-        logger.debug("Adding event : {}".format(event['id']))
+        if event.type in granted_events:
+            event = service.events().insert(
+                calendarId=ntnoe_calendar_id,
+                body=event.as_google()
+            ).execute()
+            logger.debug("Adding event : {}".format(event['id']))
 
 
 if __name__ == '__main__':
-    main()
+    arguments = docopt.docopt(__doc__, version='NTNOE Cancer 1.0')
+    main(arguments)
diff --git a/requirements.txt b/requirements.txt
index ca679ab..d90f734 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -14,3 +14,4 @@ rsa==3.4.2
 six==1.11.0
 uritemplate==3.0.0
 urllib3==1.22
+docopt==0.6.2