Save the current newswire moderation state to a file

merge-requests/8/head
Bob Mottram 2020-10-06 12:28:32 +01:00
parent fc267f8b91
commit d5248ea2d6
1 changed files with 50 additions and 19 deletions

View File

@ -15,6 +15,7 @@ from datetime import datetime
from collections import OrderedDict from collections import OrderedDict
from utils import locatePost from utils import locatePost
from utils import loadJson from utils import loadJson
from utils import saveJson
from utils import isSuspended from utils import isSuspended
from utils import getConfigParam from utils import getConfigParam
@ -170,6 +171,22 @@ def getRSSfromDict(baseDir: str, newswire: {},
return rssStr return rssStr
def isaBlogPost(postJsonObject: {}) -> bool:
"""Is the given object a blog post?
"""
if not postJsonObject:
return False
if not postJsonObject.get('object'):
return False
if not isinstance(postJsonObject['object'], dict):
return False
if postJsonObject['object'].get('summary') and \
postJsonObject['object'].get('url') and \
postJsonObject['object'].get('published'):
return True
return False
def updateNewswireModerationQueue(baseDir: str, handle: str, def updateNewswireModerationQueue(baseDir: str, handle: str,
maxBlogsPerAccount: int, maxBlogsPerAccount: int,
moderationDict: {}) -> None: moderationDict: {}) -> None:
@ -203,18 +220,34 @@ def updateNewswireModerationQueue(baseDir: str, handle: str,
locatePost(baseDir, nickname, locatePost(baseDir, nickname,
domain, postUrl, False) domain, postUrl, False)
moderationStatusFilename = fullPostFilename + '.moderate' moderationStatusFilename = fullPostFilename + '.moderate'
moderationStatusStr = ''
if not os.path.isfile(moderationStatusFilename): if not os.path.isfile(moderationStatusFilename):
statusFile = open(moderationStatusFilename, "w+") statusFile = open(moderationStatusFilename, "w+")
if statusFile: if statusFile:
statusFile.write('[waiting]') statusFile.write('[waiting]')
statusFile.close() statusFile.close()
moderationStatusStr = '[waiting]'
else:
statusFile = open(moderationStatusFilename, "r")
if statusFile:
moderationStatusStr = statusFile.read()
statusFile.close()
if '[accepted]' not in \ if '[accepted]' not in \
open(moderationStatusFilename).read(): open(moderationStatusFilename).read():
if moderationDict.get(nickname):
moderationDict[nickname] = [] postJsonObject = None
if fullPostFilename not in moderationDict[nickname]: if fullPostFilename:
moderationDict[nickname].append(fullPostFilename) postJsonObject = loadJson(fullPostFilename)
if isaBlogPost(postJsonObject):
published = postJsonObject['object']['published']
published = published.replace('T', ' ')
published = published.replace('Z', '+00:00')
moderationDict[published] = \
[postJsonObject['object']['summary'],
postJsonObject['object']['url'],
nickname, moderationStatusStr,
fullPostFilename]
ctr += 1 ctr += 1
if ctr >= maxBlogsPerAccount: if ctr >= maxBlogsPerAccount:
@ -250,24 +283,16 @@ def addAccountBlogsToNewswire(baseDir: str, nickname: str, domain: str,
fullPostFilename = \ fullPostFilename = \
locatePost(baseDir, nickname, locatePost(baseDir, nickname,
domain, postUrl, False) domain, postUrl, False)
isAPost = False
postJsonObject = None postJsonObject = None
if fullPostFilename: if fullPostFilename:
postJsonObject = loadJson(fullPostFilename) postJsonObject = loadJson(fullPostFilename)
if postJsonObject: if isaBlogPost(postJsonObject):
if postJsonObject.get('object'): published = postJsonObject['object']['published']
if isinstance(postJsonObject['object'], dict): published = published.replace('T', ' ')
isAPost = True published = published.replace('Z', '+00:00')
if isAPost: newswire[published] = \
if postJsonObject['object'].get('summary') and \ [postJsonObject['object']['summary'],
postJsonObject['object'].get('url') and \ postJsonObject['object']['url']]
postJsonObject['object'].get('published'):
published = postJsonObject['object']['published']
published = published.replace('T', ' ')
published = published.replace('Z', '+00:00')
newswire[published] = \
[postJsonObject['object']['summary'],
postJsonObject['object']['url']]
ctr += 1 ctr += 1
if ctr >= maxBlogsPerAccount: if ctr >= maxBlogsPerAccount:
@ -327,6 +352,12 @@ def addBlogsToNewswire(baseDir: str, newswire: {},
newswire, maxBlogsPerAccount, newswire, maxBlogsPerAccount,
blogsIndex) blogsIndex)
# sort the moderation dict into chronological order, latest first
sortedModerationDict = \
OrderedDict(sorted(moderationDict.items(), reverse=True))
newswireModerationFilename = baseDir + '/accounts/newswiremoderation.txt'
saveJson(sortedModerationDict, newswireModerationFilename)
def getDictFromNewswire(session, baseDir: str) -> {}: def getDictFromNewswire(session, baseDir: str) -> {}:
"""Gets rss feeds as a dictionary from newswire file """Gets rss feeds as a dictionary from newswire file