Jump to content

Widget:UPDEvents: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
Line 15: Line 15:
   var loading = container.querySelector('.upd-events-loading');
   var loading = container.querySelector('.upd-events-loading');
   var list = container.querySelector('.upd-events-list');
   var list = container.querySelector('.upd-events-list');
   var pageUrl = 'https://upd.edu.ph/events-list/';
   var pageUrl = 'https://upd.edu.ph/events-list/';


   function showError() {
   function showError() {
     loading.textContent =
     loading.textContent = 'Unable to load events automatically. ';
      'Unable to load events automatically. ';
 
     var link = document.createElement('a');
     var link = document.createElement('a');
     link.href = pageUrl;
     link.href = pageUrl;
Line 30: Line 27:
   }
   }


   function cleanTitle(text) {
   function parseEventDate(text) {
    return text
      .replace(/\s+/g, ' ')
      .replace(/\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\s+\d{1,2}\s*\|[\s\S]*$/i, '')
      .trim();
  }
 
  function getEventDate(text) {
     var match = text.match(
     var match = text.match(
       /\b(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\s+(\d{1,2})\b/i
       /\b(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\s+(\d{1,2})\s*\|\s*(\d{1,2}):(\d{2})\s*(AM|PM)/i
     );
     );


Line 49: Line 39:
     };
     };


     var month = months[match[1].slice(0,3).toLowerCase()];
    var now = new Date();
     var month = months[match[1].slice(0, 3).toLowerCase()];
     var day = Number(match[2]);
     var day = Number(match[2]);
     var now = new Date();
     var hour = Number(match[3]) % 12;
    var minute = Number(match[4]);
 
    if (match[5].toUpperCase() === 'PM') hour += 12;
 
     var year = now.getFullYear();
     var year = now.getFullYear();
    var eventDate = new Date(year, month, day, hour, minute);


    var date = new Date(year, month, day, 23, 59, 59);
     // Handle year boundaries.
 
     if (month === 0 && now.getMonth() === 11) {
     // Handle events listed around the New Year.
       eventDate.setFullYear(year + 1);
     if (date < now && now.getMonth() === 11 && month === 0) {
     } else if (month === 11 && now.getMonth() === 0) {
       date.setFullYear(year + 1);
       eventDate.setFullYear(year - 1);
     } else if (date < now && now.getMonth() === 0 && month === 11) {
       return null;
     }
     }


     return date;
     return eventDate;
   }
   }


   fetch(pageUrl)
   fetch(pageUrl)
     .then(function (response) {
     .then(function (response) {
       if (!response.ok) throw new Error('Could not retrieve event page');
       if (!response.ok) throw new Error('Unable to retrieve events page');
       return response.text();
       return response.text();
     })
     })
     .then(function (html) {
     .then(function (html) {
       var doc = new DOMParser().parseFromString(html, 'text/html');
       var doc = new DOMParser().parseFromString(html, 'text/html');
       var anchors = doc.querySelectorAll('a[href]');
       var anchors = doc.querySelectorAll(
        '#em-events-list-grouped-1 a[href*="/events/"]'
      );
 
       var events = [];
       var events = [];
       var seen = {};
       var seen = {};


       anchors.forEach(function (anchor) {
       anchors.forEach(function (anchor) {
         var url = new URL(anchor.getAttribute('href'), pageUrl);
         var heading = anchor.querySelector('h4.d-teal.fw-700');
        if (url.hostname !== 'upd.edu.ph') return;
         var dateText = anchor.querySelector('li.list-inline-item');
        if (!/^\/events\/[^/]+\/?$/.test(url.pathname)) return;
 
         var rawText = (anchor.textContent || '')
          .replace(/\s+/g, ' ')
          .trim();


         var date = getEventDate(rawText);
         if (!heading || !dateText) return;
        var title = cleanTitle(rawText);


         if (!date || date < new Date() || !title) return;
         var title = heading.textContent.trim();
        var date = parseEventDate(dateText.textContent);
        var url = new URL(anchor.getAttribute('href'), pageUrl).href;


         var key = url.href;
         if (!title || !date || date <= new Date()) return;
         if (seen[key]) return;
         if (seen[url]) return;
        seen[key] = true;


        seen[url] = true;
         events.push({
         events.push({
           title: title,
           title: title,
           url: url.href,
           url: url,
           date: date
           date: date
         });
         });
Line 109: Line 102:


       if (!events.length) {
       if (!events.length) {
         loading.textContent =
         loading.textContent = 'No upcoming events were found.';
          'No upcoming events were found.';
         return;
         return;
       }
       }

Revision as of 02:09, 29 October 2026