Skip to content

Commit 5e38234

Browse files
authored
fix: processing month names from JSON for RSS
1 parent 542cd0c commit 5e38234

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

convert_json.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/python3
22

3+
import calendar
34
from datetime import datetime
45
import json
56
import xml.etree.ElementTree as ET
@@ -41,7 +42,15 @@ def rss_item(title: str | None = None,
4142
# https://validator.w3.org/feed/docs/error/InvalidRFC2822Date.html
4243
if pubDate is not None:
4344
item.append(ET.Element("pubDate"))
44-
fmt_date = datetime.strptime(pubDate, "%B %d, %Y").strftime("%d %b %Y")
45+
46+
# Make sure months are processed correctly when there's some inconsistency
47+
# https://docs.python.org/3/library/datetime.html#format-codes
48+
if pubDate in calendar.month_name:
49+
fmt_date = datetime.strptime(pubDate, "%B %d, %Y").strftime("%d %b %Y")
50+
elif pubDate in calendar.month_abbr:
51+
fmt_date = datetime.strptime(pubDate, "%b %d, %Y").strftime("%d %b %Y")
52+
else:
53+
fmt_date = '01 January 1970' # Just default to UNIX start
4554
item[-1].text = fmt_date
4655

4756
return item

0 commit comments

Comments
 (0)