Added functionality to use PeerTube HTTP import

Depending on the config, the tool no longer needs to download and upload videos if the user does not want to.
This commit is contained in:
mister-monster 2019-10-21 22:22:40 -05:00 committed by GitHub
parent 8b72d4c947
commit f5a5ca46f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -139,18 +139,17 @@ def get_pt_channel_id(channel_conf):
channel_id = returned_json["id"] channel_id = returned_json["id"]
return channel_id return channel_id
def get_file(file_path):
mimetypes.init()
return (path.basename(file_path), open(path.abspath(file_path), 'rb'),
mimetypes.types_map[path.splitext(file_path)[1]])
def upload_to_pt(dl_dir, channel_conf, e, access_token, thumb_extension): def upload_to_pt(dl_dir, channel_conf, e, access_token, thumb_extension):
# Adapted from Prismedia https://git.lecygnenoir.info/LecygneNoir/prismedia # Adapted from Prismedia https://git.lecygnenoir.info/LecygneNoir/prismedia
pt_api = channel_conf["peertube_instance"] + "/api/v1" pt_api = channel_conf["peertube_instance"] + "/api/v1"
video_file = dl_dir + channel_conf["name"] + "/" + e["yt_videoid"] + "." + \ video_file = dl_dir + channel_conf["name"] + "/" + e["yt_videoid"] + "." + \
channel_conf["preferred_extension"] channel_conf["preferred_extension"]
thumb_file = dl_dir + channel_conf["name"] + "/" + e["yt_videoid"] + "." + thumb_extension thumb_file = dl_dir + channel_conf["name"] + "/" + e["yt_videoid"] + "." + thumb_extension
def get_file(file_path):
mimetypes.init()
return (path.basename(file_path), open(path.abspath(file_path), 'rb'),
mimetypes.types_map[path.splitext(file_path)[1]])
description = channel_conf["description_prefix"] + "\n\n" + e["summary"] + "\n\n" + channel_conf["description_suffix"] description = channel_conf["description_prefix"] + "\n\n" + e["summary"] + "\n\n" + channel_conf["description_suffix"]
channel_id = str(get_pt_channel_id(channel_conf)) channel_id = str(get_pt_channel_id(channel_conf))
# We need to transform fields into tuple to deal with tags as # We need to transform fields into tuple to deal with tags as
@ -185,6 +184,45 @@ def upload_to_pt(dl_dir, channel_conf, e, access_token, thumb_extension):
} }
print(requests.post(pt_api + "/videos/upload", data=multipart_data, headers=headers).content) print(requests.post(pt_api + "/videos/upload", data=multipart_data, headers=headers).content)
def pt_http_import(dl_dir, channel_conf, e, access_token, thumb_extension):
# Adapted from Prismedia https://git.lecygnenoir.info/LecygneNoir/prismedia
pt_api = channel_conf["peertube_instance"] + "/api/v1"
yt_video_url = e["link"]
thumb_file = dl_dir + channel_conf["name"] + "/" + e["yt_videoid"] + "." + thumb_extension
description = channel_conf["description_prefix"] + "\n\n" + e["summary"] + "\n\n" + channel_conf["description_suffix"]
channel_id = str(get_pt_channel_id(channel_conf))
# We need to transform fields into tuple to deal with tags as
# MultipartEncoder does not support list refer
# https://github.com/requests/toolbelt/issues/190 and
# https://github.com/requests/toolbelt/issues/205
fields = [
("name", e["title"]),
("licence", "1"),
("description", description),
("nsfw", channel_conf["nsfw"]),
("channelId", channel_id),
("originallyPublishedAt", e["published"]),
("category", channel_conf["pt_channel_category"]),
("lanmguage", channel_conf["default_lang"]),
("privacy", str(channel_conf["pt_privacy"])),
("commentsEnabled", channel_conf["comments_enabled"]),
("targetUrl", yt_video_url),
("thumbnailfile", get_file(thumb_file)),
("previewfile", get_file(thumb_file)),
("waitTranscoding", 'false')
]
if channel_conf["pt_tags"] != "":
fields.append(("tags", "[" + channel_conf["pt_tags"] + "]"))
else:
print("you have no tags in your configuration file for this channel")
multipart_data = MultipartEncoder(fields)
headers = {
'Content-Type': multipart_data.content_type,
'Authorization': "Bearer " + access_token
}
print(requests.post(pt_api + "/videos/imports", data=multipart_data, headers=headers).content)
def run_steps(conf): def run_steps(conf):
# TODO: logging # TODO: logging
channel = conf["channel"] channel = conf["channel"]
@ -194,6 +232,12 @@ def run_steps(conf):
delete_videos = True delete_videos = True
else: else:
delete_videos = False delete_videos = False
# The following enables the deletion of thumbnails, videos are not downloaded at all
if conf["global"]["use_pt_http_import"] == "true":
delete_videos = True
use_pt_http_import = True
else:
use_pt_http_import = False
dl_dir = global_conf["video_download_dir"] dl_dir = global_conf["video_download_dir"]
if not path.exists(dl_dir): if not path.exists(dl_dir):
mkdir(dl_dir) mkdir(dl_dir)
@ -208,6 +252,7 @@ def run_steps(conf):
mkdir(dl_dir + "/" + channel_conf["name"]) mkdir(dl_dir + "/" + channel_conf["name"])
# download videos, metadata and thumbnails from youtube # download videos, metadata and thumbnails from youtube
for item in queue: for item in queue:
if not use_pt_http_import:
print("downloading " + item["yt_videoid"] + " from YouTube...") print("downloading " + item["yt_videoid"] + " from YouTube...")
download_yt_video(item, dl_dir, channel_conf) download_yt_video(item, dl_dir, channel_conf)
print("done.") print("done.")
@ -221,11 +266,15 @@ def run_steps(conf):
access_token = get_pt_auth(channel_conf) access_token = get_pt_auth(channel_conf)
# upload videos, metadata and thumbnails to peertube # upload videos, metadata and thumbnails to peertube
for item in queue: for item in queue:
if not use_pt_http_import:
print("uploading " + item["yt_videoid"] + " to Peertube...") print("uploading " + item["yt_videoid"] + " to Peertube...")
upload_to_pt(dl_dir, channel_conf, item, access_token, thumb_extension) upload_to_pt(dl_dir, channel_conf, item, access_token, thumb_extension)
print("done.") print("done.")
else:
print("mirroring " + item["link"] + " to Peertube using HTTP import...")
pt_http_import(dl_dir, channel_conf, item, access_token, thumb_extension)
if delete_videos: if delete_videos:
print("deleting videos...") print("deleting videos and/or thumbnails...")
rmtree(dl_dir + "/" + channel_conf["name"], ignore_errors=True) rmtree(dl_dir + "/" + channel_conf["name"], ignore_errors=True)
print("done") print("done")
channel_counter += 1 channel_counter += 1