From ba9d9a830112453b3b87b18dbabf66f2858464ec Mon Sep 17 00:00:00 2001 From: Tjiho Date: Sat, 4 Jan 2020 16:44:58 +0100 Subject: [PATCH 1/3] return error when resquest fail --- youtube2peertube.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/youtube2peertube.py b/youtube2peertube.py index 01db295..83addfa 100644 --- a/youtube2peertube.py +++ b/youtube2peertube.py @@ -146,6 +146,14 @@ def get_file(file_path): return (path.basename(file_path), open(path.abspath(file_path), 'rb'), mimetypes.types_map[path.splitext(file_path)[1]]) + +def handle_peertube_result(request_result): + if request_result.status_code < 300: + return True + else: + print(request_result) + return False + def upload_to_pt(dl_dir, channel_conf, queue_item, access_token, thumb_extension): # Adapted from Prismedia https://git.lecygnenoir.info/LecygneNoir/prismedia pt_api = channel_conf["peertube_instance"] + "/api/v1" @@ -184,7 +192,8 @@ def upload_to_pt(dl_dir, channel_conf, queue_item, access_token, thumb_extension 'Content-Type': multipart_data.content_type, 'Authorization': "Bearer " + access_token } - print(requests.post(pt_api + "/videos/upload", data=multipart_data, headers=headers).content) + + return handle_peertube_result(requests.post(pt_api + "/videos/upload", data=multipart_data, headers=headers)) def pt_http_import(dl_dir, channel_conf, queue_item, access_token, thumb_extension, yt_lang): # Adapted from Prismedia https://git.lecygnenoir.info/LecygneNoir/prismedia @@ -226,7 +235,10 @@ def pt_http_import(dl_dir, channel_conf, queue_item, access_token, thumb_extensi 'Content-Type': multipart_data.content_type, 'Authorization': "Bearer " + access_token } - print(requests.post(pt_api + "/videos/imports", data=multipart_data, headers=headers).content) + + return handle_peertube_result(requests.post(pt_api + "/videos/imports", data=multipart_data, headers=headers)) + + def run_steps(conf): # TODO: logging @@ -275,12 +287,16 @@ def run_steps(conf): for queue_item in queue: if not use_pt_http_import: print("uploading " + queue_item["yt_videoid"] + " to Peertube...") - upload_to_pt(dl_dir, channel_conf, queue_item, access_token, thumb_extension) - print("done.") + pt_result = upload_to_pt(dl_dir, channel_conf, queue_item, access_token, thumb_extension) + else: print("mirroring " + queue_item["link"] + " to Peertube using HTTP import...") - pt_http_import(dl_dir, channel_conf, queue_item, access_token, thumb_extension, yt_lang) - print("done.") + pt_result = pt_http_import(dl_dir, channel_conf, queue_item, access_token, thumb_extension, yt_lang) + + if pt_result: + print("done !") + else: + print("Error !") if delete_videos: print("deleting videos and/or thumbnails...") rmtree(dl_dir + "/" + channel_conf["name"], ignore_errors=True) From a6469621b7ec012c0bc5d00ce188012f2ca46530 Mon Sep 17 00:00:00 2001 From: Tjiho Date: Sat, 4 Jan 2020 17:05:36 +0100 Subject: [PATCH 2/3] write upload error in a file --- youtube2peertube.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/youtube2peertube.py b/youtube2peertube.py index 83addfa..f93ed34 100644 --- a/youtube2peertube.py +++ b/youtube2peertube.py @@ -239,6 +239,11 @@ def pt_http_import(dl_dir, channel_conf, queue_item, access_token, thumb_extensi return handle_peertube_result(requests.post(pt_api + "/videos/imports", data=multipart_data, headers=headers)) +def log_upload_error(yt_url): + error_file = open("video_errors.txt", "a") + error_file.write(yt_url+"\n") + error_file.close() + print("error !") def run_steps(conf): # TODO: logging @@ -296,7 +301,7 @@ def run_steps(conf): if pt_result: print("done !") else: - print("Error !") + log_upload_error(queue_item["link"]) if delete_videos: print("deleting videos and/or thumbnails...") rmtree(dl_dir + "/" + channel_conf["name"], ignore_errors=True) From 7af4e6221bf0dc60bebc4a7df8e3287135d83df6 Mon Sep 17 00:00:00 2001 From: Tjiho Date: Sun, 5 Jan 2020 01:31:23 +0100 Subject: [PATCH 3/3] add channel name in error file --- youtube2peertube.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/youtube2peertube.py b/youtube2peertube.py index f93ed34..e478b46 100644 --- a/youtube2peertube.py +++ b/youtube2peertube.py @@ -239,9 +239,9 @@ def pt_http_import(dl_dir, channel_conf, queue_item, access_token, thumb_extensi return handle_peertube_result(requests.post(pt_api + "/videos/imports", data=multipart_data, headers=headers)) -def log_upload_error(yt_url): - error_file = open("video_errors.txt", "a") - error_file.write(yt_url+"\n") +def log_upload_error(yt_url,channel_conf): + error_file = open("video_errors.csv", "a") + error_file.write(channel_conf['name']+","+yt_url+"\n") error_file.close() print("error !") @@ -301,7 +301,7 @@ def run_steps(conf): if pt_result: print("done !") else: - log_upload_error(queue_item["link"]) + log_upload_error(queue_item["link"],channel_conf) if delete_videos: print("deleting videos and/or thumbnails...") rmtree(dl_dir + "/" + channel_conf["name"], ignore_errors=True)