Move rss posts to news account

main
Bob Mottram 2020-10-07 17:55:15 +01:00
parent a7e22c7590
commit 3928340dc9
3 changed files with 51 additions and 57 deletions

View File

@ -9,22 +9,24 @@ __status__ = "Production"
import os import os
import time import time
from newswire import getDictFromNewswire from newswire import getDictFromNewswire
from posts import createNewsPost from posts import createBlogPost
from utils import saveJson 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 """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 os.path.isfile(indexFilename):
if filename not in open(indexFilename).read(): if postId not in open(indexFilename).read():
try: try:
with open(indexFilename, 'r+') as feedsFile: with open(indexFilename, 'r+') as feedsFile:
content = feedsFile.read() content = feedsFile.read()
feedsFile.seek(0, 0) feedsFile.seek(0, 0)
feedsFile.write(filename + '\n' + content) feedsFile.write(postId + '\n' + content)
print('DEBUG: feeds post added to index') print('DEBUG: feeds post added to index')
except Exception as e: except Exception as e:
print('WARN: Failed to write entry to feeds posts index ' + print('WARN: Failed to write entry to feeds posts index ' +
@ -32,7 +34,7 @@ def updateFeedsIndex(baseDir: str, filename: str) -> None:
else: else:
feedsFile = open(indexFilename, 'w+') feedsFile = open(indexFilename, 'w+')
if feedsFile: if feedsFile:
feedsFile.write(filename + '\n') feedsFile.write(postId + '\n')
feedsFile.close() feedsFile.close()
@ -42,25 +44,28 @@ def convertRSStoActivityPub(baseDir: str, httpPrefix: str,
translate: {}) -> None: translate: {}) -> None:
"""Converts rss items in a newswire into posts """Converts rss items in a newswire into posts
""" """
basePath = baseDir + '/accounts/feeds' basePath = baseDir + '/accounts/news@' + domain + '/outbox'
if not os.path.isdir(basePath): if not os.path.isdir(basePath):
os.mkdir(basePath) os.mkdir(basePath)
nickname = 'feeds'
for dateStr, item in newswire.items(): for dateStr, item in newswire.items():
# convert the date to the format used by ActivityPub # convert the date to the format used by ActivityPub
dateStr = dateStr.replace(' ', 'T') dateStr = dateStr.replace(' ', 'T')
dateStr = dateStr.replace('+00:00', 'Z') dateStr = dateStr.replace('+00:00', 'Z')
statusNumber, published = getStatusNumber(dateStr)
newPostId = \
httpPrefix + '://' + domain + \
'/users/news/statuses/' + statusNumber
# file where the post is stored # file where the post is stored
filename = basePath + '/' + dateStr + '.json' filename = basePath + '/' + newPostId.replace('/', '#') + '.json'
if os.path.isfile(filename): if os.path.isfile(filename):
# if a local post exists as html then change the link # if a local post exists as html then change the link
# to the local one # to the local one
htmlFilename = basePath + '/' + dateStr + '.html' htmlFilename = filename.replace('.json', '.html')
if os.path.isfile(htmlFilename): 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 # don't create the post if it already exists
continue continue
@ -79,16 +84,37 @@ def convertRSStoActivityPub(baseDir: str, httpPrefix: str,
else: else:
rssDescription = url rssDescription = url
# create the activitypub post followersOnly = False
blog = createNewsPost(baseDir, useBlurhash = False
nickname, domain, port, blog = createBlogPost(baseDir,
httpPrefix, dateStr, 'news', domain, port, httpPrefix,
rssTitle, rssDescription, rssDescription, followersOnly, False,
None, None, None, 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 # save the post and update the index
if saveJson(blog, filename): if saveJson(blog, filename):
updateFeedsIndex(baseDir, filename) updateFeedsIndex(baseDir, domain, postId + '.json')
def runNewswireDaemon(baseDir: str, httpd, def runNewswireDaemon(baseDir: str, httpd,

View File

@ -1193,42 +1193,6 @@ def createBlogPost(baseDir: str,
return blog 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, def createQuestionPost(baseDir: str,
nickname: str, domain: str, port: int, httpPrefix: str, nickname: str, domain: str, port: int, httpPrefix: str,
content: str, qOptions: [], content: str, qOptions: [],

View File

@ -220,10 +220,14 @@ def loadJsonOnionify(filename: str, domain: str, onionDomain: str,
return jsonObject return jsonObject
def getStatusNumber() -> (str, str): def getStatusNumber(publishedStr=None) -> (str, str):
"""Returns the status number and published date """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 daysSinceEpoch = (currTime - datetime.datetime(1970, 1, 1)).days
# status is the number of seconds since epoch # status is the number of seconds since epoch
statusNumber = \ statusNumber = \