Skip to content

Instantly share code, notes, and snippets.

@PatPeter
Last active April 13, 2017 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatPeter/23140fc7c1b0df8d1ff95e55440b6151 to your computer and use it in GitHub Desktop.
Save PatPeter/23140fc7c1b0df8d1ff95e55440b6151 to your computer and use it in GitHub Desktop.
Divides channel.html/txt and server.html/txt files into folders by year and files by month
<?php
$directories = array('M1dQMnhKRVE3RHBjd3luMTdCcG8xOVBpMUZ3PQ==_2012-03-18', 'M1dQMnhKRVE3RHBjd3luMTdCcG8xOVBpMUZ3PQ==_2012-11-05/M1dQMnhKRVE3RHBjd3luMTdCcG8xOVBpMUZ3PQ==', 'M1dQMnhKRVE3RHBjd3luMTdCcG8xOVBpMUZ3PQ==_2013-11-10/M1dQMnhKRVE3RHBjd3luMTdCcG8xOVBpMUZ3PQ==', 'M1dQMnhKRVE3RHBjd3luMTdCcG8xOVBpMUZ3PQ==');
$filenames = array('channel', 'server');
$extensions = array('.html', '.txt');
foreach ($directories as $directory) {
foreach ($filenames as $filename) {
foreach ($extensions as $extension) {
$sourceFile = fopen($directory . '/' . $filename . $extension, 'r');
if ($sourceFile === false) {
exit("Could not open " . $directory . '/' . $filename . $extension . "\n");
}
$destinationFile = null;
$oldDate = null;
$currentDate = null;
$currentYear = null;
while (($line = fgets($sourceFile)) !== false) {
$chatBegins = strpos($line, ($filename == 'channel' ? '*** Chat begins ' : '*** Log begins'));
if ($chatBegins !== false) {
//error_log('Chat begins ' . $chatBegins);
$chatEnds = strpos($line, '<', $chatBegins);
//error_log('Chat ends ' . $chatEnds);
if ($chatEnds === false) {
$chatEnds = strlen($line);
}
// If the chat has begun again, close last month's log file and prepare to open a new one
if ($destinationFile != null) {
fclose($destinationFile);
$destinationFile = null;
}
//$dateHeader = str_replace('*** Chat begins ', '', $line);
$prefixLength = $filename == 'channel' ? 16 : 14;
$dateHeader = substr($line, $chatBegins + $prefixLength, ($chatEnds - $chatBegins - $prefixLength));
//error_log('Parsing date: ' . trim($dateHeader));
$dateErrors = array();
/**
* n - Numeric representation of a month without leading zeros
* j - Day of the month, 2 digits without leading zeros
* Y - A full numeric representation of a year, 4 digits
* g - 12-hour format of an hour without leading zero
* i - Minutes with leading zeros
* s - Seconds, with leading zeros
* A - Ante meridiem and Post meridiem, capitalized
*/
/**
* y - A two digit representation of a year (which is assumed to be in the range 1970-2069, inclusive
* m - Numeric representation of a month, with leading zeros
* d - Day of the month, 2 digits with leading zeros
* g - 12-hour format of an hour without leading zero
* i - Minutes with leading zeros
* s - Seconds, with leading zeros
* A - Ante meridiem and Post meridiem, capitalized
*/
$oldDate = $currentDate;
$currentDate = null;
$formats = array('n/j/Y g:i:s A', 'y/m/d g:i:s A', 'Y/m/d g:i:s A', 'Y-m-d g:i:s A', 'Y-m-d G:i:s');
for ($i = 0; $i < count($formats); $i++) {
if ($currentDate != null) {
break;
}
//$currentDate = strtotime($dateHeader);
$currentDate = \DateTime::createFromFormat($formats[$i], trim($dateHeader));
if ($currentDate === false) {
$dateErrors[] = \DateTime::getLastErrors();
//error_log('Could not parse with format ' . $formats[$i]);
$currentDate = null;
continue;
}
if ($currentDate > (new \DateTime())->setTimestamp(time()) || $currentDate < new \DateTime('2010-01-01 00:00:00')) {
//error_log('Parsed invalid date ' . $currentDate->format('c') . ' from format ' . $formats[$i]);
$currentDate = null;
}
}
if ($currentDate == null) {
var_dump($dateErrors);
throw new Exception('Could not parse any dates.');
}
if ($currentYear == null) {
$currentYear = $currentDate->format('Y');
} else if ((int) $currentDate->format('Y') > $currentYear) {
$currentYear = $currentDate->format('Y');
}
//else if ((int) $currentDate->format('Y') < $currentYear) {
// throw new Exception('Going backwards in time. Hit ' . $currentDate->format('Y') . ' logs in ' . $currentYear . '.');
//}
if (!file_exists($currentYear)) {
mkdir($currentYear);
}
$destinationFileExists = false;
if (file_exists($filename . '_' . $currentDate->format('Y-m') . $extension)) {
$destinationFileExists = true;
}
$destinationFile = fopen($currentYear . '/' . $filename . '_' . $currentDate->format('Y-m') . $extension, 'a');
if (!$destinationFileExists && $extension == '.html') {
fwrite($destinationFile, '<head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><br>'."\n");
}
if ($oldDate != null && $oldDate->format('Y-m') != $currentDate->format('Y-m')) {
error_log($filename . $extension . " is up to " . $currentDate->format('Y-m'));
}
}
if ($destinationFile != null) {
fwrite($destinationFile, $line);
}
}
}
}
fclose($sourceFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment