Send posts using retries

master
Bob Mottram 2019-06-30 14:20:23 +01:00
parent d3e005050e
commit 11046d2165
1 changed files with 21 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import commentjson
import html import html
import datetime import datetime
import os, shutil import os, shutil
import threading
from pprint import pprint from pprint import pprint
from random import randint from random import randint
from session import getJson from session import getJson
@ -24,6 +25,9 @@ try:
except ImportError: except ImportError:
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
# Contains threads for posts being sent
sendThreads = []
def permitted(url: str,federationList) -> bool: def permitted(url: str,federationList) -> bool:
"""Is a url from one of the permitted domains? """Is a url from one of the permitted domains?
""" """
@ -294,6 +298,15 @@ def createPublicPost(username: str, domain: str, https: bool, content: str, foll
prefix='http' prefix='http'
return createPostBase(username, domain, 'https://www.w3.org/ns/activitystreams#Public', prefix+'://'+domain+'/users/'+username+'/followers', https, content, followersOnly, saveToFile, inReplyTo, inReplyToAtomUri, subject) return createPostBase(username, domain, 'https://www.w3.org/ns/activitystreams#Public', prefix+'://'+domain+'/users/'+username+'/followers', https, content, followersOnly, saveToFile, inReplyTo, inReplyToAtomUri, subject)
def threadSendPost(session,postJsonObject,federationList,inboxUrl: str,signatureHeader) -> None:
tries=0
backoffTime=600
for attempt in range(12):
if postJson(session,postJsonObject,federationList,inboxUrl,signatureHeader):
break
time.sleep(backoffTime)
backoffTime *= 2
def sendPost(session,username: str, domain: str, toUsername: str, toDomain: str, cc: str, https: bool, content: str, followersOnly: bool, saveToFile: bool, federationList, inReplyTo=None, inReplyToAtomUri=None, subject=None) -> int: def sendPost(session,username: str, domain: str, toUsername: str, toDomain: str, cc: str, https: bool, content: str, followersOnly: bool, saveToFile: bool, federationList, inReplyTo=None, inReplyToAtomUri=None, subject=None) -> int:
"""Post to another inbox """Post to another inbox
""" """
@ -325,9 +338,14 @@ def sendPost(session,username: str, domain: str, toUsername: str, toDomain: str,
signatureHeader = signPostHeaders(privateKeyPem, username, domain, '/inbox', https, postJsonObject) signatureHeader = signPostHeaders(privateKeyPem, username, domain, '/inbox', https, postJsonObject)
signatureHeader['Content-type'] = 'application/json' signatureHeader['Content-type'] = 'application/json'
# TODO this should be replaced by a send buffer # Keep the number of threads being used small
postJson(session,postJsonObject,federationList,inboxUrl,signatureHeader) if len(sendThreads)>10:
thr=sendThreads[0]
thr.stop()
sendThreads.remove(thr)
thr = threading.Thread(target=threadSendPost,args=(session,postJsonObject,federationList,inboxUrl,signatureHeader),daemon=True)
sendThreads.append(thr)
thr.start()
return 0 return 0
def createOutbox(username: str,domain: str,https: bool,itemsPerPage: int,headerOnly: bool,pageNumber=None) -> {}: def createOutbox(username: str,domain: str,https: bool,itemsPerPage: int,headerOnly: bool,pageNumber=None) -> {}: