Bencoded torrents

main
Bob Mottram 2025-11-10 15:15:13 +00:00
parent 91c24f224b
commit fcf626607a
4 changed files with 32 additions and 17 deletions

View File

@ -103,7 +103,7 @@ def convert_torrent_to_note(base_dir: str, nickname: str, domain: str,
conversation_id = post_id_to_convthread_id(conversation_id, conversation_id = post_id_to_convthread_id(conversation_id,
post_json_object['published']) post_json_object['published'])
media_type, media_url, media_torrent, media_magnet = \ media_type, media_url, media_torrent, media_magnet, media_bencoded = \
get_media_url_from_torrent(post_json_object) get_media_url_from_torrent(post_json_object)
if not media_url: if not media_url:
@ -131,9 +131,9 @@ def convert_torrent_to_note(base_dir: str, nickname: str, domain: str,
if isinstance(post_json_object['cc'], list): if isinstance(post_json_object['cc'], list):
cc = post_json_object['cc'] cc = post_json_object['cc']
if media_torrent or media_magnet: if media_torrent or media_magnet or media_bencoded:
content += '<p>' content += '<p>'
if media_torrent: if media_torrent or media_bencoded:
content += '<a href="' + media_torrent + '">⇓</a> ' content += '<a href="' + media_torrent + '">⇓</a> '
if media_magnet: if media_magnet:
content += '<a href="' + media_magnet + '">🧲</a>' content += '<a href="' + media_magnet + '">🧲</a>'

View File

@ -3589,17 +3589,21 @@ def lines_in_file(filename: str) -> int:
return 0 return 0
def get_media_url_from_video(post_json_object: {}) -> (str, str, str, str): def get_media_url_from_video(post_json_object: {}) -> (str, str, str,
str, str):
"""Within a Video post (eg peertube) return the media details """Within a Video post (eg peertube) return the media details
""" """
media_type = None media_type = None
media_url = None media_url = None
media_torrent = None media_torrent = None
media_magnet = None media_magnet = None
media_bencoded = None
if not post_json_object.get('url'): if not post_json_object.get('url'):
return media_type, media_url, media_torrent, media_magnet return media_type, media_url, media_torrent, \
media_magnet, media_bencoded
if not isinstance(post_json_object['url'], list): if not isinstance(post_json_object['url'], list):
return media_type, media_url, media_torrent, media_magnet return media_type, media_url, media_torrent, \
media_magnet, media_bencoded
for media_link in post_json_object['url']: for media_link in post_json_object['url']:
if not isinstance(media_link, dict): if not isinstance(media_link, dict):
continue continue
@ -3628,26 +3632,32 @@ def get_media_url_from_video(post_json_object: {}) -> (str, str, str, str):
media_torrent = remove_html(media_link['href']) media_torrent = remove_html(media_link['href'])
if media_link['href'].startswith('magnet:'): if media_link['href'].startswith('magnet:'):
media_magnet = remove_html(media_link['href']) media_magnet = remove_html(media_link['href'])
elif media_link['href'].startswith('bencoded:'):
media_bencoded = remove_html(media_link['href'])
if media_link['mediaType'] != 'video/mp4' and \ if media_link['mediaType'] != 'video/mp4' and \
media_link['mediaType'] != 'video/ogv': media_link['mediaType'] != 'video/ogv':
continue continue
if not media_url: if not media_url:
media_type = media_link['mediaType'] media_type = media_link['mediaType']
media_url = remove_html(media_link['href']) media_url = remove_html(media_link['href'])
return media_type, media_url, media_torrent, media_magnet return media_type, media_url, media_torrent, media_magnet, media_bencoded
def get_media_url_from_torrent(post_json_object: {}) -> (str, str, str, str): def get_media_url_from_torrent(post_json_object: {}) -> (str, str, str,
str, str):
"""Within a Torrent post return the media details """Within a Torrent post return the media details
""" """
media_type = None media_type = None
media_url = None media_url = None
media_torrent = None media_torrent = None
media_magnet = None media_magnet = None
media_bencoded = None
if not post_json_object.get('url'): if not post_json_object.get('url'):
return media_type, media_url, media_torrent, media_magnet return media_type, media_url, media_torrent, \
media_magnet, media_bencoded
if not isinstance(post_json_object['url'], list): if not isinstance(post_json_object['url'], list):
return media_type, media_url, media_torrent, media_magnet return media_type, media_url, media_torrent, \
media_magnet, media_bencoded
for media_link in post_json_object['url']: for media_link in post_json_object['url']:
if not isinstance(media_link, dict): if not isinstance(media_link, dict):
continue continue
@ -3666,9 +3676,12 @@ def get_media_url_from_torrent(post_json_object: {}) -> (str, str, str, str):
if not tag_link.get('href'): if not tag_link.get('href'):
continue continue
if tag_link['mediaType'] == 'application/x-bittorrent' or \ if tag_link['mediaType'] == 'application/x-bittorrent' or \
tag_link['mediaType'].startswith('magnet:'): tag_link['mediaType'].startswith('magnet:') or \
tag_link['mediaType'].startswith('bencoded:'):
if tag_link['mediaType'].startswith('magnet:'): if tag_link['mediaType'].startswith('magnet:'):
media_magnet = remove_html(media_link['href']) media_magnet = remove_html(media_link['href'])
elif tag_link['mediaType'].startswith('bencoded:'):
media_bencoded = remove_html(media_link['href'])
else: else:
media_torrent = remove_html(media_link['href']) media_torrent = remove_html(media_link['href'])
media_type = tag_link['mediaType'] media_type = tag_link['mediaType']
@ -3680,13 +3693,15 @@ def get_media_url_from_torrent(post_json_object: {}) -> (str, str, str, str):
media_torrent = remove_html(media_link['href']) media_torrent = remove_html(media_link['href'])
if media_link['href'].startswith('magnet:'): if media_link['href'].startswith('magnet:'):
media_magnet = remove_html(media_link['href']) media_magnet = remove_html(media_link['href'])
elif media_link['href'].startswith('bencoded:'):
media_bencoded = remove_html(media_link['href'])
if media_link['mediaType'] != 'video/mp4' and \ if media_link['mediaType'] != 'video/mp4' and \
media_link['mediaType'] != 'video/ogv': media_link['mediaType'] != 'video/ogv':
continue continue
if not media_url: if not media_url:
media_type = media_link['mediaType'] media_type = media_link['mediaType']
media_url = remove_html(media_link['href']) media_url = remove_html(media_link['href'])
return media_type, media_url, media_torrent, media_magnet return media_type, media_url, media_torrent, media_magnet, media_bencoded
def get_reply_to(post_json_object: {}) -> str: def get_reply_to(post_json_object: {}) -> str:

View File

@ -104,7 +104,7 @@ def convert_video_to_note(base_dir: str, nickname: str, domain: str,
conversation_id = post_id_to_convthread_id(conversation_id, conversation_id = post_id_to_convthread_id(conversation_id,
post_json_object['published']) post_json_object['published'])
media_type, media_url, media_torrent, media_magnet = \ media_type, media_url, media_torrent, media_magnet, media_bencoded = \
get_media_url_from_video(post_json_object) get_media_url_from_video(post_json_object)
if not media_url: if not media_url:
@ -117,9 +117,9 @@ def convert_video_to_note(base_dir: str, nickname: str, domain: str,
'url': media_url 'url': media_url
}] }]
if media_torrent or media_magnet: if media_torrent or media_magnet or media_bencoded:
content += '<p>' content += '<p>'
if media_torrent: if media_torrent or media_bencoded:
content += '<a href="' + media_torrent + '">⇓</a> ' content += '<a href="' + media_torrent + '">⇓</a> '
if media_magnet: if media_magnet:
content += '<a href="' + media_magnet + '">🧲</a>' content += '<a href="' + media_magnet + '">🧲</a>'

View File

@ -1274,10 +1274,10 @@ def get_post_attachments_as_html(base_dir: str,
# handle peertube-style video posts, where the media links # handle peertube-style video posts, where the media links
# are stored in the url field # are stored in the url field
if post_json_object.get('object'): if post_json_object.get('object'):
media_type, media_url, _, _ = \ media_type, media_url, _, _, _ = \
get_media_url_from_video(post_json_object['object']) get_media_url_from_video(post_json_object['object'])
else: else:
media_type, media_url, _, _ = \ media_type, media_url, _, _, _ = \
get_media_url_from_video(post_json_object) get_media_url_from_video(post_json_object)
if media_url and media_type: if media_url and media_type:
attachment_dict = [{ attachment_dict = [{