From 5840b01cf623c8e5f4374077e93cb02442ae086f Mon Sep 17 00:00:00 2001 From: rra Date: Wed, 7 Jul 2021 17:20:18 +0200 Subject: [PATCH] video feed generator tailored to hugo --- index_template.md | 14 ++++ streams-feed.py | 4 +- video-feed-prototype.html | 167 +++++++++++++++++++------------------- video-feed.py | 91 +++++++++++++++++---- 4 files changed, 173 insertions(+), 103 deletions(-) create mode 100644 index_template.md diff --git a/index_template.md b/index_template.md new file mode 100644 index 0000000..d8678a3 --- /dev/null +++ b/index_template.md @@ -0,0 +1,14 @@ +--- +title: "{{ v.name }}" +date: "{{ v.published_at }}" #2021-06-10T10:46:33+02:00 +draft: false +uuid: "{{v.uuid}}" +video_duration: "{{ v.duration | duration }} " +video_channel: "{{ v.channel.display_name }}" +channel_url: "{{ v.channel.url }}" +preview_image: "{{ preview_image }}" +category: "tv" + +--- + +{{ v.description }} \ No newline at end of file diff --git a/streams-feed.py b/streams-feed.py index 5352402..08e2f6b 100644 --- a/streams-feed.py +++ b/streams-feed.py @@ -45,11 +45,11 @@ configuration = peertube.Configuration( host = host+"/api/v1" ) -client = peertube.ApiClient(configuration) +client = peertube.ApiClient(configuration) v = peertube.VideoApi(client) -response = v.videos_get(count=6, filter='local')#, tags_one_of='audio') +response = v.videos_get(count=6, filter='local')#, tags_one_of='publish') videos = response.to_dict() videos = videos['data'] diff --git a/video-feed-prototype.html b/video-feed-prototype.html index 891f7fa..c4cac22 100644 --- a/video-feed-prototype.html +++ b/video-feed-prototype.html @@ -9,7 +9,7 @@ - + @@ -200,13 +200,12 @@
-
- - - +
+ +
- 0:47:37 + 1:06:35
-
- - - +
+ +
- 4:30:16 + 0:14:39
-
- - - +
+ +
- 0:56:42 + 0:11:55
-
- - - +
+ +
- 5:07:46 + 1:31:07
-
- - - +
+ +
- 6:28:45 + 1:24:29
-
- - - +
+ +
- LIVE + 1:42:16
@@ -401,5 +390,17 @@ Radio Alhara is a radio station based in Bethlehem, Ramallah and Amman. Launched function toggleDescription(id){ document.querySelector(id).classList.toggle("collapsed"); } + function loadPlayer(id, embed_path){ + media = document.querySelector('#media-'+ id) + + var iframe = document.createElement('iframe'); + iframe.src = embed_path + '?autoplay=1&title=0' + iframe.width = 560; + iframe.height = 315; + iframe.frameBorder = 0; + iframe.sandbox = "allow-same-origin allow-scripts allow-popups" + media.appendChild(iframe) + document.querySelector('#thumb-'+ id).remove() + } \ No newline at end of file diff --git a/video-feed.py b/video-feed.py index f06d846..80e1f23 100644 --- a/video-feed.py +++ b/video-feed.py @@ -8,8 +8,12 @@ import jinja2 import json import os import datetime +import shutil +import requests +from slugify import slugify +#jinja filters & config def duration(n): """ convert '6655' in '1:50:55' @@ -18,23 +22,20 @@ def duration(n): return str(datetime.timedelta(seconds = n)) def linebreaks(text): - import re - br = re.compile(r"(\r\n|\r|\n)") - return br.sub(r"
\n", text) - -#def base64ify(url): - #download url to object - #run base64 lib on object - #format a data: string - #return base64string - + if not text: + return text + else: + import re + br = re.compile(r"(\r\n|\r|\n)") + return br.sub(r"
\n", text) + env = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.curdir) ) - env.filters['duration'] = duration env.filters['linebreaks'] = linebreaks +env.filters['slugify'] = slugify host = 'https://tv.lumbung.space' @@ -42,20 +43,74 @@ configuration = peertube.Configuration( host = host+"/api/v1" ) -client = peertube.ApiClient(configuration) +client = peertube.ApiClient(configuration) v = peertube.VideoApi(client) -response = v.videos_get(count=6, filter='local') +response = v.videos_get(count=1000, filter='local', tags_one_of='publish') videos = response.to_dict() videos = videos['data'] -template = env.get_template('video-feed.html') -html = template.render(videos=videos, host=host, description=description) +def create_post(post_directory, video_metadata): + global client + 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') + + #FIXME replace the truncated description with the full video description + api_response = peertube.VideoApi(client).videos_id_description_get(v['uuid']) + import ast + 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/ssg_experiment/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) + +#compare returned video uuid to existing posts to determine: +# video - existing post +# 1 0 > create +# 0 1 > delete +# 1 1 > ignore or update +# 0 0 > nothing + +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)) + -with open('video-feed-prototype.html','w') as f: - f.write(html) - print(html)