Merge branch 'dev' into cron_expressions

This commit is contained in:
HB9HIL
2024-05-09 20:29:25 +02:00
committed by GitHub
2 changed files with 207 additions and 26 deletions

View File

@@ -8,13 +8,21 @@ class Contestcalendar extends CI_Controller {
$data['page_title'] = "Contest Calendar";
$this->load->driver('cache', array('adapter' => 'file', 'backup' => 'file'));
$rssUrl = 'https://www.contestcalendar.com/calendar.rss';
if (!$rssRawData = $this->cache->get('RssRawContestCal')) {
$rssRawData = file_get_contents($rssUrl, true);
$this->cache->save('RssRawContestCal', $rssRawData, (60*60*12));
// get the raw data and parse it
$rssRawData = $this->getRssData();
$parsed = $this->parseRSS($rssRawData);
// and give it to the view
$data['contestsToday'] = $this->contestsToday($parsed);
$data['contestsNextWeekend'] = $this->contestsNextWeekend($parsed);
$data['contestsNextWeek'] = $this->contestsNextWeek($parsed);
// Get Date format
if($this->session->userdata('user_date_format')) {
$data['custom_date_format'] = $this->session->userdata('user_date_format');
} else {
$data['custom_date_format'] = $this->config->item('qso_date_format');
}
$data['rss'] = simplexml_load_string($rssRawData, null, LIBXML_NOCDATA);
$footerData['scripts'] = [
'assets/js/sections/dxcalendar.js?' . filemtime(realpath(__DIR__ . "/../../assets/js/sections/dxcalendar.js"))
@@ -26,5 +34,148 @@ class Contestcalendar extends CI_Controller {
}
private function parseRSS($rssRawData) {
$rssData = array();
$raw = simplexml_load_string($rssRawData, null, LIBXML_NOCDATA);
foreach ($raw->channel->item as $item) {
// create an array to hold the contest information
$contest = array();
// write the contest_title to the array
$contest['title'] = (string) $item->title;
// write the start time to the array. the whole time range is in the 'description' tag of the rssRawData
$description = (string) $item->description;
$timeRange = $this->parseTimeRange($description);
$contest['start'] = $timeRange['start'];
$contest['end'] = $timeRange['end'];
// and write the link to the array
$contest['link'] = (string) $item->link;
// append the contest array to the $rssData array
$rssData[] = $contest;
}
return $rssData;
}
private function parseTimeRange($string) {
$timeData = array();
// if the timeRange is over midnight the string contains 'to'
if (strpos($string, 'to')) {
// split in start and end time
$parts = explode('to', $string);
$start = trim($parts[0]);
$end = trim($parts[1]);
// create proper dateTime
$timeData['start'] = DateTime::createFromFormat('Hi\Z, M d', $start);
$timeData['end'] = DateTime::createFromFormat('Hi\Z, M d', $end);
} else {
// split in start and end time
$parts = explode('-', $string);
$start = trim($parts[0]);
$end = trim($parts[1]);
// extract the date. we need to add this to the start time
$date = substr($parts[1], strpos($parts[1], ',') + 2);
// create proper dateTime
$timeData['start'] = DateTime::createFromFormat('Hi\Z, M d', $start . ', ' . $date);
$timeData['end'] = DateTime::createFromFormat('Hi\Z, M d', $end);
}
return $timeData;
}
private function getRssData() {
$this->load->driver('cache', array('adapter' => 'file', 'backup' => 'file'));
if (!$rssRawData = $this->cache->get('RssRawContestCal')) {
$rssUrl = 'https://www.contestcalendar.com/calendar.rss';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $rssUrl);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Wavelog Updater');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rssRawData = curl_exec($ch);
curl_close($ch);
if ($rssRawData === FALSE) {
$msg = "Something went wrong with fetching the Contest Data";
log_message('error', $msg);
return;
}
$this->cache->save('RssRawContestCal', $rssRawData, (60*60*12)); // 12 hours cache time
curl_close($ch);
}
return $rssRawData;
}
private function contestsToday($rss) {
$contestsToday = array();
$today = date('Y-m-d');
foreach ($rss as $contest) {
$start = date('Y-m-d', strtotime($contest['start']->format('Y-m-d')));
if ($start === $today) {
$contestsToday[] = $contest;
}
}
return $contestsToday;
}
private function contestsNextWeekend($rss) {
$contestsNextWeekend = array();
$today = date('Y-m-d');
$nextFriday = date('Y-m-d', strtotime('next friday', strtotime($today)));
$nextSunday = date('Y-m-d', strtotime('next sunday', strtotime($today)));
foreach ($rss as $contest) {
$start = date('Y-m-d', strtotime($contest['start']->format('Y-m-d')));
if ($start >= $nextFriday && $start <= $nextSunday) {
$contestsNextWeekend[] = $contest;
}
}
return $contestsNextWeekend;
}
private function contestsNextWeek($rss) {
$contestsNextWeek = array();
$today = date('Y-m-d');
$nextMonday = date('Y-m-d', strtotime('next monday', strtotime($today)));
foreach ($rss as $contest) {
$start = date('Y-m-d', strtotime($contest['start']->format('Y-m-d')));
if ($start >= $nextMonday) {
$contestsNextWeek[] = $contest;
}
}
return $contestsNextWeek;
}
}

View File

@@ -1,32 +1,62 @@
<div class="container">
<br>
<h2><?php echo $page_title; ?></h2>
<h2><?= $page_title; ?></h2>
<p>This data is from <a target="_blank" href="https://www.contestcalendar.com/">https://www.contestcalendar.com/</a></p>
<table style="width:100%" class="table-sm table table-bordered table-hover table-striped table-condensed dxcalendar">
<?php
function generateTableRows($contests, $custom_date_format) { ?>
<table style="width:100%" class="table-sm table table-bordered table-hover table-striped table-condensed">
<thead>
<tr>
<th>Contest</th>
<th>Date</th>
<th>Start</th>
<th>End</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<?php
foreach($rss->channel->item as $item) {
echo '<tr>';
$title = (string) $item->title;
$link = (string) $item->link;
$description = (string) $item->description;
<?php foreach ($contests as $contest) { ?>
<tr>
<td><b><?php echo $contest['title']; ?></b></td>
<td><?php echo $contest['start']->format('d M - H:i'); ?></td>
<td><?php echo $contest['end']->format('d M - H:i'); ?></td>
<td><a class='btn btn-secondary btn-sm' href='<?php echo $contest['link']; ?>' target='_blank'>Show Details</a></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
echo "<td>$title</td>";
echo "<td>$description</td>";
echo "<td><a href='$link' target=_blank>$link</a></td>";
echo '</tr>';
}
?>
</tbody>
</table>
</div>
<div class="row mb-3">
<div class="col">
<div class="card">
<div class="card-header">
<h4>Today</h4>
</div>
<div class="card-body">
<?php generateTableRows($contestsToday, $custom_date_format); ?>
</div>
</div>
</div>
<div class="col">
<div class="card">
<div class="card-header">
<h4>Weekend</h4>
</div>
<div class="card-body">
<?php generateTableRows($contestsNextWeekend, $custom_date_format); ?>
</div>
</div>
</div>
</div>
<div class="border-1">
<div class="card">
<div class="card-header">
<h4>Next Week</h4>
</div>
<div class="card-body">
<?php generateTableRows($contestsNextWeek, $custom_date_format); ?>
</div>
</div>
</div>
</div>