Skip to content

Instantly share code, notes, and snippets.

@apie
Last active November 15, 2018 22:19
Show Gist options
  • Save apie/16d795f468a0df7ed6e8648c1724b8a5 to your computer and use it in GitHub Desktop.
Save apie/16d795f468a0df7ed6e8648c1724b8a5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Format items in an RSS feed for use in an IRC channel topic. E.g:
# ma 18jun Hackatrain| ma 18jun UT Educational Award Ceremony| wo 20jun Lunchlezing Ricardo Rail
#
# Licence: MIT
import re
import datetime
from rss_parse import RSSParser
from dateutil.parser import parse
RSS_URL = 'https://www.scintilla.utwente.nl/nl/activity/calendar.rss'
xpath_configuration = {
'xpathParse': {
'stripHTML': True,
'item': '/rss/channel/item',
'namespace': {'re': 'http://exslt.org/regular-expressions'},
'title': './/title/text()',
'url': './/link/text()',
'body': './/description/text()',
'date': './/pubDate/text()',
'image': './/description/text()',
}
}
def dutch_month_to_english_abbr(dutch_month):
dutch_month = dutch_month.lower()
if dutch_month == 'maart':
return 'mar'
if dutch_month == 'mei':
return 'may'
if dutch_month == 'oktober':
return 'oct'
return dutch_month[:3]
def get_date(item):
date_pat = r'(\d{2}) (\w+) (\d{4}) (\d{2}:\d{2})'
# Split date string from body
# Date str = dd dutchmonth yyyy hh:mm
day, month, year, time = re.findall(date_pat, item.body[:50])[0]
return parse(' '.join([day, dutch_month_to_english_abbr(month), year, time]))
# Format title in the following way: 'ma 18jun UT Educational award'
def format_item(title, body):
full_date_pat = r'(\w+) (\d{2}) (\w+) (\d{4}) (\d{2}:\d{2})'
# Date str = dutchweekday dd dutchmonth yyyy hh:mm
weekday, day, month, year, time = re.findall(full_date_pat, body[:50])[0]
return '%s %s%s %s' % (weekday[:2], day, month[:3], title)
parsed_feed = RSSParser(RSS_URL, xpath_configuration)
# Sort items by event date
# Format the title
# Print all the titles, joined by bars
print('| '.join([format_item(i.title, i.body[:50]) for i in sorted(parsed_feed, key=get_date) if get_date(i) >= datetime.datetime.now()]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment