diff --git a/blog.py b/blog.py index a4e4ed50..067487a8 100644 --- a/blog.py +++ b/blog.py @@ -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 += '
' + blogStr += '' - blogStr += '' - blogStr += '
' + blogStr += '' + + blogStr += '' + blogStr += '' + + blogStr += '' 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 diff --git a/daemon.py b/daemon.py index 4b6afc1f..4cc452c2 100644 --- a/daemon.py +++ b/daemon.py @@ -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 diff --git a/img/icons/rss3.png b/img/icons/rss3.png new file mode 100644 index 00000000..83521cd1 Binary files /dev/null and b/img/icons/rss3.png differ