From afe971bc2594904edbcf60ac0d7996452fea36c5 Mon Sep 17 00:00:00 2001
From: Martin Mares <mj@ucw.cz>
Date: Wed, 18 Mar 2020 20:05:24 +0100
Subject: [PATCH] Recurring meetings done right (hopefully)

---
 db.ddl            |  5 +++--
 fetch-meetings.py | 38 +++++++++++++++++++++++++++++++++++---
 2 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/db.ddl b/db.ddl
index 3332c6b..e969836 100644
--- a/db.ddl
+++ b/db.ddl
@@ -7,8 +7,9 @@ CREATE TABLE zoom_users (
 
 CREATE TABLE zoom_meetings (
 	id		serial		PRIMARY KEY,		-- our internal
-	meeting_id	int		NOT NULL,		-- Zoom's meeting ID (not unique for recurring meetings!)
-	uuid		varchar(255)	NOT NULL,		-- Zoom's
+	meeting_id	int		NOT NULL,		-- Zoom's meeting ID
+	uuid		varchar(255)	NOT NULL,		-- Zoom's meeting instance ID
+	occurrence_id	bigint		DEFAULT NULL,		-- Occurrence for recurring meetings
 	host_id		int		NOT NULL REFERENCES zoom_users(id),
 	topic		varchar(255)	DEFAULT '',
 	type		int		NOT NULL,		-- 1=instant, 2=scheduled, 3=recurring no time, 8=recurring fixed time
diff --git a/fetch-meetings.py b/fetch-meetings.py
index dc8b911..7037637 100755
--- a/fetch-meetings.py
+++ b/fetch-meetings.py
@@ -9,9 +9,10 @@ import psycopg2.extras
 import time
 import dateutil.parser
 # import dateutil.tz
+import sys
 
 verbose = True
-very_verbose = False
+very_verbose = True
 
 config = configparser.ConfigParser()
 config.read('zoom.ini')
@@ -29,11 +30,42 @@ def parse_time(iso_time, tz_name):
     return utc
 
 
+def add_recurring(uid, meet):
+    time.sleep(0.2)
+    resp = client.meeting.get(host_id = meet['host_id'], id = meet['id'])
+    resp.raise_for_status()
+    details = json.loads(resp.content)
+    if very_verbose:
+        pprint(details)
+
+    for occ in details["occurrences"]:
+        db.execute("""
+                INSERT INTO zoom_meetings
+                (meeting_id, uuid, occurrence_id, host_id, topic, type, start_time, duration)
+                VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
+            """, (
+                meet['id'],
+                meet['uuid'],
+                occ['occurrence_id'],
+                uid,
+                meet['topic'],
+                meet['type'],
+                parse_time(occ['start_time'], meet['timezone']),
+                occ['duration'],
+            ))
+
+
 def add_meeting(uid, meet):
-    if 'start_time' not in meet:
+    type = meet['type']
+    if type == 3:
         # Type 3 meetings (scheduled with no time) are ignored
         return
 
+    if type == 8:
+        # Recurring meetings: need to ask for the list of occurrences
+        add_recurring(uid, meet)
+        return
+
     db.execute("""
             INSERT INTO zoom_meetings
             (meeting_id, uuid, host_id, topic, type, start_time, duration)
@@ -59,7 +91,7 @@ def get_meetings(uid, user_id):
         if verbose:
             print(f"Fetching meetings for user {user_id}: page {page_id} of {num_pages}")
 
-        resp = client.meeting.list(user_id = user_id, type = 'upcoming', page_number = page_id)
+        resp = client.meeting.list(user_id = user_id, type = 'scheduled', page_number = page_id)
         resp.raise_for_status()
 
         meeting_list = json.loads(resp.content)
-- 
GitLab