<?php
/**
* File: archive-everything.php
* Purpose: Download everything of interest from your organization(s)
*/
CONST APIKEY = 'YOUR-KEY-HERE';
CONST APIURL = 'https://SITE.lucidmeetings.com/lucid/api/v1/';
date_default_timezone_set('America/Los_Angeles');
/**
* Callback function to extract headers from the CURL response.
*/
$curl_headers = array();
function header_line($curl, $header_line) {
global $curl_headers;
$curl_headers[] = $header_line;
return strlen($header_line);
}
/**
* Function to prepare and execute an API call via CURL.
*/
function send_request($uri, $method='GET', $params=NULL, $data=NULL) {
global $curl_headers;
// Minor sanitizing on the method param
$method = strtoupper($method);
$headers = array('Content-Type: application/json', 'X-API-KEY: ' . APIKEY);
$curl_headers = array();
// Build the API request string
$request = APIURL . $uri . ($params ? '?' . http_build_query($params) : '');
$result = NULL;
// Build the request/response container
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'header_line');
switch ($method) {
case 'GET':
curl_setopt($ch, CURLOPT_POST, FALSE);
break;
case 'POST':
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case 'PUT':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
break;
default:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
// Do the request
$result = new StdClass();
$result->response = curl_exec($ch);
$result->code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result->meta = curl_getinfo($ch);
// Check for errors
$curl_error = ($result->code > 0 ? NULL : curl_error($ch) . ' (' . curl_errno($ch) . ')');
curl_close($ch);
if ($curl_error) {
print $curl_error;
exit;
}
// Check for rate limiting
while ($result->code == 429) {
$rate_limit_reset = 0;
foreach ($curl_headers as $header) {
if (strstr($header, 'X-Rate-Limit-Reset')) {
list(, $rate_limit_reset) = explode(':', $header);
$rate_limit_reset = trim($rate_limit_reset);
break;
}
}
$rate_limit_reset += 1;
print 'Rate limit exceeded, waiting ' . $rate_limit_reset . ' seconds... '. "\n";
sleep($rate_limit_reset);
$result = send_request($uri, $method, $params, $data);
}
return $result;
}
//////////////////////////////////////////////////////////////////////
// Retrieve all the organizations this user manages
//////////////////////////////////////////////////////////////////////
print 'Getting organizations...' . "\n";
// Get the organizations and their rooms
$endpoint = 'organizations';
$params = array('fields' => 'organization_id,name', 'manager' => 'true', 'embed' => 'rooms');
$result = send_request($endpoint, 'GET', $params);
// Decode the JSON response
$data = json_decode($result->response, TRUE);
//////////////////////////////////////////////////////////////////////
// Walk through the organization list returned for this API Key
//////////////////////////////////////////////////////////////////////
foreach ($data as $organization) {
$organization_id = $organization['organization_id'];
$organization_name = $organization['name'];
print 'Getting ' . $organization_name . '... ' . "\n";
// Local path for this organization's data
$organization_directory = 'organizations/' . preg_replace('/\s+/', '_', $organization_name) . '_' . $organization_id;
@mkdir($organization_directory, 0775, TRUE);
//////////////////////////////////////////////////////////////////////
// Walk through the rooms for this organization
//////////////////////////////////////////////////////////////////////
foreach ($organization['rooms'] as $room) {
$room_id = $room['room_id'];
$room_name = $room['name'];
print 'Getting ' . $room_name . '... ' . "\n";
// Local path for this room's data
$room_directory = $organization_directory . '/rooms/' . preg_replace('/\s+/', '_', $room_name) . '_' . $room_id;
@mkdir($room_directory, 0775, TRUE);
//////////////////////////////////////////////////////////////////////
// Get the people in this room
//////////////////////////////////////////////////////////////////////
print 'Getting room members... ' . "\n";
$endpoint = 'rooms/' . $room_id . '/room_members';
$params = array('format' => 'xlsx', 'per_page' => 1000);
$result = send_request($endpoint, 'GET', $params);
// Get the filename from the Content-Type header
$filename = NULL;
foreach ($curl_headers as $header) {
if (strstr($header, 'filename=')) {
list(, $filename) = explode('filename=', $header);
$filename = preg_replace('/\s+/', '_', trim($filename, " \t\n\r\0\x0B\"\'"));
break;
}
}
// Save the file
if ($filename) {
file_put_contents($room_directory . '/' . $filename, $result->response);
}
//////////////////////////////////////////////////////////////////////
// Get the action items in this room
//////////////////////////////////////////////////////////////////////
print 'Getting action items... ' . "\n";
$endpoint = 'rooms/' . $room_id . '/action_items';
$params = array('format' => 'xlsx', 'per_page' => 1000);
$result = send_request($endpoint, 'GET', $params);
// Get the filename from the Content-Type header
$filename = NULL;
foreach ($curl_headers as $header) {
if (strstr($header, 'filename=')) {
list(, $filename) = explode('filename=', $header);
$filename = preg_replace('/\s+/', '_', trim($filename, " \t\n\r\0\x0B\"\'"));
break;
}
}
// Save the file
if ($filename) {
file_put_contents($room_directory . '/' . $filename, $result->response);
}
//////////////////////////////////////////////////////////////////////
// Get the meetings in this room
//////////////////////////////////////////////////////////////////////
print 'Getting meetings... ' . "\n";
$endpoint = 'rooms/' . $room_id . '/meetings';
$params = array('fields' => 'meeting_id,name,start_time', 'timeframe' => 'past', 'per_page' => 1000);
$result = send_request($endpoint, 'GET', $params);
// Decode the JSON response
$meeting_data = json_decode($result->response, TRUE);
// Walk through the meetings list returned for this room
foreach ($meeting_data as $meeting) {
$meeting_id = $meeting['meeting_id'];
$meeting_name = $meeting['name'];
$meeting_time = $meeting['start_time']['value'];
print 'Getting ' . $meeting_name . '... ' . "\n";
// Request a meeting export
$params = array('format' => 'pdf', 'embed' => 'attachments,attendance,chat');
$result = send_request('meetings/' . $meeting_id, 'GET', $params);
// Get the filename from the Content-Type header
$filename = NULL;
foreach ($curl_headers as $header) {
if (strstr($header, 'filename=')) {
list(, $filename) = explode('filename=', $header);
$filename = preg_replace('/\s+/', '_', trim($filename, " \t\n\r\0\x0B\"\'"));
break;
}
}
// Save the file
if ($filename) {
$meetings_directory = $room_directory . '/meetings/' . date('Ym', $meeting_time);
@mkdir($meetings_directory, 0775, TRUE);
file_put_contents($meetings_directory . '/' . $filename, $result->response);
}
}
print "\n";
}
print "\n";
}