Skip to content

Instantly share code, notes, and snippets.

@georgestephanis
Last active April 23, 2019 14:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgestephanis/5d6018784518e449b0496b92179214de to your computer and use it in GitHub Desktop.
Save georgestephanis/5d6018784518e449b0496b92179214de to your computer and use it in GitHub Desktop.
First, run `composer require google/apiclient:^2.0` on cli -- then get your credentials.json from the link in the file and drop this file in the folder. You WILL need to populate the two ID constants. They can be the same calendar if you really want, but I prefer to keep my blockers on a second hidden calendar to be tidier.
<?php
// credentials.json is gotten from https://developers.google.com/calendar/quickstart/php
define( 'EVENT_CALENDAR_ID', '' );
define( 'BLOCKER_CALENDAR_ID', '' );
define( 'WEEKS_TO_CHECK', 6 );
define( 'WEEKLY_LIMIT', 3 );
define( 'WEEK_IN_SECONDS', 7*24*60*60 );
define( 'TZ_P', substr_replace( trim( shell_exec( 'date +"%z"' ) ), ':', 3, 0 ) );
define( 'CLR_RESET', "\e[0m" );
define( 'CLR_RED', "\e[1;31m" );
define( 'CLR_GREEN', "\e[1;32m" );
define( 'CLR_YELLOW', "\e[1;33m" );
define( 'CLR_BLUE', "\e[1;34m" );
define( 'CLR_PURPLE', "\e[35m" );
define( 'CLR_CYAN', "\e[1;36m" );
define( 'CLR_WHITE', "\e[1;37m" );
define( 'UNDERLINE', "\e[4m" );
define( 'BEEP', "\007" );
require __DIR__ . '/vendor/autoload.php';
if ( php_sapi_name() !== 'cli' ) {
throw new Exception( 'This application must be run on the command line.' );
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setApplicationName( 'Automattic Hiring Calend.ly Limiter' );
$client->setScopes( Google_Service_Calendar::CALENDAR_EVENTS );
$client->setAuthConfig( 'credentials.json' );
$client->setAccessType( 'offline' );
$client->setPrompt( 'select_account consent') ;
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if ( file_exists( $tokenPath ) ) {
$accessToken = json_decode( file_get_contents( $tokenPath ), true );
$client->setAccessToken( $accessToken );
}
// If there is no previous token or it's expired.
if ( $client->isAccessTokenExpired() ) {
// Refresh the token if possible, else fetch a new one.
if ( $client->getRefreshToken() ) {
$client->fetchAccessTokenWithRefreshToken( $client->getRefreshToken() );
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf( "Open the following link in your browser:\n%s\n", $authUrl );
print 'Enter verification code: ';
$authCode = trim( fgets( STDIN ) );
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode( $authCode );
$client->setAccessToken( $accessToken );
// Check to see if there was an error.
if ( array_key_exists( 'error', $accessToken ) ) {
throw new Exception( join( ', ', $accessToken ) );
}
}
// Save the token to a file.
if ( ! file_exists( dirname( $tokenPath ) ) ) {
mkdir( dirname( $tokenPath ), 0700, true );
}
file_put_contents( $tokenPath, json_encode( $client->getAccessToken() ) );
}
return $client;
}
function do_week( $time, $service ) {
$results = $service->events->listEvents( EVENT_CALENDAR_ID, array(
'q' => 'Powered by Calendly.com',
'timeMin' => date( 'Y-m-d\TH:i:s', $time ) . TZ_P,
'timeMax' => date( 'Y-m-d\TH:i:s', ( $time + WEEK_IN_SECONDS ) ) . TZ_P,
) );
$events = $results->getItems();
printf( "\n\n" . CLR_GREEN . "For the week of %s:\n" . CLR_RESET, date( 'F jS', $time ) );
if ( empty( $events ) ) {
print CLR_RED . " No interviews found.\n" . CLR_RESET;
} else {
printf( CLR_YELLOW . " %d " . CLR_RESET . "interviews found:\n" . CLR_RESET, sizeof( $events ) );
usort( $events, function( $a, $b ) {
return $a->start->dateTime <=> $b->start->dateTime;
} );
foreach ( $events as $event ) {
$start = $event->start->dateTime;
if ( empty( $start ) ) {
$start = $event->start->date;
}
printf( " %s (%s)\n", $event->getSummary(), date( 'l @ g:ia e', strtotime( $start ) ) );
}
if ( sizeof( $events ) >= WEEKLY_LIMIT ) {
$results = $service->events->listEvents( BLOCKER_CALENDAR_ID, array(
'q' => 'Automattic Interview Blocker',
'timeMin' => date( 'Y-m-d\TH:i:s', $time ) . TZ_P,
'timeMax' => date( 'Y-m-d\TH:i:s', ( $time + WEEK_IN_SECONDS - 1 ) ) . TZ_P,
) );
$blockers = $results->getItems();
if ( empty( $blockers ) ) {
$event = new \Google_Service_Calendar_Event( array(
'summary' => 'Automattic Interview Blocker',
'transparency' => 'opaque', // busy
'reminders' => array(
'useDefault' => 'false',
),
'start' => array(
'dateTime' => date( 'Y-m-d\TH:i:s', $time ) . TZ_P,
),
'end' => array(
'dateTime' => date( 'Y-m-d\TH:i:s', ( $time + WEEK_IN_SECONDS - 1 ) ) . TZ_P,
),
) );
$event = $service->events->insert( BLOCKER_CALENDAR_ID, $event );
print CLR_BLUE . ' Blocker created!' . CLR_RESET;
} else {
print CLR_BLUE . ' Blocker already exists!' . CLR_RESET;
}
}
}
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar( $client );
$week_start = strtotime( 'Last Sunday' );
for ( $i = 0; $i < WEEKS_TO_CHECK; $i++ ) {
do_week( $week_start + ( $i * WEEK_IN_SECONDS ), $service );
}
@tcn33
Copy link

tcn33 commented Apr 22, 2019

@georgestephanis How does the user find the calendar IDs? nvm, found it: Settings > select calendar in sidebar > "Integrate calendar" section

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