cli commands
This commit is contained in:
@ -1,8 +1,3 @@
|
||||
#!/bin/python3
|
||||
|
||||
# lumbung.space video feed generator
|
||||
# c 2021 roel roscam abbing gpvl3 etc
|
||||
|
||||
import ast
|
||||
import datetime
|
||||
import json
|
||||
@ -15,6 +10,9 @@ import jinja2
|
||||
import peertube
|
||||
import requests
|
||||
|
||||
host = "https://tv.lumbung.space"
|
||||
configuration = peertube.Configuration(host=host + "/api/v1")
|
||||
client = peertube.ApiClient(configuration)
|
||||
|
||||
# jinja filters & config
|
||||
def duration(n):
|
||||
@ -35,29 +33,10 @@ def linebreaks(text):
|
||||
return br.sub(r"<br />\n", text)
|
||||
|
||||
|
||||
template_dir = os.path.join(Path(__file__).parent.resolve(), "templates")
|
||||
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
|
||||
env.filters["duration"] = duration
|
||||
env.filters["linebreaks"] = linebreaks
|
||||
|
||||
host = "https://tv.lumbung.space"
|
||||
|
||||
configuration = peertube.Configuration(host=host + "/api/v1")
|
||||
|
||||
client = peertube.ApiClient(configuration)
|
||||
|
||||
v = peertube.VideoApi(client)
|
||||
|
||||
response = v.videos_get(count=100, filter="local", tags_one_of="publish")
|
||||
|
||||
videos = response.to_dict()
|
||||
videos = videos["data"]
|
||||
|
||||
|
||||
def create_post(post_directory, video_metadata):
|
||||
def create_post(post_directory, video_metadata, host):
|
||||
global client # lazy
|
||||
|
||||
if not os.path.exists(post_dir):
|
||||
if not os.path.exists(post_directory):
|
||||
os.mkdir(post_directory)
|
||||
|
||||
preview_image = video_metadata["preview_path"].split("/")[-1]
|
||||
@ -77,6 +56,12 @@ def create_post(post_directory, video_metadata):
|
||||
long_description = ast.literal_eval(api_response)
|
||||
video_metadata["description"] = long_description["description"]
|
||||
|
||||
template_dir = os.path.join(Path(__file__).parent.resolve(), "templates")
|
||||
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))
|
||||
env.filters["duration"] = duration
|
||||
env.filters["linebreaks"] = linebreaks
|
||||
template = env.get_template("video.md")
|
||||
|
||||
with open(os.path.join(post_directory, "index.md"), "w") as f:
|
||||
post = template.render(v=video_metadata, host=host, preview_image=preview_image)
|
||||
f.write(post)
|
||||
@ -86,7 +71,7 @@ def create_post(post_directory, video_metadata):
|
||||
f.write(timestamp.format("X"))
|
||||
|
||||
|
||||
def update_post(post_directory, video_metadata):
|
||||
def update_post(post_directory, video_metadata, host):
|
||||
if os.path.exists(post_directory):
|
||||
if os.path.exists(os.path.join(post_directory, ".timestamp")):
|
||||
old_timestamp = open(os.path.join(post_directory, ".timestamp")).read()
|
||||
@ -101,7 +86,7 @@ def update_post(post_directory, video_metadata):
|
||||
video_metadata["name"],
|
||||
"({})".format(video_metadata["uuid"]),
|
||||
)
|
||||
create_post(post_dir, video_metadata)
|
||||
create_post(post_directory, video_metadata, host)
|
||||
else:
|
||||
print(
|
||||
"Video current: ",
|
||||
@ -110,37 +95,43 @@ def update_post(post_directory, video_metadata):
|
||||
)
|
||||
else:
|
||||
# compat for when there is no timestamp yet..
|
||||
create_post(post_dir, video_metadata)
|
||||
create_post(post_directory, video_metadata, host)
|
||||
|
||||
|
||||
output_dir = os.environ.get(
|
||||
"OUTPUT_DIR", "/home/r/Programming/lumbung.space/lumbung.space-web/content/video"
|
||||
)
|
||||
def main():
|
||||
v = peertube.VideoApi(client)
|
||||
|
||||
if not os.path.exists(output_dir):
|
||||
os.mkdir(output_dir)
|
||||
response = v.videos_get(count=100, filter="local", tags_one_of="publish")
|
||||
|
||||
template = env.get_template("video.md")
|
||||
videos = response.to_dict()
|
||||
videos = videos["data"]
|
||||
|
||||
existing_posts = os.listdir(output_dir)
|
||||
output_dir = os.environ.get("OUTPUT_DIR")
|
||||
|
||||
for video_metadata in videos:
|
||||
post_dir = os.path.join(output_dir, video_metadata["uuid"])
|
||||
if not os.path.exists(output_dir):
|
||||
os.mkdir(output_dir)
|
||||
|
||||
if (
|
||||
video_metadata["uuid"] not in existing_posts
|
||||
): # if there is a video we dont already have, make it
|
||||
print("New: ", video_metadata["name"], "({})".format(video_metadata["uuid"]))
|
||||
create_post(post_dir, video_metadata)
|
||||
existing_posts = os.listdir(output_dir)
|
||||
|
||||
elif (
|
||||
video_metadata["uuid"] in existing_posts
|
||||
): # if we already have the video do nothing, possibly update
|
||||
update_post(post_dir, video_metadata)
|
||||
existing_posts.remove(
|
||||
video_metadata["uuid"]
|
||||
) # create list of posts which have not been returned by peertube
|
||||
for video_metadata in videos:
|
||||
post_dir = os.path.join(output_dir, video_metadata["uuid"])
|
||||
|
||||
for post in existing_posts:
|
||||
print("deleted", post) # rm posts not returned
|
||||
shutil.rmtree(os.path.join(output_dir, post))
|
||||
if (
|
||||
video_metadata["uuid"] not in existing_posts
|
||||
): # if there is a video we dont already have, make it
|
||||
print(
|
||||
"New: ", video_metadata["name"], "({})".format(video_metadata["uuid"])
|
||||
)
|
||||
create_post(post_dir, video_metadata, host)
|
||||
|
||||
elif (
|
||||
video_metadata["uuid"] in existing_posts
|
||||
): # if we already have the video do nothing, possibly update
|
||||
update_post(post_dir, video_metadata, host)
|
||||
existing_posts.remove(
|
||||
video_metadata["uuid"]
|
||||
) # create list of posts which have not been returned by peertube
|
||||
|
||||
for post in existing_posts:
|
||||
print("deleted", post) # rm posts not returned
|
||||
shutil.rmtree(os.path.join(output_dir, post))
|
||||
|
Reference in New Issue
Block a user