You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

110 lines
3.0 KiB

#!/bin/python3
#lumbung.space video feed generator
#c 2021 roel roscam abbing gpvl3 etc
import peertube
import jinja2
import json
import os
import datetime
import shutil
import requests
import ast
#jinja filters & config
def duration(n):
"""
convert '6655' in '1:50:55'
"""
return str(datetime.timedelta(seconds = n))
def linebreaks(text):
if not text:
return text
else:
import re
br = re.compile(r"(\r\n|\r|\n)")
return br.sub(r"<br />\n", text)
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.curdir)
)
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):
global client #lazy
if not os.path.exists(post_dir):
os.mkdir(post_directory)
preview_image = video_metadata['preview_path'].split('/')[-1]
if not os.path.exists(os.path.join(post_directory, preview_image)):
#download preview image
response = requests.get(host+video_metadata['preview_path'], stream=True)
with open(os.path.join(post_directory, preview_image), 'wb') as img_file:
shutil.copyfileobj(response.raw, img_file)
print('got image')
else:
print('image exists')
#replace the truncated description with the full video description
#peertube api is some broken thing in between a py dict and a json file
api_response = peertube.VideoApi(client).videos_id_description_get(v['uuid'])
long_description = ast.literal_eval(api_response)
v['description'] = long_description['description']
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)
print(video_metadata['uuid'], 'written')
output_dir = '/home/r/Programming/lumbung.space/lumbung.space-web/content/video'
if not os.path.exists(output_dir):
os.mkdir(output_dir)
template = env.get_template('index_template.md')
existing_posts = os.listdir(output_dir)
for v in videos:
if v['uuid'] not in existing_posts: #if there is a video we dont already have, make it
post_dir = os.path.join(output_dir,v['uuid'])
create_post(post_dir, v)
elif v['uuid'] in existing_posts: # if we already have the video do nothing, possibly update
print(v['uuid'],'already exists')
existing_posts.remove(v['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))