diff --git a/bot.py b/bot.py index 6674bde..35de048 100644 --- a/bot.py +++ b/bot.py @@ -7,6 +7,8 @@ import time import re import feedparser +import datetime +import dateutil.parser from colour import Colours from db import FeedDB from config import Config @@ -22,6 +24,7 @@ def __init__(self, config, db, on_connect_cb): self.date = self.__config.date self.feedname = self.__config.feedname self.shorturls = self.__config.shorturls + self.dateformat = self.__config.dateformat def on_welcome(self, connection, event): """Join the correct channel upon connecting""" @@ -179,12 +182,20 @@ def __fetch_feed(self, feed_info): else: newsurl = newsitem.link - # Try to get the published date. Otherwise set it to 'no date' + # Try to get the published or updated date. Otherwise set it to 'no date' try: - newsdate = newsitem.published + # Get date and parse it + newsdate = dateutil.parser.parse(newsitem.published) + # Format date based on 'dateformat' in config.py + newsdate = newsdate.strftime(self.__config.dateformat) + except Exception as e: try: - newsdate = newsitem.updated + # Get date and parse it + newsdate = dateutil.parser.parse(newsitem.updated) + # Format date based on 'dateformat' in config.py + newsdate = newsdate.strftime(self.__config.dateformat) + except Exception as e: newsdate = "no date" diff --git a/config.py.sample b/config.py.sample index 88342d1..50e37f3 100644 --- a/config.py.sample +++ b/config.py.sample @@ -17,3 +17,4 @@ class Config(object): self.date = '8' self.feedname = '2' self.shorturls = False + self.dateformat = '%Y-%m-%d %H:%M:%S %z' diff --git a/requirements.txt b/requirements.txt index 90c02e3..df2be02 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,6 +10,7 @@ jaraco.logging==1.3.1 jaraco.stream==1.1 jaraco.text==1.6.3 more-itertools==2.2 +python-dateutil==2.5.3 pytz==2016.4 six==1.10.0 git+https://github.com/palantir/sqlite3worker#egg=sqlite3worker