Created
July 17, 2020 03:29
-
-
Save patrick-mcdougle/ba5341411ddc5d468fb9ad0798f778cf to your computer and use it in GitHub Desktop.
Reads a CSV of IMDB List Data and outputs a .ics compatible data for their 25x anniversaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // PHP >= 7.1 (because of the ical library used) | |
// Autoload file comes from composer | |
require_once __DIR__.'/../vendor/autoload.php'; | |
// composer require eluceo/ical | |
// https://packagist.org/packages/eluceo/ical | |
use Eluceo\iCal\Component\Calendar; | |
use Eluceo\iCal\Component\Event; | |
function createAllDayEvent(\DateTimeInterface $day, $summary = '', $description = '') { | |
$event = new Event(); | |
$event->setDtStart($day); | |
$event->setDtEnd($day); | |
$event->setNoTime(true); | |
$event->setSummary($summary); | |
$event->setDescription($description); | |
return $event; | |
} | |
// This is the date to start adding events (defaults to today) | |
$earlyBounds = new DateTimeImmutable(''); | |
// This is the date which it will not add any events after this date. | |
$lateBounds = new DateTimeImmutable('December 31, 2099'); | |
$calendar = new Calendar('Anniversaries'); | |
// Reads a csv from this file path (IMDB list export formatted) | |
$csvHandle = fopen(__DIR__.'/../data.csv', 'r'); | |
// Throw away the header row. | |
$headerRow = fgetcsv($csvHandle); | |
while($data = fgetcsv($csvHandle)) { | |
$data = array_combine($headerRow, $data); | |
unset($data["Position"], $data["Const"], $data["Created"], $data["Modified"]); | |
// Create a cursor starting at the release date of the movie | |
$dateCursor = new \DateTimeImmutable($data['Release Date']); | |
// This will be the event description in the calendar event. | |
$eventDescription = <<<EOT | |
{$data["Title"]} ({$dateCursor->format('Y')}) | |
Runtime: {$data["Runtime (mins)"]} minutes | |
Rating: {$data["IMDb Rating"]} | |
Genre: {$data["Genres"]} | |
IMDB Link: {$data["URL"]} | |
EOT; | |
$years = 0; | |
// Advance the cursor to at least the $earlyBounds date without creating events. | |
while ($dateCursor < $earlyBounds) { | |
// This is the number of years to advance the cursor. | |
$years = $years + 25; | |
$dateCursor = $dateCursor->modify('+25 years'); | |
} | |
// Now that we're in bounds, create all day events and do so every anniversary. | |
while ($dateCursor < $lateBounds) { | |
// Handle the case when the premire occurs after earlyBounds (this will not happen as configured in this gist) | |
$summary = $years === 0 ? 'Premire' : "${years}th Anniversary"; | |
$calendar->addComponent( | |
createAllDayEvent($dateCursor, "${data['Title']} - ${summary}", $eventDescription) | |
); | |
// This is the number of years to advance the cursor. | |
$years = $years + 25; | |
$dateCursor = $dateCursor->modify('+25 years'); | |
} | |
} | |
// Technically the IETF specification says that UTF8 is ok, | |
// but google calendar imports .ics files using either ISO 8859-1 (Latin1) or Windows-1252 | |
// and some multibyte UTF8 characters get mangled in the import. | |
echo utf8_decode($calendar->render()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment