From 098932440c5bcd1492dabc7d2026b4e878b9e1fd Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 10 Oct 2020 13:24:14 +0100 Subject: [PATCH] Same date format as rss --- newswire.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/newswire.py b/newswire.py index 5e80e255..70191282 100644 --- a/newswire.py +++ b/newswire.py @@ -101,11 +101,69 @@ def xml2StrToDict(xmlStr: str, moderated: bool) -> {}: return result +def atomFeedToDict(xmlStr: str, moderated: bool) -> {}: + """Converts an atom feed string to a dictionary + """ + if '' not in xmlStr: + return {} + result = {} + rssItems = xmlStr.split('') + for rssItem in rssItems: + if '' not in rssItem: + continue + if '' not in rssItem: + continue + if '' not in rssItem: + continue + if '' not in rssItem: + continue + if '' not in rssItem: + continue + if '' not in rssItem: + continue + title = rssItem.split('')[1] + title = title.split('')[0] + description = '' + if '' in rssItem and '' in rssItem: + description = rssItem.split('')[1] + description = description.split('')[0] + link = rssItem.split('')[1] + link = link.split('')[0] + pubDate = rssItem.split('')[1] + pubDate = pubDate.split('')[0] + parsed = False + try: + publishedDate = \ + datetime.strptime(pubDate, "%Y-%m-%dT%H:%M:%SZ") + publishedDate = publishedDate.replace('Z', '') + publishedDate = publishedDate.replace('T', ' ') + '+00:00' + postFilename = '' + votesStatus = [] + result[str(publishedDate)] = [title, link, + votesStatus, postFilename, + description, moderated] + parsed = True + except BaseException: + pass + if not parsed: + try: + publishedDate = \ + datetime.strptime(pubDate, "%a, %d %b %Y %H:%M:%S UT") + result[str(publishedDate) + '+00:00'] = [title, link] + parsed = True + except BaseException: + print('WARN: unrecognized atom feed date format: ' + pubDate) + pass + return result + + def xmlStrToDict(xmlStr: str, moderated: bool) -> {}: """Converts an xml string to a dictionary """ if 'rss version="2.0"' in xmlStr: return xml2StrToDict(xmlStr, moderated) + elif 'xmlns="http://www.w3.org/2005/Atom"' in xmlStr: + return atomFeedToDict(xmlStr, moderated) return {}