diff --git a/newsdaemon.py b/newsdaemon.py index ed8f3eb5..74e3e1a4 100644 --- a/newsdaemon.py +++ b/newsdaemon.py @@ -9,22 +9,24 @@ __status__ = "Production" import os import time from newswire import getDictFromNewswire -from posts import createNewsPost +from posts import createBlogPost from utils import saveJson +from utils import getStatusNumber -def updateFeedsIndex(baseDir: str, filename: str) -> None: +def updateFeedsIndex(baseDir: str, domain: str, postId: str) -> None: """Updates the index used for imported RSS feeds """ - indexFilename = baseDir + '/accounts/feeds.index' + basePath = baseDir + '/accounts/news@' + domain + indexFilename = basePath + '/outbox.index' if os.path.isfile(indexFilename): - if filename not in open(indexFilename).read(): + if postId not in open(indexFilename).read(): try: with open(indexFilename, 'r+') as feedsFile: content = feedsFile.read() feedsFile.seek(0, 0) - feedsFile.write(filename + '\n' + content) + feedsFile.write(postId + '\n' + content) print('DEBUG: feeds post added to index') except Exception as e: print('WARN: Failed to write entry to feeds posts index ' + @@ -32,7 +34,7 @@ def updateFeedsIndex(baseDir: str, filename: str) -> None: else: feedsFile = open(indexFilename, 'w+') if feedsFile: - feedsFile.write(filename + '\n') + feedsFile.write(postId + '\n') feedsFile.close() @@ -42,25 +44,28 @@ def convertRSStoActivityPub(baseDir: str, httpPrefix: str, translate: {}) -> None: """Converts rss items in a newswire into posts """ - basePath = baseDir + '/accounts/feeds' + basePath = baseDir + '/accounts/news@' + domain + '/outbox' if not os.path.isdir(basePath): os.mkdir(basePath) - nickname = 'feeds' - for dateStr, item in newswire.items(): # convert the date to the format used by ActivityPub dateStr = dateStr.replace(' ', 'T') dateStr = dateStr.replace('+00:00', 'Z') + statusNumber, published = getStatusNumber(dateStr) + newPostId = \ + httpPrefix + '://' + domain + \ + '/users/news/statuses/' + statusNumber + # file where the post is stored - filename = basePath + '/' + dateStr + '.json' + filename = basePath + '/' + newPostId.replace('/', '#') + '.json' if os.path.isfile(filename): # if a local post exists as html then change the link # to the local one - htmlFilename = basePath + '/' + dateStr + '.html' + htmlFilename = filename.replace('.json', '.html') if os.path.isfile(htmlFilename): - item[1] = '/feeds/' + dateStr + '.html' + item[1] = '/users/news/statuses/' + statusNumber + '.html' # don't create the post if it already exists continue @@ -79,16 +84,37 @@ def convertRSStoActivityPub(baseDir: str, httpPrefix: str, else: rssDescription = url - # create the activitypub post - blog = createNewsPost(baseDir, - nickname, domain, port, - httpPrefix, dateStr, - rssTitle, rssDescription, - None, None, None, False) + followersOnly = False + useBlurhash = False + blog = createBlogPost(baseDir, + 'news', domain, port, httpPrefix, + rssDescription, followersOnly, False, + False, + None, None, None, useBlurhash, + None, None, rssTitle, + False, + None, None, None) + if not blog: + continue + + idStr = \ + httpPrefix + '://' + domain + '/users/news' + \ + '/statuses/' + statusNumber + '/replies' + blog['object']['replies']['id'] = idStr + blog['object']['replies']['first']['partOf'] = idStr + + blog['id'] = newPostId + '/activity' + blog['object']['id'] = newPostId + blog['object']['atomUri'] = newPostId + blog['object']['url'] = \ + httpPrefix + '://' + domain + '/@news/' + statusNumber + blog['object']['published'] = dateStr + + postId = newPostId.replace('/', '#') # save the post and update the index if saveJson(blog, filename): - updateFeedsIndex(baseDir, filename) + updateFeedsIndex(baseDir, domain, postId + '.json') def runNewswireDaemon(baseDir: str, httpd, diff --git a/posts.py b/posts.py index 991fd3c8..739f565c 100644 --- a/posts.py +++ b/posts.py @@ -1193,42 +1193,6 @@ def createBlogPost(baseDir: str, return blog -def createNewsPost(baseDir: str, - nickname: str, domain: str, port: int, httpPrefix: str, - published: str, rssTitle: str, rssDescription: str, - attachImageFilename: str, mediaType: str, - imageDescription: str, useBlurhash: bool) -> {}: - """Converts title and description from an rss feed into a post - """ - inReplyTo = None - inReplyToAtomUri = None - schedulePost = False - eventDate = None - eventTime = None - location = None - schedulePost = False - eventDate = None - eventTime = None - location = None - clientToServer = False - saveToFile = False - followersOnly = False - - blog = \ - createPublicPost(baseDir, - nickname, domain, port, httpPrefix, - rssDescription, followersOnly, saveToFile, - clientToServer, - attachImageFilename, mediaType, - imageDescription, useBlurhash, - inReplyTo, inReplyToAtomUri, rssTitle, - schedulePost, - eventDate, eventTime, location) - blog['object']['type'] = 'Article' - blog['object']['published'] = published - return blog - - def createQuestionPost(baseDir: str, nickname: str, domain: str, port: int, httpPrefix: str, content: str, qOptions: [], diff --git a/utils.py b/utils.py index 323292ac..0d3977e7 100644 --- a/utils.py +++ b/utils.py @@ -220,10 +220,14 @@ def loadJsonOnionify(filename: str, domain: str, onionDomain: str, return jsonObject -def getStatusNumber() -> (str, str): +def getStatusNumber(publishedStr=None) -> (str, str): """Returns the status number and published date """ - currTime = datetime.datetime.utcnow() + if not publishedStr: + currTime = datetime.datetime.utcnow() + else: + currTime = \ + datetime.datetime.strptime(publishedStr, '%Y-%m-%dT%H:%M:%SZ') daysSinceEpoch = (currTime - datetime.datetime(1970, 1, 1)).days # status is the number of seconds since epoch statusNumber = \