Browse Source

add update logic

master
rra 3 years ago
parent
commit
7a73acfbd6
  1. 33
      video-feed.py

33
video-feed.py

@ -11,6 +11,7 @@ import datetime
import shutil import shutil
import requests import requests
import ast import ast
import arrow
#jinja filters & config #jinja filters & config
@ -65,9 +66,7 @@ def create_post(post_directory, video_metadata):
response = requests.get(host+video_metadata['preview_path'], stream=True) response = requests.get(host+video_metadata['preview_path'], stream=True)
with open(os.path.join(post_directory, preview_image), 'wb') as img_file: with open(os.path.join(post_directory, preview_image), 'wb') as img_file:
shutil.copyfileobj(response.raw, img_file) shutil.copyfileobj(response.raw, img_file)
print('got image') print('Downloaded cover image')
else:
print('image exists')
#replace the truncated description with the full video description #replace the truncated description with the full video description
#peertube api is some broken thing in between a py dict and a json file #peertube api is some broken thing in between a py dict and a json file
@ -79,9 +78,28 @@ def create_post(post_directory, video_metadata):
with open(os.path.join(post_directory,'index.md'),'w') as f: with open(os.path.join(post_directory,'index.md'),'w') as f:
post = template.render(v=video_metadata, host=host, preview_image=preview_image) post = template.render(v=video_metadata, host=host, preview_image=preview_image)
f.write(post) f.write(post)
print(video_metadata['uuid'], 'written')
with open(os.path.join(post_directory, '.timestamp'), 'w') as f:
timestamp = arrow.get(video_metadata['updated_at'])
f.write(timestamp.format('X'))
def update_post(post_directory, video_metadata):
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()
current_timestamp = arrow.get(video_metadata['updated_at'])
current_timestamp = arrow.get(current_timestamp.format('X'))
if current_timestamp > arrow.get(old_timestamp):
print('Updating', video_metadata['name'], '({})'.format(video_metadata['uuid']))
create_post(post_dir, video_metadata)
else:
print('Video current: ', video_metadata['name'], '({})'.format(video_metadata['uuid']))
else:
create_post(post_dir, video_metadata)
output_dir = '/home/r/Programming/lumbung.space/lumbung.space-web/content/video' output_dir = '/home/r/Programming/lumbung.space/lumbung.space-web/content/video'
@ -93,13 +111,14 @@ template = env.get_template('index_template.md')
existing_posts = os.listdir(output_dir) existing_posts = os.listdir(output_dir)
for v in videos: for v in videos:
post_dir = os.path.join(output_dir,v['uuid'])
if v['uuid'] not in existing_posts: #if there is a video we dont already have, make it 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']) print('New: ', v['name'], '({})'.format(v['uuid']))
create_post(post_dir, v) create_post(post_dir, v)
elif v['uuid'] in existing_posts: # if we already have the video do nothing, possibly update elif v['uuid'] in existing_posts: # if we already have the video do nothing, possibly update
print(v['uuid'],'already exists') update_post(post_dir, v)
existing_posts.remove(v['uuid']) # create list of posts which have not been returned by peertube existing_posts.remove(v['uuid']) # create list of posts which have not been returned by peertube
for post in existing_posts: for post in existing_posts:

Loading…
Cancel
Save