add support for videos in posts

This commit is contained in:
rra 2022-09-09 13:22:32 +02:00
parent 98299daa1b
commit d21158eb91
2 changed files with 42 additions and 16 deletions

View File

@ -11,19 +11,19 @@ instance = "https://social.lumbung.space"
email = "" email = ""
password = "" password = ""
hashtags = [ hashtags = [
"documentafifteen", "documentafifteen"#,
"harvestedbyputra", # "harvestedbyputra",
"jalansesama", # "jalansesama",
"lumbungdotspace", # "lumbungdotspace",
"majelisakakbar", # "majelisakakbar",
"majelisakbar", # "majelisakbar",
"warungkopi", # "warungkopi",
"lumbungkios", # "lumbungkios",
"kassel_ecosystem", # "kassel_ecosystem",
"ruruhaus", # "ruruhaus",
"offbeatentrack_kassel", # "offbeatentrack_kassel",
"lumbungofpublishers", # "lumbungofpublishers",
"lumbungkiosproducts", # "lumbungkiosproducts",
] ]
@ -60,6 +60,21 @@ def download_media(post_directory, media_attachments):
with open(os.path.join(post_directory, image), "wb") as img_file: with open(os.path.join(post_directory, image), "wb") as img_file:
shutil.copyfileobj(response.raw, img_file) shutil.copyfileobj(response.raw, img_file)
print("Downloaded cover image", image) print("Downloaded cover image", image)
elif item["type"] == "video":
video = localize_media_url(item["url"])
if not os.path.exists(os.path.join(post_directory, video)):
# download video file
response = requests.get(item["url"], stream=True)
with open(os.path.join(post_directory, video), "wb") as video_file:
shutil.copyfileobj(response.raw, video_file)
print("Downloaded video in post", video)
if not os.path.exists(os.path.join(post_directory, "thumbnail.png")):
#download video preview
response = requests.get(item["preview_url"], stream=True)
with open(os.path.join(post_directory, "thumbnail.png"), "wb") as thumbnail:
shutil.copyfileobj(response.raw, thumbnail)
print("Downloaded thumbnail for", video)
def create_post(post_directory, post_metadata): def create_post(post_directory, post_metadata):
@ -78,7 +93,6 @@ def create_post(post_directory, post_metadata):
post_metadata["account"]["display_name"] = name post_metadata["account"]["display_name"] = name
env.filters["localize_media_url"] = localize_media_url env.filters["localize_media_url"] = localize_media_url
env.filters["filter_mastodon_urls"] = filter_mastodon_urls env.filters["filter_mastodon_urls"] = filter_mastodon_urls
template = env.get_template("hashtag.md") template = env.get_template("hashtag.md")
with open(os.path.join(post_directory, "index.html"), "w") as f: with open(os.path.join(post_directory, "index.html"), "w") as f:
@ -130,7 +144,7 @@ def main():
timeline timeline
) # returns all the rest n.b. can take a while because of rate limit ) # returns all the rest n.b. can take a while because of rate limit
for post_metadata in timeline: for post_metadata in timeline[:6]:
post_dir = os.path.join(hashtag_dir, str(post_metadata["id"])) post_dir = os.path.join(hashtag_dir, str(post_metadata["id"]))
# if there is a post in the feed we dont already have locally, make it # if there is a post in the feed we dont already have locally, make it
if str(post_metadata["id"]) not in all_existing_posts: if str(post_metadata["id"]) not in all_existing_posts:

View File

@ -5,13 +5,25 @@ authors: ["{{ post_metadata.account.display_name }}"]
contributors: ["{{ post_metadata.account.acct}}"] contributors: ["{{ post_metadata.account.acct}}"]
avatar: {{ post_metadata.account.avatar }} avatar: {{ post_metadata.account.avatar }}
categories: ["shouts"] categories: ["shouts"]
images: [{% for i in post_metadata.media_attachments %} {{ i.url }}, {% endfor %}]
title: {{ post_metadata.account.display_name }} title: {{ post_metadata.account.display_name }}
tags: [{% for i in post_metadata.tags %} "{{ i.name }}", {% endfor %}] tags: [{% for i in post_metadata.tags %} "{{ i.name }}", {% endfor %}]
images: [{% for i in post_metadata.media_attachments %}{% if i.type == "image" %}"{{ i.url | localize_media_url }}", {%endif%}{% endfor %}]
videos: [{% for i in post_metadata.media_attachments %}{% if i.type == "video" %}"{{ i.url | localize_media_url }}", {%endif%}{% endfor %}]
--- ---
{% for item in post_metadata.media_attachments %} {% for item in post_metadata.media_attachments %}
{% if item.type == "image" %}
<img src="{{item.url | localize_media_url }}" alt="{{item.description}}"> <img src="{{item.url | localize_media_url }}" alt="{{item.description}}">
{% endif %}
{% endfor %}
{% for item in post_metadata.media_attachments %}
{% if item.type == "video" %}
<video controls width="540px" preload="none" poster="thumbnail.png">
<source src="{{item.url | localize_media_url }}" type="video/mp4">
{% if item.description %}{{item.description}}{% endif %}
</video>
{% endif %}
{% endfor %} {% endfor %}
{{ post_metadata.content | filter_mastodon_urls }} {{ post_metadata.content | filter_mastodon_urls }}