From bd1dd8e6282ddbbf974a70888b64d30b9933012e Mon Sep 17 00:00:00 2001 From: HB9HIL Date: Thu, 9 May 2024 17:32:41 +0200 Subject: [PATCH 1/3] rss parser --- application/controllers/Contestcalendar.php | 100 ++++++++++++++++++-- application/views/contestcalendar/index.php | 44 ++++----- 2 files changed, 113 insertions(+), 31 deletions(-) diff --git a/application/controllers/Contestcalendar.php b/application/controllers/Contestcalendar.php index e61a67f45..d0afc7cf4 100644 --- a/application/controllers/Contestcalendar.php +++ b/application/controllers/Contestcalendar.php @@ -8,13 +8,8 @@ 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)); - } - $data['rss'] = simplexml_load_string($rssRawData, null, LIBXML_NOCDATA); + $rssRawData = $this->getRssData(); + $data['rss'] = $this->parseRSS($rssRawData); $footerData['scripts'] = [ 'assets/js/sections/dxcalendar.js?' . filemtime(realpath(__DIR__ . "/../../assets/js/sections/dxcalendar.js")) @@ -26,5 +21,96 @@ 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']->format('Y-m-d H:i:s'); + $contest['end'] = $timeRange['end']->format('Y-m-d H:i:s'); + + // 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; + } + } diff --git a/application/views/contestcalendar/index.php b/application/views/contestcalendar/index.php index 15aa7df8a..b06113df2 100644 --- a/application/views/contestcalendar/index.php +++ b/application/views/contestcalendar/index.php @@ -3,30 +3,26 @@

This data is from https://www.contestcalendar.com/

- - - - - - - - - +
ContestDateLink
+ + + + + + + + + channel->item as $item) { - echo ''; - $title = (string) $item->title; - $link = (string) $item->link; - $description = (string) $item->description; - - echo ""; - echo ""; - echo ""; - - echo ''; - } - ?> + foreach ($rss as $contest) { + echo ''; + echo ""; + echo ""; + echo ""; + echo ""; + echo ''; + } + ?>
ContestStartEndLink
$title$description$link
" . $contest['title'] . "" . $contest['start'] . "" . $contest['end'] . "" . $contest['link'] . "
- - + \ No newline at end of file From ab8a08b246e0218bf2111bf0265d6e31c4d8e63c Mon Sep 17 00:00:00 2001 From: HB9HIL Date: Thu, 9 May 2024 19:54:17 +0200 Subject: [PATCH 2/3] new layout --- application/controllers/Contestcalendar.php | 71 ++++++++++++++++++++- application/views/contestcalendar/index.php | 67 +++++++++++++++---- 2 files changed, 123 insertions(+), 15 deletions(-) diff --git a/application/controllers/Contestcalendar.php b/application/controllers/Contestcalendar.php index d0afc7cf4..e84efe770 100644 --- a/application/controllers/Contestcalendar.php +++ b/application/controllers/Contestcalendar.php @@ -8,8 +8,21 @@ class Contestcalendar extends CI_Controller { $data['page_title'] = "Contest Calendar"; + // get the raw data and parse it $rssRawData = $this->getRssData(); - $data['rss'] = $this->parseRSS($rssRawData); + $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'); + } $footerData['scripts'] = [ 'assets/js/sections/dxcalendar.js?' . filemtime(realpath(__DIR__ . "/../../assets/js/sections/dxcalendar.js")) @@ -37,8 +50,8 @@ class Contestcalendar extends CI_Controller { // 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']->format('Y-m-d H:i:s'); - $contest['end'] = $timeRange['end']->format('Y-m-d H:i:s'); + $contest['start'] = $timeRange['start']; + $contest['end'] = $timeRange['end']; // and write the link to the array $contest['link'] = (string) $item->link; @@ -112,5 +125,57 @@ class Contestcalendar extends CI_Controller { 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; + } } diff --git a/application/views/contestcalendar/index.php b/application/views/contestcalendar/index.php index b06113df2..17a8a21c8 100644 --- a/application/views/contestcalendar/index.php +++ b/application/views/contestcalendar/index.php @@ -1,28 +1,71 @@

-

+

This data is from https://www.contestcalendar.com/

- + '; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ''; + } + } + ?> + +
+
+

Today

+
{$contest['title']}{$contest['start']->format($custom_date_format)}{$contest['start']->format('H:i')}{$contest['end']->format($custom_date_format.' H:i')}Show Details
+ + + + + + + + + + + + +
ContestDateStartEndLink
+
+
+

Weekend

+ + + + + + + + + + + + + +
ContestDateStartEndLink
+
+ +

Next Week

+ + - '; - echo ""; - echo ""; - echo ""; - echo ""; - echo ''; - } - ?> +
ContestDate Start End Link
" . $contest['title'] . "" . $contest['start'] . "" . $contest['end'] . "" . $contest['link'] . "
\ No newline at end of file From 0b0eccb83530f5d6a1386f1fae1b95fda54b12ba Mon Sep 17 00:00:00 2001 From: HB9HIL Date: Thu, 9 May 2024 20:20:54 +0200 Subject: [PATCH 3/3] code reduction --- application/views/contestcalendar/index.php | 103 +++++++++----------- 1 file changed, 47 insertions(+), 56 deletions(-) diff --git a/application/views/contestcalendar/index.php b/application/views/contestcalendar/index.php index 17a8a21c8..15eb8ec33 100644 --- a/application/views/contestcalendar/index.php +++ b/application/views/contestcalendar/index.php @@ -4,68 +4,59 @@

This data is from https://www.contestcalendar.com/

'; - echo "{$contest['title']}"; - echo "{$contest['start']->format($custom_date_format)}"; - echo "{$contest['start']->format('H:i')}"; - echo "{$contest['end']->format($custom_date_format.' H:i')}"; - echo "Show Details"; - echo ''; - } - } - ?> - -
-
-

Today

- - + function generateTableRows($contests, $custom_date_format) { ?> +
+ + + + + + + + + + - - - - - + + + + - - + + +
ContestStartEndLink
ContestDateStartEndLinkformat('d M - H:i'); ?>format('d M - H:i'); ?>' target='_blank'>Show Details
+ + +
+
+
+
+

Today

+
+
- - +
+
-

Weekend

- - - - - - - - - - - +
+
+

Weekend

+
+
-
-
ContestDateStartEndLink
+
+
+
+
+
+
+
+

Next Week

+
+
+ +
-

Next Week

- - - - - - - - - - - - - -
ContestDateStartEndLink
\ No newline at end of file