From 50ec0f6ce29673ffa8435f4d5b6165a1240cd05d Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 3 Mar 2021 19:06:18 +0000 Subject: [PATCH] Tidying --- inbox.py | 85 +++++------------------------------------------------- speaker.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 83 insertions(+), 83 deletions(-) diff --git a/inbox.py b/inbox.py index fa72339c5..b47ec94e3 100644 --- a/inbox.py +++ b/inbox.py @@ -10,12 +10,7 @@ import json import os import datetime import time -import html -import urllib.parse from linked_data_sig import verifyJsonSignature -from utils import getDisplayName -from utils import getGenderFromBio -from utils import removeHtml from utils import getConfigParam from utils import hasUsersPath from utils import validPostDate @@ -82,11 +77,7 @@ from happening import saveEventPost from delete import removeOldHashtags from categories import guessHashtagCategory from context import hasValidContext -from content import htmlReplaceQuoteMarks -from speaker import speakerReplaceLinks -from speaker import speakerPronounce -from speaker import speakerEndpointJson -from speaker import removeEmojiFromText +from speaker import updateSpeaker def storeHashTags(baseDir: str, nickname: str, postJsonObject: {}) -> None: @@ -1414,9 +1405,9 @@ def _receiveAnnounce(recentPostsCache: {}, lookupActor = lookupActor.split('/statuses/')[0] if not os.path.isfile(postFilename + '.tts'): - _updateSpeaker(baseDir, nickname, domain, - postJsonObject, personCache, - translate, lookupActor) + updateSpeaker(baseDir, nickname, domain, + postJsonObject, personCache, + translate, lookupActor) ttsFile = open(postFilename + '.tts', "w+") if ttsFile: ttsFile.write('\n') @@ -2155,68 +2146,6 @@ def _bounceDM(senderPostId: str, session, httpPrefix: str, return True -def _updateSpeaker(baseDir: str, nickname: str, domain: str, - postJsonObject: {}, personCache: {}, - translate: {}, announcingActor: str) -> None: - """ Generates a json file which can be used for TTS announcement - of incoming inbox posts - """ - if not postJsonObject.get('object'): - return - if not isinstance(postJsonObject['object'], dict): - return - if not postJsonObject['object'].get('content'): - return - if not isinstance(postJsonObject['object']['content'], str): - return - speakerFilename = \ - baseDir + '/accounts/' + nickname + '@' + domain + '/speaker.json' - detectedLinks = [] - content = urllib.parse.unquote_plus(postJsonObject['object']['content']) - content = html.unescape(content) - content = content.replace('

', '').replace('

', ' ') - content = removeHtml(htmlReplaceQuoteMarks(content)) - content = speakerReplaceLinks(content, translate, detectedLinks) - content = speakerPronounce(baseDir, content, translate) - - imageDescription = '' - if postJsonObject['object'].get('attachment'): - attachList = postJsonObject['object']['attachment'] - if isinstance(attachList, list): - for img in attachList: - if not isinstance(img, dict): - continue - if img.get('name'): - if isinstance(img['name'], str): - imageDescription += \ - img['name'] + '. ' - - summary = '' - if postJsonObject['object'].get('summary'): - if isinstance(postJsonObject['object']['summary'], str): - summary = \ - urllib.parse.unquote_plus(postJsonObject['object']['summary']) - summary = html.unescape(summary) - - speakerName = \ - getDisplayName(baseDir, postJsonObject['actor'], personCache) - if not speakerName: - return - speakerName = removeEmojiFromText(speakerName) - gender = getGenderFromBio(baseDir, postJsonObject['actor'], - personCache, translate) - if announcingActor: - announcedNickname = getNicknameFromActor(announcingActor) - announcedDomain = getDomainFromActor(announcingActor) - announcedHandle = announcedNickname + '@' + announcedDomain - content = \ - translate['announces'] + ' ' + announcedHandle + '. ' + content - speakerJson = speakerEndpointJson(speakerName, summary, - content, imageDescription, - detectedLinks, gender) - saveJson(speakerJson, speakerFilename) - - def _inboxAfterInitial(recentPostsCache: {}, maxRecentPosts: int, session, keyId: str, handle: str, messageJson: {}, baseDir: str, httpPrefix: str, sendThreads: [], @@ -2552,9 +2481,9 @@ def _inboxAfterInitial(recentPostsCache: {}, maxRecentPosts: int, print('ERROR: unable to update ' + boxname + ' index') else: if boxname == 'inbox': - _updateSpeaker(baseDir, nickname, domain, - postJsonObject, personCache, - translate, None) + updateSpeaker(baseDir, nickname, domain, + postJsonObject, personCache, + translate, None) if not unitTest: if debug: print('Saving inbox post as html to cache') diff --git a/speaker.py b/speaker.py index 284938427..062c0b72f 100644 --- a/speaker.py +++ b/speaker.py @@ -7,11 +7,20 @@ __email__ = "bob@freedombone.net" __status__ = "Production" import os +import html import random +import urllib.parse from auth import createBasicAuthHeader from session import getJson +from utils import getDomainFromActor +from utils import getNicknameFromActor +from utils import getGenderFromBio +from utils import getDisplayName +from utils import removeHtml from utils import loadJson +from utils import saveJson from utils import getFullDomain +from content import htmlReplaceQuoteMarks speakerRemoveChars = ('.\n', '. ', ',', ';', '?', '!') @@ -52,7 +61,7 @@ def getSpeakerRange(displayName: str) -> int: return random.randint(300, 800) -def speakerPronounce(baseDir: str, sayText: str, translate: {}) -> str: +def _speakerPronounce(baseDir: str, sayText: str, translate: {}) -> str: """Screen readers may not always pronounce correctly, so you can have a file which specifies conversions. File should contain line items such as: @@ -171,7 +180,7 @@ def _addSSMLemphasis(sayText: str) -> str: return sayText -def removeEmojiFromText(sayText: str) -> str: +def _removeEmojiFromText(sayText: str) -> str: """Removes :emoji: from the given text """ if '*' not in sayText: @@ -222,9 +231,9 @@ def getSpeakerFromServer(baseDir: str, session, return speakerJson -def speakerEndpointJson(displayName: str, summary: str, - content: str, imageDescription: str, - links: [], gender: str) -> {}: +def _speakerEndpointJson(displayName: str, summary: str, + content: str, imageDescription: str, + links: [], gender: str) -> {}: """Returns a json endpoint for the TTS speaker """ speakerJson = { @@ -312,3 +321,65 @@ def getSSMLbox(baseDir: str, path: str, speakerJson['detectedLinks'], systemLanguage, instanceTitle, gender) + + +def updateSpeaker(baseDir: str, nickname: str, domain: str, + postJsonObject: {}, personCache: {}, + translate: {}, announcingActor: str) -> None: + """ Generates a json file which can be used for TTS announcement + of incoming inbox posts + """ + if not postJsonObject.get('object'): + return + if not isinstance(postJsonObject['object'], dict): + return + if not postJsonObject['object'].get('content'): + return + if not isinstance(postJsonObject['object']['content'], str): + return + speakerFilename = \ + baseDir + '/accounts/' + nickname + '@' + domain + '/speaker.json' + detectedLinks = [] + content = urllib.parse.unquote_plus(postJsonObject['object']['content']) + content = html.unescape(content) + content = content.replace('

', '').replace('

', ' ') + content = removeHtml(htmlReplaceQuoteMarks(content)) + content = speakerReplaceLinks(content, translate, detectedLinks) + content = _speakerPronounce(baseDir, content, translate) + + imageDescription = '' + if postJsonObject['object'].get('attachment'): + attachList = postJsonObject['object']['attachment'] + if isinstance(attachList, list): + for img in attachList: + if not isinstance(img, dict): + continue + if img.get('name'): + if isinstance(img['name'], str): + imageDescription += \ + img['name'] + '. ' + + summary = '' + if postJsonObject['object'].get('summary'): + if isinstance(postJsonObject['object']['summary'], str): + summary = \ + urllib.parse.unquote_plus(postJsonObject['object']['summary']) + summary = html.unescape(summary) + + speakerName = \ + getDisplayName(baseDir, postJsonObject['actor'], personCache) + if not speakerName: + return + speakerName = _removeEmojiFromText(speakerName) + gender = getGenderFromBio(baseDir, postJsonObject['actor'], + personCache, translate) + if announcingActor: + announcedNickname = getNicknameFromActor(announcingActor) + announcedDomain = getDomainFromActor(announcingActor) + announcedHandle = announcedNickname + '@' + announcedDomain + content = \ + translate['announces'] + ' ' + announcedHandle + '. ' + content + speakerJson = _speakerEndpointJson(speakerName, summary, + content, imageDescription, + detectedLinks, gender) + saveJson(speakerJson, speakerFilename)