From 23b20a8a58054c4d5dd664ac82aae08ddbf30a05 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 29 Oct 2023 22:00:04 +0000 Subject: [PATCH] Tidying --- utils.py | 28 ++++++++++++++++++++++++++++ video.py | 24 +++--------------------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/utils.py b/utils.py index d5d52f18d..59822faf0 100644 --- a/utils.py +++ b/utils.py @@ -4677,3 +4677,31 @@ def corp_servers() -> (): """ return ('GitHub.com', 'github.com', 'cloudflare', 'microsoft.com', 'google.com') + + +def get_media_url_from_video(post_json_object: {}) -> (str, str, str, str): + """Within a Video post (eg peertube) return the media details + """ + media_type = None + media_url = None + media_torrent = None + media_magnet = None + if isinstance(post_json_object['url'], list): + for media_link in post_json_object['url']: + if not isinstance(media_link, dict): + continue + if not media_link.get('mediaType'): + continue + if not media_link.get('href'): + continue + if media_link['mediaType'] == 'application/x-bittorrent': + media_torrent = remove_html(media_link['href']) + if media_link['href'].startswith('magnet:'): + media_magnet = remove_html(media_link['href']) + if media_link['mediaType'] != 'video/mp4' and \ + media_link['mediaType'] != 'video/ogv': + continue + if not media_url: + media_type = media_link['mediaType'] + media_url = remove_html(media_link['href']) + return media_type, media_url, media_torrent, media_magnet diff --git a/video.py b/video.py index 266021a0e..38edfdb7a 100644 --- a/video.py +++ b/video.py @@ -16,6 +16,7 @@ from utils import get_attributed_to from utils import get_content_from_post from utils import dangerous_markup from utils import license_link_from_name +from utils import get_media_url_from_video from blocking import is_blocked from filters import is_filtered @@ -91,27 +92,8 @@ def convert_video_to_note(base_dir: str, nickname: str, domain: str, conversation_id = remove_id_ending(post_json_object['id']) - media_type = None - media_url = None - media_torrent = None - media_magnet = None - for media_link in post_json_object['url']: - if not isinstance(media_link, dict): - continue - if not media_link.get('mediaType'): - continue - if not media_link.get('href'): - continue - if media_link['mediaType'] == 'application/x-bittorrent': - media_torrent = remove_html(media_link['href']) - if media_link['href'].startswith('magnet:'): - media_magnet = remove_html(media_link['href']) - if media_link['mediaType'] != 'video/mp4' and \ - media_link['mediaType'] != 'video/ogv': - continue - if not media_url: - media_type = media_link['mediaType'] - media_url = remove_html(media_link['href']) + media_type, media_url, media_torrent, media_magnet = \ + get_media_url_from_video(post_json_object) if not media_url: return None