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 = ""
password = ""
hashtags = [
"documentafifteen",
"harvestedbyputra",
"jalansesama",
"lumbungdotspace",
"majelisakakbar",
"majelisakbar",
"warungkopi",
"lumbungkios",
"kassel_ecosystem",
"ruruhaus",
"offbeatentrack_kassel",
"lumbungofpublishers",
"lumbungkiosproducts",
"documentafifteen"#,
# "harvestedbyputra",
# "jalansesama",
# "lumbungdotspace",
# "majelisakakbar",
# "majelisakbar",
# "warungkopi",
# "lumbungkios",
# "kassel_ecosystem",
# "ruruhaus",
# "offbeatentrack_kassel",
# "lumbungofpublishers",
# "lumbungkiosproducts",
]
@ -60,6 +60,21 @@ def download_media(post_directory, media_attachments):
with open(os.path.join(post_directory, image), "wb") as img_file:
shutil.copyfileobj(response.raw, img_file)
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):
@ -78,7 +93,6 @@ def create_post(post_directory, post_metadata):
post_metadata["account"]["display_name"] = name
env.filters["localize_media_url"] = localize_media_url
env.filters["filter_mastodon_urls"] = filter_mastodon_urls
template = env.get_template("hashtag.md")
with open(os.path.join(post_directory, "index.html"), "w") as f:
@ -130,7 +144,7 @@ def main():
timeline
) # 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"]))
# 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:

View File

@ -5,13 +5,25 @@ authors: ["{{ post_metadata.account.display_name }}"]
contributors: ["{{ post_metadata.account.acct}}"]
avatar: {{ post_metadata.account.avatar }}
categories: ["shouts"]
images: [{% for i in post_metadata.media_attachments %} {{ i.url }}, {% endfor %}]
title: {{ post_metadata.account.display_name }}
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 %}
{% if item.type == "image" %}
<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 %}
{{ post_metadata.content | filter_mastodon_urls }}