Download conversation posts

main
Bob Mottram 2022-12-23 19:51:00 +00:00
parent 103d114cfb
commit 5493747add
2 changed files with 125 additions and 1 deletions

View File

@ -26,6 +26,7 @@ from roles import set_role
from webfinger import webfinger_handle
from bookmarks import send_bookmark_via_server
from bookmarks import send_undo_bookmark_via_server
from posts import download_conversation_posts
from posts import set_post_expiry_days
from posts import get_instance_actor_key
from posts import send_mute_via_server
@ -183,6 +184,9 @@ def _command_options() -> None:
parser.add_argument('-n', '--nickname', dest='nickname', type=str,
default=None,
help='Nickname of the account to use')
parser.add_argument('--conversation', dest='conversation', type=str,
default=None,
help='Download a conversation for the given post id')
parser.add_argument('--screenreader', dest='screenreader', type=str,
default=None,
help='Name of the screen reader: ' +
@ -1112,7 +1116,12 @@ def _command_options() -> None:
sys.exit()
if argb.json:
session = create_session(None)
proxy_type = None
if '.onion/' in argb.json:
proxy_type = 'tor'
elif '.i2p/' in argb.json:
proxy_type = 'i2p'
session = create_session(proxy_type)
profile_str = 'https://www.w3.org/ns/activitystreams'
as_header = {
'Accept': 'application/ld+json; profile="' + profile_str + '"'
@ -1136,6 +1145,38 @@ def _command_options() -> None:
pprint(test_json)
sys.exit()
if argb.conversation:
post_id = argb.conversation
if '://' not in post_id:
print('--conversation should be the url of a post')
sys.exit()
proxy_type = None
if '.onion/' in post_id:
proxy_type = 'tor'
elif '.i2p/' in post_id:
proxy_type = 'i2p'
session = create_session(proxy_type)
if not argb.domain:
argb.domain = get_config_param(base_dir, 'domain')
domain = ''
if argb.domain:
domain = argb.domain
if not domain:
print('Please specify a domain with the --domain option')
sys.exit()
nickname = ''
if argb.nickname:
nickname = argb.nickname
if not nickname:
print('Please specify a nickname with the --nickname option')
sys.exit()
conv_json = download_conversation_posts(session, http_prefix,
base_dir, nickname, domain,
post_id, argb.debug)
if conv_json:
pprint(conv_json)
sys.exit()
if argb.ssml:
session = create_session(None)
profile_str = 'https://www.w3.org/ns/activitystreams'

View File

@ -6035,3 +6035,86 @@ def set_max_profile_posts(base_dir: str, nickname: str, domain: str,
max_posts_filename)
return False
return True
def download_conversation_posts(session, http_prefix: str, base_dir: str,
nickname: str, domain: str,
post_id: str, debug: bool) -> []:
"""Downloads all posts for a conversation and returns a list of the
json objects
"""
if '://' not in post_id:
return []
profile_str = 'https://www.w3.org/ns/activitystreams'
as_header = {
'Accept': 'application/ld+json; profile="' + profile_str + '"'
}
conversation_thread = []
signing_priv_key_pem = get_instance_actor_key(base_dir, domain)
post_id = remove_id_ending(post_id)
post_filename = \
locate_post(base_dir, nickname, domain, post_id)
if post_filename:
post_json = load_json(post_filename)
else:
post_json = get_json(signing_priv_key_pem, session, post_id,
as_header, None, debug, __version__,
http_prefix, domain)
if debug:
if not post_json:
print(post_id + ' returned no json')
while post_json:
if not has_object_dict(post_json):
if not post_json.get('attributedTo'):
print(str(post_json))
if debug:
print(post_id + ' has no attributedTo')
break
if not isinstance(post_json['attributedTo'], str):
break
if not post_json.get('published'):
if debug:
print(post_id + ' has no published date')
break
if not post_json.get('to'):
if debug:
print(post_id + ' has no "to" list')
break
if not isinstance(post_json['to'], list):
break
if 'cc' not in post_json:
if debug:
print(post_id + ' has no "cc" list')
break
if not isinstance(post_json['cc'], list):
break
wrapped_post = {
"@context": "https://www.w3.org/ns/activitystreams",
'id': post_id + '/activity',
'type': 'Create',
'actor': post_json['attributedTo'],
'published': post_json['published'],
'to': post_json['to'],
'cc': post_json['cc'],
'object': post_json
}
post_json = wrapped_post
conversation_thread.append(post_json)
if not post_json['object'].get('inReplyTo'):
if debug:
print(post_id + ' is not a reply')
break
post_id = post_json['object']['inReplyTo']
post_id = remove_id_ending(post_id)
post_filename = \
locate_post(base_dir, nickname, domain, post_id)
if post_filename:
post_json = load_json(post_filename)
else:
post_json = get_json(signing_priv_key_pem, session, post_id,
as_header, None, debug, __version__,
http_prefix, domain)
if debug:
if not post_json:
print(post_id + ' returned no json')
return conversation_thread