Support rss version 3.0 blog feeds

main
Bob Mottram 2020-05-23 09:41:50 +00:00
parent e454377a07
commit 19a64c7f93
3 changed files with 114 additions and 4 deletions

84
blog.py
View File

@ -295,6 +295,29 @@ def htmlBlogPostRSS2(authorized: bool,
return rssStr
def htmlBlogPostRSS3(authorized: bool,
baseDir: str, httpPrefix: str, translate: {},
nickname: str, domain: str, domainFull: str,
postJsonObject: {},
handle: str, restrictToDomain: bool) -> str:
"""Returns the RSS version 3 feed for a single blog post
"""
messageLink = ''
if postJsonObject['object'].get('id'):
messageLink = postJsonObject['object']['id'].replace('/statuses/', '/')
if not restrictToDomain or \
(restrictToDomain and '/' + domain in messageLink):
if postJsonObject['object'].get('summary'):
published = postJsonObject['object']['published']
pubDate = datetime.strptime(published, "%Y-%m-%dT%H:%M:%SZ")
titleStr = postJsonObject['object']['summary']
rssDateStr = pubDate.strftime("%a, %d %b %Y %H:%M:%S UT")
rssStr = 'title: ' + titleStr + '\n'
rssStr += 'link: ' + messageLink + '\n'
rssStr += 'created: ' + rssDateStr + '\n\n'
return rssStr
def htmlBlogPost(authorized: bool,
baseDir: str, httpPrefix: str, translate: {},
nickname: str, domain: str, domainFull: str,
@ -317,14 +340,21 @@ def htmlBlogPost(authorized: bool,
domainFull, postJsonObject,
None, False)
# show rss link
# show rss links
iconsDir = getIconsDir(baseDir)
blogStr += '<p class="rssfeed">'
blogStr += '<a href="' + httpPrefix + '://' + \
domainFull + '/blog/' + nickname + '/rss.xml">'
blogStr += '<img loading="lazy" alt="RSS" title="RSS" src="/' + \
iconsDir + '/rss.png" />'
blogStr += '</a></p>'
blogStr += '<img loading="lazy" alt="RSS 2.0" title="RSS 2.0" src="/' + \
iconsDir + '/rss.png" /></a>'
blogStr += '<a href="' + httpPrefix + '://' + \
domainFull + '/blog/' + nickname + '/rss.txt">'
blogStr += '<img loading="lazy" alt="RSS 3.0" title="RSS 3.0" src="/' + \
iconsDir + '/rss3.png" /></a>'
blogStr += '</p>'
return blogStr + htmlFooter()
return None
@ -479,6 +509,52 @@ def htmlBlogPageRSS2(authorized: bool, session,
return blogRSS2 + rss2Footer()
def htmlBlogPageRSS3(authorized: bool, session,
baseDir: str, httpPrefix: str, translate: {},
nickname: str, domain: str, port: int,
noOfItems: int, pageNumber: int) -> str:
"""Returns an RSS version 3 feed containing posts
"""
if ' ' in nickname or '@' in nickname or \
'\n' in nickname or '\r' in nickname:
return None
domainFull = domain
if port:
if port != 80 and port != 443:
domainFull = domain + ':' + str(port)
blogRSS3 = ''
blogsIndex = baseDir + '/accounts/' + \
nickname + '@' + domain + '/tlblogs.index'
if not os.path.isfile(blogsIndex):
return blogRSS3
timelineJson = createBlogsTimeline(session, baseDir,
nickname, domain, port,
httpPrefix,
noOfItems, False, False,
pageNumber)
if not timelineJson:
return blogRSS3
if pageNumber is not None:
for item in timelineJson['orderedItems']:
if item['type'] != 'Create':
continue
blogRSS3 += \
htmlBlogPostRSS3(authorized, baseDir,
httpPrefix, translate,
nickname, domain,
domainFull, item,
None, True)
return blogRSS3
def getBlogIndexesForAccounts(baseDir: str) -> {}:
""" Get the index files for blogs for each account
and add them to a dict

View File

@ -95,6 +95,7 @@ from config import getConfigParam
from roles import setRole
from roles import clearModeratorStatus
from blog import htmlBlogPageRSS2
from blog import htmlBlogPageRSS3
from blog import htmlBlogView
from blog import htmlBlogPage
from blog import htmlBlogPost
@ -1128,6 +1129,7 @@ class PubServer(BaseHTTPRequestHandler):
self._benchmarkGETtimings(GETstartTime, GETtimings, 8)
# RSS 2.0
if self.path.startswith('/blog/') and \
self.path.endswith('/rss.xml'):
nickname = self.path.split('/blog/')[1]
@ -1159,6 +1161,38 @@ class PubServer(BaseHTTPRequestHandler):
self._404()
return
# RSS 3.0
if self.path.startswith('/blog/') and \
self.path.endswith('/rss.txt'):
nickname = self.path.split('/blog/')[1]
if '/' in nickname:
nickname = nickname.split('/')[0]
if not nickname.startswith('rss.'):
if os.path.isdir(self.server.baseDir +
'/accounts/' + nickname +
'@' + self.server.domain):
if not self.server.session:
self.server.session = \
createSession(self.server.useTor)
msg = \
htmlBlogPageRSS3(authorized,
self.server.session,
self.server.baseDir,
self.server.httpPrefix,
self.server.translate,
nickname,
self.server.domain,
self.server.port,
maxPostsInRSSFeed, 1)
if msg is not None:
msg = msg.encode()
self._set_headers('text/plain; charset=utf-8',
len(msg), cookie, callingDomain)
self._write(msg)
return
self._404()
return
# show the main blog page
if htmlGET and (self.path == '/blog' or
self.path == '/blog/' or

BIN
img/icons/rss3.png 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB