epicyon/newsdaemon.py

890 lines
33 KiB
Python
Raw Normal View History

2020-10-07 12:05:49 +00:00
__filename__ = "newsdaemon.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-10-07 12:05:49 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-10-07 12:05:49 +00:00
__status__ = "Production"
2021-06-26 11:27:14 +00:00
__module_group__ = "Web Interface Columns"
2020-10-07 12:05:49 +00:00
2020-10-17 18:53:08 +00:00
# Example hashtag logic:
#
# if moderated and not #imcoxford then block
# if #pol and contains "westminster" then add #britpol
2020-10-17 19:06:56 +00:00
# if #unwantedtag then block
2020-10-17 18:53:08 +00:00
import os
2020-10-07 12:05:49 +00:00
import time
2020-10-09 10:05:01 +00:00
import datetime
2020-10-20 13:07:02 +00:00
import html
2020-10-19 19:26:58 +00:00
from shutil import rmtree
from subprocess import Popen
2020-10-07 18:46:42 +00:00
from collections import OrderedDict
2021-12-29 21:55:09 +00:00
from newswire import get_dict_from_newswire
# from posts import send_signed_json
from posts import create_news_post
from posts import archive_posts_for_person
from content import valid_hash_tag
2021-12-26 11:29:40 +00:00
from utils import get_base_content_from_post
2021-12-27 15:43:22 +00:00
from utils import remove_html
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2021-12-26 15:13:34 +00:00
from utils import load_json
2021-12-26 14:47:21 +00:00
from utils import save_json
2021-12-27 17:42:35 +00:00
from utils import get_status_number
2021-12-28 10:17:58 +00:00
from utils import clear_from_post_caches
2021-12-27 21:42:08 +00:00
from utils import dangerous_markup
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
2021-12-29 21:55:09 +00:00
from inbox import store_hash_tags
2021-12-28 16:56:57 +00:00
from session import create_session
2020-10-07 12:05:49 +00:00
2020-10-08 12:29:40 +00:00
2021-12-29 21:55:09 +00:00
def _update_feeds_outbox_index(base_dir: str, domain: str,
post_id: str) -> None:
"""Updates the index used for imported RSS feeds
"""
2021-12-25 16:17:53 +00:00
basePath = base_dir + '/accounts/news@' + domain
2020-10-07 16:55:15 +00:00
indexFilename = basePath + '/outbox.index'
if os.path.isfile(indexFilename):
2021-12-26 19:47:06 +00:00
if post_id not in open(indexFilename).read():
2020-10-07 18:46:42 +00:00
try:
with open(indexFilename, 'r+') as feedsFile:
content = feedsFile.read()
2021-12-26 19:47:06 +00:00
if post_id + '\n' not in content:
feedsFile.seek(0, 0)
2021-12-26 19:47:06 +00:00
feedsFile.write(post_id + '\n' + content)
print('DEBUG: feeds post added to index')
2021-12-25 15:28:52 +00:00
except Exception as ex:
2020-10-07 18:46:42 +00:00
print('WARN: Failed to write entry to feeds posts index ' +
2021-12-25 15:28:52 +00:00
indexFilename + ' ' + str(ex))
else:
2021-11-25 21:18:53 +00:00
try:
with open(indexFilename, 'w+') as feedsFile:
2021-12-26 19:47:06 +00:00
feedsFile.write(post_id + '\n')
2021-11-25 21:18:53 +00:00
except OSError:
2021-11-25 22:22:54 +00:00
print('EX: unable to write ' + indexFilename)
2021-12-29 21:55:09 +00:00
def _save_arrived_time(base_dir: str, post_filename: str,
arrived: str) -> None:
2020-10-09 12:15:20 +00:00
"""Saves the time when an rss post arrived to a file
"""
2021-11-25 21:18:53 +00:00
try:
2021-12-26 23:41:34 +00:00
with open(post_filename + '.arrived', 'w+') as arrivedFile:
2021-11-25 21:18:53 +00:00
arrivedFile.write(arrived)
except OSError:
2021-12-26 23:41:34 +00:00
print('EX: unable to write ' + post_filename + '.arrived')
2020-10-09 12:15:20 +00:00
2021-12-29 21:55:09 +00:00
def _remove_control_characters(content: str) -> str:
2020-10-20 13:07:02 +00:00
"""Remove escaped html
2020-10-11 09:33:31 +00:00
"""
2020-10-20 13:07:02 +00:00
if '&' in content:
return html.unescape(content)
2020-10-11 09:33:31 +00:00
return content
2020-10-10 09:36:23 +00:00
2020-10-10 08:54:13 +00:00
2021-12-29 21:55:09 +00:00
def _hashtag_logical_not(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
2021-07-04 09:24:35 +00:00
""" NOT
"""
if len(tree) != 2:
return False
if isinstance(tree[1], str):
return tree[1] not in hashtags
elif isinstance(tree[1], list):
2021-12-29 21:55:09 +00:00
return not hashtag_rule_resolve(tree[1], hashtags,
moderated, content, url)
2021-07-04 09:24:35 +00:00
return False
2021-12-29 21:55:09 +00:00
def _hashtag_logical_contains(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
2021-07-04 09:24:35 +00:00
""" Contains
"""
if len(tree) != 2:
return False
matchStr = None
if isinstance(tree[1], str):
matchStr = tree[1]
elif isinstance(tree[1], list):
matchStr = tree[1][0]
if matchStr:
if matchStr.startswith('"') and matchStr.endswith('"'):
matchStr = matchStr[1:]
matchStr = matchStr[:len(matchStr) - 1]
matchStrLower = matchStr.lower()
contentWithoutTags = content.replace('#' + matchStrLower, '')
return matchStrLower in contentWithoutTags
return False
2021-12-29 21:55:09 +00:00
def _hashtag_logical_from(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
2021-07-04 09:24:35 +00:00
""" FROM
"""
if len(tree) != 2:
return False
matchStr = None
if isinstance(tree[1], str):
matchStr = tree[1]
elif isinstance(tree[1], list):
matchStr = tree[1][0]
if matchStr:
if matchStr.startswith('"') and matchStr.endswith('"'):
matchStr = matchStr[1:]
matchStr = matchStr[:len(matchStr) - 1]
return matchStr.lower() in url
return False
2021-12-29 21:55:09 +00:00
def _hashtag_logical_and(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
2021-07-04 09:24:35 +00:00
""" AND
"""
if len(tree) < 3:
return False
for argIndex in range(1, len(tree)):
argValue = False
if isinstance(tree[argIndex], str):
argValue = (tree[argIndex] in hashtags)
elif isinstance(tree[argIndex], list):
2021-12-29 21:55:09 +00:00
argValue = hashtag_rule_resolve(tree[argIndex],
hashtags, moderated,
content, url)
2021-07-04 09:24:35 +00:00
if not argValue:
return False
return True
2021-12-29 21:55:09 +00:00
def _hashtag_logical_or(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
2021-07-04 09:24:35 +00:00
""" OR
"""
if len(tree) < 3:
return False
for argIndex in range(1, len(tree)):
argValue = False
if isinstance(tree[argIndex], str):
argValue = (tree[argIndex] in hashtags)
elif isinstance(tree[argIndex], list):
2021-12-29 21:55:09 +00:00
argValue = hashtag_rule_resolve(tree[argIndex],
hashtags, moderated,
content, url)
2021-07-04 09:24:35 +00:00
if argValue:
return True
return False
2021-12-29 21:55:09 +00:00
def _hashtag_logical_xor(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
2021-07-04 09:24:35 +00:00
""" XOR
"""
if len(tree) < 3:
return False
trueCtr = 0
for argIndex in range(1, len(tree)):
argValue = False
if isinstance(tree[argIndex], str):
argValue = (tree[argIndex] in hashtags)
elif isinstance(tree[argIndex], list):
2021-12-29 21:55:09 +00:00
argValue = hashtag_rule_resolve(tree[argIndex],
hashtags, moderated,
content, url)
2021-07-04 09:24:35 +00:00
if argValue:
trueCtr += 1
if trueCtr == 1:
return True
return False
2021-12-29 21:55:09 +00:00
def hashtag_rule_resolve(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
2020-10-17 12:05:41 +00:00
"""Returns whether the tree for a hashtag rule evaluates to true or false
"""
if not tree:
return False
if tree[0] == 'not':
2021-12-29 21:55:09 +00:00
return _hashtag_logical_not(tree, hashtags, moderated, content, url)
elif tree[0] == 'contains':
2021-12-29 21:55:09 +00:00
return _hashtag_logical_contains(tree, hashtags, moderated,
content, url)
2020-10-20 17:37:15 +00:00
elif tree[0] == 'from':
2021-12-29 21:55:09 +00:00
return _hashtag_logical_from(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
elif tree[0] == 'and':
2021-12-29 21:55:09 +00:00
return _hashtag_logical_and(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
elif tree[0] == 'or':
2021-12-29 21:55:09 +00:00
return _hashtag_logical_or(tree, hashtags, moderated, content, url)
2020-10-20 09:43:30 +00:00
elif tree[0] == 'xor':
2021-12-29 21:55:09 +00:00
return _hashtag_logical_xor(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
elif tree[0].startswith('#') and len(tree) == 1:
return tree[0] in hashtags
2020-10-17 17:36:10 +00:00
elif tree[0].startswith('moderated'):
return moderated
elif tree[0].startswith('"') and tree[0].endswith('"'):
return True
2020-10-17 12:05:41 +00:00
return False
2021-12-29 21:55:09 +00:00
def hashtag_rule_tree(operators: [],
conditionsStr: str,
tagsInConditions: [],
moderated: bool) -> []:
2020-10-17 12:05:41 +00:00
"""Walks the tree
"""
if not operators and conditionsStr:
conditionsStr = conditionsStr.strip()
isStr = conditionsStr.startswith('"') and conditionsStr.endswith('"')
if conditionsStr.startswith('#') or isStr or \
2020-10-17 17:36:10 +00:00
conditionsStr in operators or \
conditionsStr == 'moderated' or \
conditionsStr == 'contains':
2020-10-17 12:05:41 +00:00
if conditionsStr.startswith('#'):
if conditionsStr not in tagsInConditions:
if ' ' not in conditionsStr or \
conditionsStr.startswith('"'):
2020-10-17 12:05:41 +00:00
tagsInConditions.append(conditionsStr)
return [conditionsStr.strip()]
else:
return None
if not operators or not conditionsStr:
return None
tree = None
conditionsStr = conditionsStr.strip()
isStr = conditionsStr.startswith('"') and conditionsStr.endswith('"')
if conditionsStr.startswith('#') or isStr or \
2020-10-17 17:36:10 +00:00
conditionsStr in operators or \
conditionsStr == 'moderated' or \
conditionsStr == 'contains':
2020-10-17 12:05:41 +00:00
if conditionsStr.startswith('#'):
if conditionsStr not in tagsInConditions:
if ' ' not in conditionsStr or \
conditionsStr.startswith('"'):
2020-10-17 12:05:41 +00:00
tagsInConditions.append(conditionsStr)
tree = [conditionsStr.strip()]
ctr = 0
while ctr < len(operators):
op = operators[ctr]
2020-10-18 15:10:36 +00:00
opMatch = ' ' + op + ' '
if opMatch not in conditionsStr and \
not conditionsStr.startswith(op + ' '):
2020-10-17 12:05:41 +00:00
ctr += 1
continue
else:
tree = [op]
2020-10-18 15:10:36 +00:00
if opMatch in conditionsStr:
sections = conditionsStr.split(opMatch)
else:
sections = conditionsStr.split(op + ' ', 1)
2020-10-17 12:05:41 +00:00
for subConditionStr in sections:
2021-12-29 21:55:09 +00:00
result = hashtag_rule_tree(operators[ctr + 1:],
subConditionStr,
tagsInConditions, moderated)
2020-10-17 12:05:41 +00:00
if result:
tree.append(result)
break
return tree
2021-12-29 21:55:09 +00:00
def _hashtag_add(base_dir: str, http_prefix: str, domain_full: str,
post_json_object: {},
actionStr: str, hashtags: [], system_language: str,
translate: {}) -> None:
2021-07-04 09:46:48 +00:00
"""Adds a hashtag via a hashtag rule
"""
addHashtag = actionStr.split('add ', 1)[1].strip()
if not addHashtag.startswith('#'):
return
if addHashtag not in hashtags:
hashtags.append(addHashtag)
htId = addHashtag.replace('#', '')
2021-12-29 21:55:09 +00:00
if not valid_hash_tag(htId):
2021-07-04 09:46:48 +00:00
return
2021-12-26 10:00:46 +00:00
hashtagUrl = http_prefix + "://" + domain_full + "/tags/" + htId
2021-07-04 09:46:48 +00:00
newTag = {
'href': hashtagUrl,
'name': addHashtag,
'type': 'Hashtag'
}
# does the tag already exist?
addTagObject = None
2021-12-25 22:09:19 +00:00
for t in post_json_object['object']['tag']:
2021-07-04 09:46:48 +00:00
if t.get('type') and t.get('name'):
if t['type'] == 'Hashtag' and \
t['name'] == addHashtag:
addTagObject = t
break
# append the tag if it wasn't found
if not addTagObject:
2021-12-25 22:09:19 +00:00
post_json_object['object']['tag'].append(newTag)
2021-07-04 09:46:48 +00:00
# add corresponding html to the post content
hashtagHtml = \
" <a href=\"" + hashtagUrl + "\" class=\"addedHashtag\" " + \
"rel=\"tag\">#<span>" + htId + "</span></a>"
2021-12-26 11:29:40 +00:00
content = get_base_content_from_post(post_json_object, system_language)
2021-07-04 09:46:48 +00:00
if hashtagHtml in content:
return
if content.endswith('</p>'):
content = \
content[:len(content) - len('</p>')] + \
hashtagHtml + '</p>'
else:
content += hashtagHtml
2021-12-25 22:09:19 +00:00
post_json_object['object']['content'] = content
2021-12-26 10:00:46 +00:00
domain = domain_full
2021-10-20 13:33:34 +00:00
if ':' in domain:
domain = domain.split(':')[0]
2021-12-29 21:55:09 +00:00
store_hash_tags(base_dir, 'news', domain,
http_prefix, domain_full,
post_json_object, translate)
2021-07-04 09:46:48 +00:00
2021-12-29 21:55:09 +00:00
def _hashtag_remove(http_prefix: str, domain_full: str, post_json_object: {},
actionStr: str, hashtags: [],
system_language: str) -> None:
2021-07-04 09:46:48 +00:00
"""Removes a hashtag via a hashtag rule
"""
rmHashtag = actionStr.split('remove ', 1)[1].strip()
if not rmHashtag.startswith('#'):
return
if rmHashtag in hashtags:
hashtags.remove(rmHashtag)
htId = rmHashtag.replace('#', '')
2021-12-26 10:00:46 +00:00
hashtagUrl = http_prefix + "://" + domain_full + "/tags/" + htId
2021-07-04 09:46:48 +00:00
# remove tag html from the post content
hashtagHtml = \
"<a href=\"" + hashtagUrl + "\" class=\"addedHashtag\" " + \
"rel=\"tag\">#<span>" + htId + "</span></a>"
2021-12-26 11:29:40 +00:00
content = get_base_content_from_post(post_json_object, system_language)
2021-07-04 09:46:48 +00:00
if hashtagHtml in content:
content = content.replace(hashtagHtml, '').replace(' ', ' ')
2021-12-25 22:09:19 +00:00
post_json_object['object']['content'] = content
2021-12-25 23:03:28 +00:00
post_json_object['object']['contentMap'][system_language] = content
2021-07-04 09:46:48 +00:00
rmTagObject = None
2021-12-25 22:09:19 +00:00
for t in post_json_object['object']['tag']:
2021-07-04 09:46:48 +00:00
if t.get('type') and t.get('name'):
if t['type'] == 'Hashtag' and \
t['name'] == rmHashtag:
rmTagObject = t
break
if rmTagObject:
2021-12-25 22:09:19 +00:00
post_json_object['object']['tag'].remove(rmTagObject)
2021-07-04 09:46:48 +00:00
2021-12-29 21:55:09 +00:00
def _newswire_hashtag_processing(session, base_dir: str, post_json_object: {},
hashtags: [], http_prefix: str,
domain: str, port: int,
person_cache: {},
cached_webfingers: {},
federation_list: [],
send_threads: [], postLog: [],
moderated: bool, url: str,
system_language: str,
translate: {}) -> bool:
2020-10-16 21:33:18 +00:00
"""Applies hashtag rules to a news post.
Returns true if the post should be saved to the news timeline
of this instance
"""
2021-12-25 16:17:53 +00:00
rulesFilename = base_dir + '/accounts/hashtagrules.txt'
2020-10-17 12:05:41 +00:00
if not os.path.isfile(rulesFilename):
return True
rules = []
2021-07-13 14:40:49 +00:00
with open(rulesFilename, 'r') as f:
2020-10-17 12:05:41 +00:00
rules = f.readlines()
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2020-10-17 12:05:41 +00:00
# get the full text content of the post
content = ''
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('content'):
2021-12-26 11:29:40 +00:00
content += get_base_content_from_post(post_json_object,
system_language)
2021-12-25 22:09:19 +00:00
if post_json_object['object'].get('summary'):
content += ' ' + post_json_object['object']['summary']
content = content.lower()
2020-10-17 13:41:20 +00:00
# actionOccurred = False
2020-10-20 17:37:15 +00:00
operators = ('not', 'and', 'or', 'xor', 'from', 'contains')
2020-10-17 12:05:41 +00:00
for ruleStr in rules:
if not ruleStr:
continue
if not ruleStr.startswith('if '):
continue
if ' then ' not in ruleStr:
continue
conditionsStr = ruleStr.split('if ', 1)[1]
conditionsStr = conditionsStr.split(' then ')[0]
tagsInConditions = []
2021-12-29 21:55:09 +00:00
tree = hashtag_rule_tree(operators, conditionsStr,
tagsInConditions, moderated)
if not hashtag_rule_resolve(tree, hashtags, moderated, content, url):
2020-10-17 12:05:41 +00:00
continue
# the condition matches, so do something
actionStr = ruleStr.split(' then ')[1].strip()
if actionStr.startswith('add '):
2021-07-04 09:46:48 +00:00
# add a hashtag
2021-12-29 21:55:09 +00:00
_hashtag_add(base_dir, http_prefix, domain_full,
post_json_object, actionStr, hashtags,
system_language, translate)
2021-07-04 09:46:48 +00:00
elif actionStr.startswith('remove '):
# remove a hashtag
2021-12-29 21:55:09 +00:00
_hashtag_remove(http_prefix, domain_full, post_json_object,
actionStr, hashtags, system_language)
2021-07-04 09:46:48 +00:00
elif actionStr.startswith('block') or actionStr.startswith('drop'):
# Block this item
return False
2020-10-16 21:33:18 +00:00
return True
2021-12-29 21:55:09 +00:00
def _create_news_mirror(base_dir: str, domain: str,
post_idNumber: str, url: str,
max_mirrored_articles: int) -> bool:
2020-10-19 16:33:58 +00:00
"""Creates a local mirror of a news article
"""
2020-10-19 19:26:58 +00:00
if '|' in url or '>' in url:
return True
2021-12-25 16:17:53 +00:00
mirrorDir = base_dir + '/accounts/newsmirror'
2020-10-19 16:33:58 +00:00
if not os.path.isdir(mirrorDir):
os.mkdir(mirrorDir)
2020-10-19 19:26:58 +00:00
# count the directories
noOfDirs = 0
for subdir, dirs, files in os.walk(mirrorDir):
noOfDirs = len(dirs)
2021-12-25 16:17:53 +00:00
mirrorIndexFilename = base_dir + '/accounts/newsmirror.txt'
2020-10-19 19:26:58 +00:00
2021-12-25 19:42:14 +00:00
if max_mirrored_articles > 0 and noOfDirs > max_mirrored_articles:
2020-10-19 19:26:58 +00:00
if not os.path.isfile(mirrorIndexFilename):
# no index for mirrors found
return True
removals = []
with open(mirrorIndexFilename, 'r') as indexFile:
# remove the oldest directories
ctr = 0
2021-12-25 19:42:14 +00:00
while noOfDirs > max_mirrored_articles:
2020-10-19 19:26:58 +00:00
ctr += 1
if ctr > 5000:
# escape valve
break
2021-12-26 19:47:06 +00:00
post_id = indexFile.readline()
if not post_id:
2020-10-19 19:26:58 +00:00
continue
2021-12-26 19:47:06 +00:00
post_id = post_id.strip()
mirrorArticleDir = mirrorDir + '/' + post_id
2020-10-19 19:26:58 +00:00
if os.path.isdir(mirrorArticleDir):
2021-10-29 18:48:15 +00:00
rmtree(mirrorArticleDir, ignore_errors=False, onerror=None)
2021-12-26 19:47:06 +00:00
removals.append(post_id)
2020-10-19 19:26:58 +00:00
noOfDirs -= 1
# remove the corresponding index entries
if removals:
indexContent = ''
with open(mirrorIndexFilename, 'r') as indexFile:
indexContent = indexFile.read()
2020-10-19 19:26:58 +00:00
for removePostId in removals:
indexContent = \
indexContent.replace(removePostId + '\n', '')
2021-11-25 21:18:53 +00:00
try:
with open(mirrorIndexFilename, 'w+') as indexFile:
indexFile.write(indexContent)
except OSError:
2021-11-25 22:22:54 +00:00
print('EX: unable to write ' + mirrorIndexFilename)
2020-10-19 19:26:58 +00:00
2021-12-26 19:47:06 +00:00
mirrorArticleDir = mirrorDir + '/' + post_idNumber
2020-10-19 19:26:58 +00:00
if os.path.isdir(mirrorArticleDir):
# already mirrored
return True
2020-10-20 09:27:58 +00:00
# for onion instances mirror via tor
prefixStr = ''
if domain.endswith('.onion'):
prefixStr = '/usr/bin/torsocks '
2020-10-19 19:26:58 +00:00
# download the files
commandStr = \
2020-10-20 09:27:58 +00:00
prefixStr + '/usr/bin/wget -mkEpnp -e robots=off ' + url + \
2020-10-19 19:26:58 +00:00
' -P ' + mirrorArticleDir
p = Popen(commandStr, shell=True)
os.waitpid(p.pid, 0)
if not os.path.isdir(mirrorArticleDir):
2020-10-20 09:27:58 +00:00
print('WARN: failed to mirror ' + url)
2020-10-19 19:26:58 +00:00
return True
# append the post Id number to the index file
if os.path.isfile(mirrorIndexFilename):
2021-11-25 21:18:53 +00:00
try:
with open(mirrorIndexFilename, 'a+') as indexFile:
2021-12-26 19:47:06 +00:00
indexFile.write(post_idNumber + '\n')
2021-11-25 21:18:53 +00:00
except OSError:
2021-11-25 22:22:54 +00:00
print('EX: unable to append ' + mirrorIndexFilename)
2020-10-19 19:26:58 +00:00
else:
2021-11-25 21:18:53 +00:00
try:
with open(mirrorIndexFilename, 'w+') as indexFile:
2021-12-26 19:47:06 +00:00
indexFile.write(post_idNumber + '\n')
2021-11-25 21:18:53 +00:00
except OSError:
2021-11-25 22:22:54 +00:00
print('EX: unable to write ' + mirrorIndexFilename)
2020-10-19 19:26:58 +00:00
2020-10-19 16:33:58 +00:00
return True
2021-12-29 21:55:09 +00:00
def _convert_rs_sto_activity_pub(base_dir: str, http_prefix: str,
domain: str, port: int,
newswire: {},
translate: {},
recent_posts_cache: {}, max_recent_posts: int,
session, cached_webfingers: {},
person_cache: {},
federation_list: [],
send_threads: [], postLog: [],
max_mirrored_articles: int,
allow_local_network_access: bool,
system_language: str,
low_bandwidth: bool,
content_license_url: str) -> None:
"""Converts rss items in a newswire into posts
"""
2020-11-03 14:41:28 +00:00
if not newswire:
2021-09-15 17:43:06 +00:00
print('No newswire to convert')
2020-11-03 14:41:28 +00:00
return
2021-12-25 16:17:53 +00:00
basePath = base_dir + '/accounts/news@' + domain + '/outbox'
if not os.path.isdir(basePath):
os.mkdir(basePath)
2020-10-09 10:05:01 +00:00
# oldest items first
2021-07-13 21:59:53 +00:00
newswireReverse = OrderedDict(sorted(newswire.items(), reverse=False))
2020-10-07 18:46:42 +00:00
for dateStr, item in newswireReverse.items():
2020-10-07 20:03:39 +00:00
originalDateStr = dateStr
# convert the date to the format used by ActivityPub
2020-10-20 12:37:32 +00:00
if '+00:00' in dateStr:
dateStr = dateStr.replace(' ', 'T')
dateStr = dateStr.replace('+00:00', 'Z')
else:
2021-09-15 17:43:06 +00:00
try:
dateStrWithOffset = \
datetime.datetime.strptime(dateStr, "%Y-%m-%d %H:%M:%S%z")
except BaseException:
2021-10-29 18:48:15 +00:00
print('EX: Newswire strptime failed ' + str(dateStr))
2021-09-15 17:43:06 +00:00
continue
2021-09-15 19:04:29 +00:00
try:
dateStr = dateStrWithOffset.strftime("%Y-%m-%dT%H:%M:%SZ")
except BaseException:
2021-10-29 18:48:15 +00:00
print('EX: Newswire dateStrWithOffset failed ' +
2021-09-15 19:04:29 +00:00
str(dateStrWithOffset))
continue
2021-12-27 17:42:35 +00:00
statusNumber, published = get_status_number(dateStr)
2020-10-07 16:55:15 +00:00
newPostId = \
2021-12-26 10:19:59 +00:00
local_actor_url(http_prefix, 'news', domain) + \
2021-08-14 11:13:39 +00:00
'/statuses/' + statusNumber
2020-10-07 16:55:15 +00:00
# file where the post is stored
2020-10-07 16:55:15 +00:00
filename = basePath + '/' + newPostId.replace('/', '#') + '.json'
if os.path.isfile(filename):
2020-10-08 12:52:15 +00:00
# don't create the post if it already exists
2020-10-08 14:35:26 +00:00
# set the url
2020-11-08 18:29:01 +00:00
# newswire[originalDateStr][1] = \
# '/users/news/statuses/' + statusNumber
2020-10-08 14:35:26 +00:00
# set the filename
newswire[originalDateStr][3] = filename
continue
2021-12-29 21:55:09 +00:00
rssTitle = _remove_control_characters(item[0])
url = item[1]
2021-12-27 21:42:08 +00:00
if dangerous_markup(url, allow_local_network_access) or \
dangerous_markup(rssTitle, allow_local_network_access):
2020-10-11 09:33:31 +00:00
continue
2020-10-07 13:55:27 +00:00
rssDescription = ''
# get the rss description if it exists
2021-12-27 15:43:22 +00:00
rssDescription = '<p>' + remove_html(item[4]) + '<p>'
2020-10-19 20:43:27 +00:00
mirrored = item[7]
postUrl = url
if mirrored and '://' in url:
2020-10-19 22:17:06 +00:00
postUrl = '/newsmirror/' + statusNumber + '/' + \
2020-10-19 22:21:30 +00:00
url.split('://')[1]
if postUrl.endswith('/'):
postUrl += 'index.html'
else:
postUrl += '/index.html'
2020-10-19 20:43:27 +00:00
# add the off-site link to the description
rssDescription += \
'<br><a href="' + postUrl + '">' + \
translate['Read more...'] + '</a>'
2020-10-11 09:33:31 +00:00
2020-10-07 16:55:15 +00:00
followersOnly = False
2020-10-09 10:08:01 +00:00
# NOTE: the id when the post is created will not be
# consistent (it's based on the current time, not the
# published time), so we change that later
2021-05-09 19:11:05 +00:00
saveToFile = False
attachImageFilename = None
mediaType = None
imageDescription = None
2021-05-09 19:29:53 +00:00
city = 'London, England'
2021-08-08 16:52:32 +00:00
conversationId = None
2021-12-29 21:55:09 +00:00
blog = create_news_post(base_dir,
domain, port, http_prefix,
rssDescription,
followersOnly, saveToFile,
attachImageFilename, mediaType,
imageDescription, city,
rssTitle, system_language,
conversationId, low_bandwidth,
content_license_url)
2020-10-07 16:55:15 +00:00
if not blog:
continue
2020-10-19 16:33:58 +00:00
if mirrored:
2021-12-29 21:55:09 +00:00
if not _create_news_mirror(base_dir, domain, statusNumber,
url, max_mirrored_articles):
2020-10-19 16:33:58 +00:00
continue
2020-10-07 16:55:15 +00:00
idStr = \
2021-12-26 10:19:59 +00:00
local_actor_url(http_prefix, 'news', domain) + \
2020-10-07 16:55:15 +00:00
'/statuses/' + statusNumber + '/replies'
blog['news'] = True
2020-10-09 10:05:01 +00:00
# note the time of arrival
2021-12-26 13:17:46 +00:00
curr_time = datetime.datetime.utcnow()
blog['object']['arrived'] = curr_time.strftime("%Y-%m-%dT%H:%M:%SZ")
2020-10-09 10:05:01 +00:00
2020-10-09 10:08:01 +00:00
# change the id, based upon the published time
2020-10-07 16:55:15 +00:00
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'] = \
2021-12-25 17:09:22 +00:00
http_prefix + '://' + domain + '/@news/' + statusNumber
2020-10-07 16:55:15 +00:00
blog['object']['published'] = dateStr
2020-10-20 13:07:02 +00:00
2020-10-20 12:49:12 +00:00
blog['object']['content'] = rssDescription
2021-12-25 23:03:28 +00:00
blog['object']['contentMap'][system_language] = rssDescription
2020-10-07 16:55:15 +00:00
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2020-10-17 13:59:47 +00:00
hashtags = item[6]
2021-12-26 19:47:06 +00:00
post_id = newPostId.replace('/', '#')
2020-10-09 12:15:20 +00:00
moderated = item[5]
2021-12-29 21:55:09 +00:00
savePost = \
_newswire_hashtag_processing(session, base_dir,
blog, hashtags,
http_prefix, domain, port,
person_cache, cached_webfingers,
federation_list,
send_threads, postLog,
moderated, url, system_language,
translate)
2020-10-09 12:15:20 +00:00
2020-10-16 21:33:18 +00:00
# save the post and update the index
if savePost:
# ensure that all hashtags are stored in the json
# and appended to the content
blog['object']['tag'] = []
2020-10-25 11:22:52 +00:00
for tagName in hashtags:
htId = tagName.replace('#', '')
hashtagUrl = \
2021-12-26 10:00:46 +00:00
http_prefix + "://" + domain_full + "/tags/" + htId
2020-10-25 11:22:52 +00:00
newTag = {
'href': hashtagUrl,
'name': tagName,
'type': 'Hashtag'
}
blog['object']['tag'].append(newTag)
hashtagHtml = \
2020-10-25 12:57:14 +00:00
" <a href=\"" + hashtagUrl + \
"\" class=\"addedHashtag\" " + \
"rel=\"tag\">#<span>" + \
htId + "</span></a>"
2021-12-26 11:29:40 +00:00
content = get_base_content_from_post(blog, system_language)
2020-10-25 14:37:51 +00:00
if hashtagHtml not in content:
if content.endswith('</p>'):
content = \
content[:len(content) - len('</p>')] + \
hashtagHtml + '</p>'
else:
content += hashtagHtml
blog['object']['content'] = content
2021-12-25 23:03:28 +00:00
blog['object']['contentMap'][system_language] = content
2020-10-25 11:22:52 +00:00
2020-10-25 14:21:29 +00:00
# update the newswire tags if new ones have been found by
2021-12-29 21:55:09 +00:00
# _newswire_hashtag_processing
2020-10-25 14:21:29 +00:00
for tag in hashtags:
if tag not in newswire[originalDateStr][6]:
newswire[originalDateStr][6].append(tag)
2020-10-17 13:39:04 +00:00
2021-12-29 21:55:09 +00:00
store_hash_tags(base_dir, 'news', domain,
http_prefix, domain_full,
blog, translate)
2020-10-17 13:39:04 +00:00
2021-12-28 10:17:58 +00:00
clear_from_post_caches(base_dir, recent_posts_cache, post_id)
2021-12-26 14:47:21 +00:00
if save_json(blog, filename):
2021-12-29 21:55:09 +00:00
_update_feeds_outbox_index(base_dir, domain, post_id + '.json')
2020-10-16 21:33:18 +00:00
# Save a file containing the time when the post arrived
# this can then later be used to construct the news timeline
# excluding items during the voting period
if moderated:
2021-12-29 21:55:09 +00:00
_save_arrived_time(base_dir, filename,
blog['object']['arrived'])
2020-10-16 21:33:18 +00:00
else:
if os.path.isfile(filename + '.arrived'):
try:
os.remove(filename + '.arrived')
2021-11-25 18:42:38 +00:00
except OSError:
2021-12-29 21:55:09 +00:00
print('EX: _convert_rs_sto_activity_pub ' +
2021-10-29 18:48:15 +00:00
'unable to delete ' + filename + '.arrived')
2020-10-16 21:33:18 +00:00
2020-11-08 16:52:57 +00:00
# setting the url here links to the activitypub object
# stored locally
2020-11-08 16:50:50 +00:00
# newswire[originalDateStr][1] = \
# '/users/news/statuses/' + statusNumber
2020-11-08 16:52:57 +00:00
2020-10-16 21:33:18 +00:00
# set the filename
newswire[originalDateStr][3] = filename
2021-12-29 21:55:09 +00:00
def _merge_with_previous_newswire(oldNewswire: {}, newNewswire: {}) -> None:
2020-10-09 09:02:01 +00:00
"""Preserve any votes or generated activitypub post filename
as rss feeds are updated
"""
2020-11-03 14:41:28 +00:00
if not oldNewswire:
return
2020-10-09 09:02:01 +00:00
for published, fields in oldNewswire.items():
if not newNewswire.get(published):
continue
2020-10-13 08:53:59 +00:00
for i in range(1, 5):
newNewswire[published][i] = fields[i]
2020-10-09 09:02:01 +00:00
2021-12-29 21:55:09 +00:00
def run_newswire_daemon(base_dir: str, httpd,
http_prefix: str, domain: str, port: int,
translate: {}) -> None:
2020-10-07 12:05:49 +00:00
"""Periodically updates RSS feeds
"""
2021-12-25 16:17:53 +00:00
newswireStateFilename = base_dir + '/accounts/.newswirestate.json'
refreshFilename = base_dir + '/accounts/.refresh_newswire'
2020-10-09 09:02:01 +00:00
2020-10-07 12:05:49 +00:00
# initial sleep to allow the system to start up
time.sleep(50)
while True:
# has the session been created yet?
if not httpd.session:
2020-11-03 16:10:54 +00:00
print('Newswire daemon waiting for session')
2021-12-28 16:56:57 +00:00
httpd.session = create_session(httpd.proxy_type)
if not httpd.session:
2020-11-03 16:10:54 +00:00
print('Newswire daemon has no session')
time.sleep(60)
continue
else:
print('Newswire daemon session established')
2020-10-07 12:05:49 +00:00
# try to update the feeds
2021-09-15 17:03:20 +00:00
print('Updating newswire feeds')
2020-11-22 20:28:32 +00:00
newNewswire = \
2021-12-29 21:55:09 +00:00
get_dict_from_newswire(httpd.session, base_dir, domain,
httpd.max_newswire_postsPerSource,
httpd.max_newswire_feed_size_kb,
httpd.maxTags,
httpd.max_feed_item_size_kb,
httpd.max_newswire_posts,
httpd.maxCategoriesFeedItemSizeKb,
httpd.system_language,
httpd.debug)
2020-10-07 12:05:49 +00:00
2020-10-09 09:02:01 +00:00
if not httpd.newswire:
2021-09-15 17:03:20 +00:00
print('Newswire feeds not updated')
if os.path.isfile(newswireStateFilename):
2021-09-15 17:03:20 +00:00
print('Loading newswire from file')
2021-12-26 15:13:34 +00:00
httpd.newswire = load_json(newswireStateFilename)
2020-10-09 09:02:01 +00:00
2021-09-15 17:03:20 +00:00
print('Merging with previous newswire')
2021-12-29 21:55:09 +00:00
_merge_with_previous_newswire(httpd.newswire, newNewswire)
2020-10-09 09:02:01 +00:00
2020-10-07 12:05:49 +00:00
httpd.newswire = newNewswire
2020-11-03 21:53:29 +00:00
if newNewswire:
2021-12-26 14:47:21 +00:00
save_json(httpd.newswire, newswireStateFilename)
2020-11-03 21:53:29 +00:00
print('Newswire updated')
2021-09-15 17:03:20 +00:00
else:
print('No new newswire')
2021-09-15 17:03:20 +00:00
print('Converting newswire to activitypub format')
2021-12-29 21:55:09 +00:00
_convert_rs_sto_activity_pub(base_dir,
http_prefix, domain, port,
newNewswire, translate,
httpd.recent_posts_cache,
httpd.max_recent_posts,
httpd.session,
httpd.cached_webfingers,
httpd.person_cache,
httpd.federation_list,
httpd.send_threads,
httpd.postLog,
httpd.max_mirrored_articles,
httpd.allow_local_network_access,
httpd.system_language,
httpd.low_bandwidth,
httpd.content_license_url)
print('Newswire feed converted to ActivityPub')
2021-12-25 19:39:45 +00:00
if httpd.max_news_posts > 0:
2021-12-25 23:41:17 +00:00
archive_dir = base_dir + '/archive'
2020-10-21 10:39:09 +00:00
archiveSubdir = \
2021-12-25 23:41:17 +00:00
archive_dir + '/accounts/news@' + domain + '/outbox'
2021-09-15 17:03:20 +00:00
print('Archiving news posts')
2021-12-29 21:55:09 +00:00
archive_posts_for_person(http_prefix, 'news',
domain, base_dir, 'outbox',
archiveSubdir,
httpd.recent_posts_cache,
httpd.max_news_posts)
2020-10-21 10:39:09 +00:00
2020-10-07 12:05:49 +00:00
# wait a while before the next feeds update
for tick in range(120):
time.sleep(10)
# if a new blog post has been created then stop
# waiting and recalculate the newswire
if os.path.isfile(refreshFilename):
try:
os.remove(refreshFilename)
2021-11-25 18:42:38 +00:00
except OSError:
2021-12-29 21:55:09 +00:00
print('EX: run_newswire_daemon unable to delete ' +
2021-10-29 18:48:15 +00:00
str(refreshFilename))
break
2020-10-07 12:05:49 +00:00
2021-12-29 21:55:09 +00:00
def run_newswire_watchdog(project_version: str, httpd) -> None:
2020-10-07 12:05:49 +00:00
"""This tries to keep the newswire update thread running even if it dies
"""
print('Starting newswire watchdog')
newswireOriginal = \
2021-12-29 21:55:09 +00:00
httpd.thrPostSchedule.clone(run_newswire_daemon)
2020-10-07 12:05:49 +00:00
httpd.thrNewswireDaemon.start()
while True:
time.sleep(50)
2021-06-05 12:43:57 +00:00
if httpd.thrNewswireDaemon.is_alive():
continue
httpd.thrNewswireDaemon.kill()
httpd.thrNewswireDaemon = \
2021-12-29 21:55:09 +00:00
newswireOriginal.clone(run_newswire_daemon)
2021-06-05 12:43:57 +00:00
httpd.thrNewswireDaemon.start()
print('Restarting newswire daemon...')