From b2359e281cde43dbbb5144995871c729e982519f Mon Sep 17 00:00:00 2001 From: Noe Larrieu-Lacoste Date: Fri, 11 Dec 2020 17:23:48 +0100 Subject: [PATCH 01/14] update composer --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 590d36e..e96bf55 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,8 @@ ], "require": { "tchenu/myges": "dev-master", - "google/apiclient": "^2.7", "ext-json": "*", - "ext-ctype": "*" + "ext-ctype": "*", + "google/apiclient": "^2.8" } } From ffe64c32b5fcf23af8d9bcda24b28ecb1301adb9 Mon Sep 17 00:00:00 2001 From: Noe Larrieu-Lacoste Date: Fri, 11 Dec 2020 17:24:15 +0100 Subject: [PATCH 02/14] change order of arguments to put optionnal at end --- functions.php | 2 +- index.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/functions.php b/functions.php index 02fe337..fd0cd27 100644 --- a/functions.php +++ b/functions.php @@ -22,7 +22,7 @@ function getClient(): Client } } -function getAgenda(int $days = 7, Me $me): array +function getAgenda(Me $me, int $days = 7): array { return $me->getAgenda(getDateStart()->getTimestamp() * 1000, getDateEnd($days)->getTimestamp() * 1000); } diff --git a/index.php b/index.php index 5b7b3e7..9def145 100644 --- a/index.php +++ b/index.php @@ -20,7 +20,7 @@ printDivider(); printf("Récupération des cours sur %d jours..." . PHP_EOL, $days); -$agenda = getAgenda($days, $me); +$agenda = getAgenda($me, $days); printf("Réussi ! %d cours trouvés" . PHP_EOL, sizeof($agenda)); print "Traitement des doublons..." . PHP_EOL; From cf34e7102610e629b07a2cd3598490c6028fe05e Mon Sep 17 00:00:00 2001 From: Noe Larrieu-Lacoste Date: Fri, 11 Dec 2020 17:24:30 +0100 Subject: [PATCH 03/14] simpler date add --- functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions.php b/functions.php index fd0cd27..b9dfaab 100644 --- a/functions.php +++ b/functions.php @@ -37,7 +37,7 @@ function getDateStart(): DateTime function getDateEnd(int $days): DateTime { $end = new DateTime(); - date_add($end, date_interval_create_from_date_string($days . ' days')); + $end->add(date_interval_create_from_date_string($days . ' days')); $end->setTime(23, 59, 59); return $end; } From 64b1fd01e0a5e843fcdbc608e82659439bc7c37b Mon Sep 17 00:00:00 2001 From: Noe Larrieu-Lacoste Date: Fri, 11 Dec 2020 17:24:39 +0100 Subject: [PATCH 04/14] date interval winter mode --- functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions.php b/functions.php index b9dfaab..c5468be 100644 --- a/functions.php +++ b/functions.php @@ -104,12 +104,12 @@ function getCourseResume(Course $course): string $start = new DateTime(); $start->setTimestamp($course->start_date / 1000); - $start->add(date_interval_create_from_date_string("2 hours")); + $start->add(date_interval_create_from_date_string("1 hours")); $str .= ", Debut : " . $start->format("d-m-Y à H:i"); $end = new DateTime(); $end->setTimestamp($course->end_date / 1000); - $end->add(date_interval_create_from_date_string("2 hours")); + $end->add(date_interval_create_from_date_string("1 hours")); $str .= ", Fin : " . $end->format("d-m-Y à H:i"); if (!empty($course->rooms)) { From a68debf5e23a66b7f151280723174eed6a5cccb0 Mon Sep 17 00:00:00 2001 From: Noe Larrieu-Lacoste Date: Fri, 11 Dec 2020 17:24:51 +0100 Subject: [PATCH 05/14] return signature --- functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions.php b/functions.php index c5468be..6e55b11 100644 --- a/functions.php +++ b/functions.php @@ -173,7 +173,7 @@ function getCalendarClient(): Google_Client return $client; } -function getEvent(Google_Client $client, $days = 7) +function getEvent(Google_Client $client, $days = 7): Google_Service_Calendar_Events { $service = new Google_Service_Calendar($client); From 7b6d5ec43f065ac827faceeef87b26a45c4b59ef Mon Sep 17 00:00:00 2001 From: Noe Larrieu-Lacoste Date: Fri, 11 Dec 2020 17:25:06 +0100 Subject: [PATCH 06/14] remove old comments --- functions.php | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/functions.php b/functions.php index 6e55b11..087aee5 100644 --- a/functions.php +++ b/functions.php @@ -186,19 +186,6 @@ function getEvent(Google_Client $client, $days = 7): Google_Service_Calendar_Eve ); return $service->events->listEvents(calendar_id, $optParams); - - /*if (empty($events)) { - print "No upcoming events found.\n"; - } else { - print "Upcoming events:\n"; - foreach ($events as $event) { - $start = $event->start->dateTime; - if (empty($start)) { - $start = $event->start->date; - } - printf("%s - %s - (%s)\n", $event->getSummary(), $event->getDescription(), $start); - } - }*/ } function removeEvents(Google_Client $client, $events) @@ -208,14 +195,6 @@ function removeEvents(Google_Client $client, $events) foreach ($events as $event) { $service->events->delete(calendar_id, $event->getId()); } - - /* foreach ($events as $event) { - $start = $event->start->dateTime; - if (empty($start)) { - $start = $event->start->date; - } - printf("%s - %s - (%s)\n", $event->getSummary(), $event->getLocation(), $start); - }*/ } function getDateTimeForEvent($msTimestamp): string From 38d58c1662441c37001ca53377f6307730135903 Mon Sep 17 00:00:00 2001 From: Noe Larrieu-Lacoste Date: Fri, 11 Dec 2020 17:25:24 +0100 Subject: [PATCH 07/14] batch insert concept (big IMPROVE) --- functions.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/functions.php b/functions.php index 087aee5..1978618 100644 --- a/functions.php +++ b/functions.php @@ -247,6 +247,53 @@ function addEvents(Google_Client $client, array $agenda) } } +function batchInsert(Google_Client $client, array $agenda) +{ + $client->setUseBatch(true); + $service = new Google_Service_Calendar($client); + $batch_client = $service->createBatch(); +//don't set more than 50 + foreach ($agenda as $course) { + $course = Course::fromObject($course); + printf("Ajout du cours :%s%s" . PHP_EOL, PHP_EOL, getCourseResume($course)); + + $event = new Google_Service_Calendar_Event(); + $event->setSummary($course->name); + //TODO set location and color +// $event->setLocation() + + $description = ""; + + if (!empty($course->teacher) && strlen($course->teacher) > 1) { + $description .= "Intervenant : " . $course->teacher . "
"; + } + + if (!empty($course->rooms)) { + $description .= "Salle(s) :
    "; + + foreach ($course->rooms as $room) { + $description .= "
  • " . $room->campus . " - " . $room->name . "
  • "; + } + $description .= "
"; + } + + $event->setDescription($description); + + $start = new Google_Service_Calendar_EventDateTime(); + $start->setDateTime(getDateTimeForEvent($course->start_date)); + $event->setStart($start); + + $end = new Google_Service_Calendar_EventDateTime(); + $end->setDateTime(getDateTimeForEvent($course->end_date)); + $event->setEnd($end); + + $request = $service->events->insert(calendar_id, $event); + + $batch_client->add($request); + } + $batch_client->execute(); +} + function printDivider() { print "-------------------------------------------------------------------------------------------------------------" . From e2d923cdc88249e5383a08f52f5fbad838febbe7 Mon Sep 17 00:00:00 2001 From: Nouu Date: Sat, 12 Dec 2020 03:16:39 +0100 Subject: [PATCH 08/14] update composer to latest --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e96bf55..848cc4e 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,6 @@ "tchenu/myges": "dev-master", "ext-json": "*", "ext-ctype": "*", - "google/apiclient": "^2.8" + "google/apiclient": "^v2.8.3" } } From 671cb23dbea52de9f9f119a9f2ff290b250976e9 Mon Sep 17 00:00:00 2001 From: Nouu Date: Sat, 12 Dec 2020 03:17:58 +0100 Subject: [PATCH 09/14] update env example --- env.php.example | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/env.php.example b/env.php.example index 2ac5a16..7c06f0e 100644 --- a/env.php.example +++ b/env.php.example @@ -18,4 +18,8 @@ define("calendar_api_auth_config_file", ""); /** * Google calendar id (ex : vf2kq9ary690m8iee8ahv3e3d0@group.calendar.google.com) */ -define("calendar_id", ""); \ No newline at end of file +define("calendar_id", ""); +/** + * Max batch request (don't set above 50) + */ +define('max_batch_request', 45); \ No newline at end of file From cc7c1f31ef7a7a3f7c0902813e669c9ead053cd9 Mon Sep 17 00:00:00 2001 From: Nouu Date: Sat, 12 Dec 2020 03:18:13 +0100 Subject: [PATCH 10/14] finalize batch add and batch remove --- functions.php | 122 +++++++++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 57 deletions(-) diff --git a/functions.php b/functions.php index 1978618..40a03f9 100644 --- a/functions.php +++ b/functions.php @@ -8,6 +8,7 @@ require_once __DIR__ . '/models/Course.php'; require_once __DIR__ . '/models/Room.php'; + function getMe(): Me { return new Me(getClient()); @@ -212,86 +213,93 @@ function addEvents(Google_Client $client, array $agenda) foreach ($agenda as $course) { $course = Course::fromObject($course); printf("Ajout du cours :%s%s" . PHP_EOL, PHP_EOL, getCourseResume($course)); - - $event = new Google_Service_Calendar_Event(); - $event->setSummary($course->name); - //TODO set location and color -// $event->setLocation() - - $description = ""; - - if (!empty($course->teacher) && strlen($course->teacher) > 1) { - $description .= "Intervenant : " . $course->teacher . "
"; - } - - if (!empty($course->rooms)) { - $description .= "Salle(s) :
    "; - - foreach ($course->rooms as $room) { - $description .= "
  • " . $room->campus . " - " . $room->name . "
  • "; - } - $description .= "
"; - } - - $event->setDescription($description); - - $start = new Google_Service_Calendar_EventDateTime(); - $start->setDateTime(getDateTimeForEvent($course->start_date)); - $event->setStart($start); - - $end = new Google_Service_Calendar_EventDateTime(); - $end->setDateTime(getDateTimeForEvent($course->end_date)); - $event->setEnd($end); + $event = createGoogleEvent($course); $service->events->insert($calendarId, $event); } } -function batchInsert(Google_Client $client, array $agenda) +function batchAddEvents(Google_Client $client, array $agenda) { $client->setUseBatch(true); $service = new Google_Service_Calendar($client); $batch_client = $service->createBatch(); -//don't set more than 50 + $count = 0; foreach ($agenda as $course) { $course = Course::fromObject($course); printf("Ajout du cours :%s%s" . PHP_EOL, PHP_EOL, getCourseResume($course)); + $event = createGoogleEvent($course); - $event = new Google_Service_Calendar_Event(); - $event->setSummary($course->name); - //TODO set location and color -// $event->setLocation() - - $description = ""; - - if (!empty($course->teacher) && strlen($course->teacher) > 1) { - $description .= "Intervenant : " . $course->teacher . "
"; + $request = $service->events->insert(calendar_id, $event); + $count++; + $batch_client->add($request); + if ($count >= max_batch_request) { + //don't set more than 50 + $batch_client->execute(); + $batch_client = $service->createBatch(); + $count = 0; } + } + $batch_client->execute(); + $client->setUseBatch(false); +} - if (!empty($course->rooms)) { - $description .= "Salle(s) :
    "; +function batchRemoveEvents(Google_Client $client, Google_Service_Calendar_Events $events) +{ + $client->setUseBatch(true); + $service = new Google_Service_Calendar($client); + $batch_client = $service->createBatch(); + $count = 0; - foreach ($course->rooms as $room) { - $description .= "
  • " . $room->campus . " - " . $room->name . "
  • "; - } - $description .= "
"; + foreach ($events as $event) { + $count++; + $request = $service->events->delete(calendar_id, $event->getId()); + $batch_client->add($request); + + if ($count >= max_batch_request) { + //don't set more than 50 + $batch_client->execute(); + $batch_client = $service->createBatch(); + $count = 0; } - $event->setDescription($description); + } + $batch_client->execute(); + $client->setUseBatch(false); +} + +function createGoogleEvent(Course $course): Google_Service_Calendar_Event +{ + $event = new Google_Service_Calendar_Event(); + $event->setSummary($course->name); + //TODO set location and color +// $event->setLocation() - $start = new Google_Service_Calendar_EventDateTime(); - $start->setDateTime(getDateTimeForEvent($course->start_date)); - $event->setStart($start); + $description = ""; - $end = new Google_Service_Calendar_EventDateTime(); - $end->setDateTime(getDateTimeForEvent($course->end_date)); - $event->setEnd($end); + if (!empty($course->teacher) && strlen($course->teacher) > 1) { + $description .= "Intervenant : " . $course->teacher . "
"; + } - $request = $service->events->insert(calendar_id, $event); + if (!empty($course->rooms)) { + $description .= "Salle(s) :
    "; - $batch_client->add($request); + foreach ($course->rooms as $room) { + $description .= "
  • " . $room->campus . " - " . $room->name . "
  • "; + } + $description .= "
"; } - $batch_client->execute(); + + $event->setDescription($description); + + $start = new Google_Service_Calendar_EventDateTime(); + $start->setDateTime(getDateTimeForEvent($course->start_date)); + $event->setStart($start); + + $end = new Google_Service_Calendar_EventDateTime(); + $end->setDateTime(getDateTimeForEvent($course->end_date)); + $event->setEnd($end); + return $event; } function printDivider() From 22d36525084ba496462795b959319038c12296b9 Mon Sep 17 00:00:00 2001 From: Nouu Date: Sat, 12 Dec 2020 03:18:22 +0100 Subject: [PATCH 11/14] implement batch to index --- index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 9def145..438938c 100644 --- a/index.php +++ b/index.php @@ -39,12 +39,12 @@ printDivider(); print "Nettoyage des cours sur le calendrier google..." . PHP_EOL; -removeEvents($client, $events); +batchRemoveEvents($client, $events); print "Les cours ont été supprimés de l'agenda google!" . PHP_EOL; printDivider(); print "Ajout des cours sur le calendrier google..." . PHP_EOL; -addEvents($client, $agenda); +batchAddEvents($client, $agenda); printDivider(); print "Les cours ont été ajoutés!" . PHP_EOL; From 55fdfcaafe337887cf0d376e48ffa63f9db8628d Mon Sep 17 00:00:00 2001 From: Nouu Date: Sat, 12 Dec 2020 18:50:15 +0100 Subject: [PATCH 12/14] take argv --- index.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/index.php b/index.php index 438938c..ae3a406 100644 --- a/index.php +++ b/index.php @@ -7,14 +7,15 @@ print("MYGES CALENDAR SYNC (by Nospy)" . PHP_EOL); printDivider(); -print "Entrez le nombre de jours que vous souhaitez synchroniser à partir d'aujourd'hui : "; -$days = trim(fgets(STDIN)); -if (!ctype_digit($days)) { - die; +$days = $argv[1]; + +while (!ctype_digit($days) && $days >= 0) { + print "Entrez le nombre de jours que vous souhaitez synchroniser à partir d'aujourd'hui : "; + $days = trim(fgets(STDIN)); } printDivider(); -print "Connexion à myges..." . PHP_EOL; +print "Connexion à MyGES..." . PHP_EOL; $me = getMe(); print "Connecté !" . PHP_EOL; @@ -25,7 +26,7 @@ print "Traitement des doublons..." . PHP_EOL; $agenda = removeDuplicate($agenda); -printf("Réussi : %d cours uniques" . PHP_EOL, sizeof($agenda)); +printf("Réussi ! %d cours uniques" . PHP_EOL, sizeof($agenda)); printDivider(); print "Connexion à l'API google..." . PHP_EOL; From 074aaf2a482de2a5d611c3f9731c7bb55b15416c Mon Sep 17 00:00:00 2001 From: Nouu Date: Sat, 12 Dec 2020 18:56:17 +0100 Subject: [PATCH 13/14] update readme --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3bb2f70..a1081b1 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,13 @@ Version php utilisé : 7.4 * `calendar_api_application_name` [Nom de l'API google calendar](#api-google) * `calendar_api_auth_config_file` [Nom du fichiers d'authentification à l'API google calendar.](#api-google) * `calendar_id`[ Id du calendrier google](#calendrier-google) (ex: vf2kq9ary690m8iee8ahv3e3d0@group.calendar.google.com) - + * `max_batch_request` Nombre maximum d'évènement dans une seule requête pour l'api (ne pas depasser 50 !) ## Lancement -Une fois le fichier `env.php` rempli, lancez (avec linux ou phpstorm de préférence, **evitez git bash !**) la commande : `php index.php`. +Une fois le fichier `env.php` rempli, lancez (avec linux ou phpstorm de préférence, **evitez git bash !**) la commande : `php run.php`. + +Le programme vous demandera le nombre de jours à synchroniser à partir d'aujourd'hui. -Le programme vous demandera le nombre de jours à synchroniser à partir d'aujourd'hui. +Vous pouvez également indiquer le nombre de jours voulu directement depuis la ligne de commande de cette manière : `php run.php 7` ![image](https://i.imgur.com/qojtmG6.png) From 5d4932487cf2829c581d791e89caa2e532e9f5d6 Mon Sep 17 00:00:00 2001 From: Nouu Date: Sat, 12 Dec 2020 18:56:27 +0100 Subject: [PATCH 14/14] rename index.php --- index.php => run.php | 102 +++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 51 deletions(-) rename index.php => run.php (96%) diff --git a/index.php b/run.php similarity index 96% rename from index.php rename to run.php index ae3a406..065d355 100644 --- a/index.php +++ b/run.php @@ -1,52 +1,52 @@ -= 0) { - print "Entrez le nombre de jours que vous souhaitez synchroniser à partir d'aujourd'hui : "; - $days = trim(fgets(STDIN)); -} - -printDivider(); -print "Connexion à MyGES..." . PHP_EOL; -$me = getMe(); -print "Connecté !" . PHP_EOL; - -printDivider(); -printf("Récupération des cours sur %d jours..." . PHP_EOL, $days); -$agenda = getAgenda($me, $days); -printf("Réussi ! %d cours trouvés" . PHP_EOL, sizeof($agenda)); - -print "Traitement des doublons..." . PHP_EOL; -$agenda = removeDuplicate($agenda); -printf("Réussi ! %d cours uniques" . PHP_EOL, sizeof($agenda)); - -printDivider(); -print "Connexion à l'API google..." . PHP_EOL; -$client = getCalendarClient(); -print "Connecté !" . PHP_EOL; - -printDivider(); -printf("Récupération de la liste des cours présent sur le calendrier google sur %d jours..." . PHP_EOL, $days); -$events = getEvent($client, $days); -printf("Réussi ! %d cours trouvés" . PHP_EOL, sizeof($events)); - -printDivider(); -print "Nettoyage des cours sur le calendrier google..." . PHP_EOL; -batchRemoveEvents($client, $events); -print "Les cours ont été supprimés de l'agenda google!" . PHP_EOL; - -printDivider(); -print "Ajout des cours sur le calendrier google..." . PHP_EOL; -batchAddEvents($client, $agenda); -printDivider(); -print "Les cours ont été ajoutés!" . PHP_EOL; - += 0) { + print "Entrez le nombre de jours que vous souhaitez synchroniser à partir d'aujourd'hui : "; + $days = trim(fgets(STDIN)); +} + +printDivider(); +print "Connexion à MyGES..." . PHP_EOL; +$me = getMe(); +print "Connecté !" . PHP_EOL; + +printDivider(); +printf("Récupération des cours sur %d jours..." . PHP_EOL, $days); +$agenda = getAgenda($me, $days); +printf("Réussi ! %d cours trouvés" . PHP_EOL, sizeof($agenda)); + +print "Traitement des doublons..." . PHP_EOL; +$agenda = removeDuplicate($agenda); +printf("Réussi ! %d cours uniques" . PHP_EOL, sizeof($agenda)); + +printDivider(); +print "Connexion à l'API google..." . PHP_EOL; +$client = getCalendarClient(); +print "Connecté !" . PHP_EOL; + +printDivider(); +printf("Récupération de la liste des cours présent sur le calendrier google sur %d jours..." . PHP_EOL, $days); +$events = getEvent($client, $days); +printf("Réussi ! %d cours trouvés" . PHP_EOL, sizeof($events)); + +printDivider(); +print "Nettoyage des cours sur le calendrier google..." . PHP_EOL; +batchRemoveEvents($client, $events); +print "Les cours ont été supprimés de l'agenda google!" . PHP_EOL; + +printDivider(); +print "Ajout des cours sur le calendrier google..." . PHP_EOL; +batchAddEvents($client, $agenda); +printDivider(); +print "Les cours ont été ajoutés!" . PHP_EOL; + print "Finit." . PHP_EOL; \ No newline at end of file