added og meta tegs

This commit is contained in:
Alexander Karpov 2023-04-21 13:52:09 +03:00
parent 3a172e558a
commit 4c2a00a06a
10 changed files with 154 additions and 14 deletions

View File

@ -80,3 +80,20 @@
| source_code
| fonts_ext
)
meta = {
"video": {
"mp4": video.basic.meta,
"ogg": video.basic.meta,
"mpeg": video.basic.meta,
"quicktime": video.basic.meta,
}
}
meta_extensions = {
"mp4": video.basic.meta,
"mov": video.basic.meta,
"ogv": video.basic.meta,
"mpeg": video.basic.meta,
}

View File

@ -63,3 +63,29 @@ def view(file: File) -> (str, str):
)
return static, content
def meta(file: File):
descr = ""
i = 0
with file.file.open("r") as f:
lines = f.readlines()
for line in lines:
descr += line + "\n"
i += 1
if i > 100:
descr += "..."
break
url = file.get_absolute_url()
meat_f = f"""
<meta property="og:type" content="article">
<meta property="og:title" content="{file.name}">
<meta property="og:url" content="{url}">
<meta property="og:image" content="">
<meta property="og:description" content="{descr}">
<meta property="article:author" content="{file.user.username}">
<meta property="article:published_time" content="{file.created}">
<meta property="article:modified_time" content="{file.modified}">
"""
return meat_f

View File

@ -1,3 +1,5 @@
import ffmpeg
from akarpov.files.models import File
@ -26,3 +28,51 @@ def view(file: File) -> (str, str):
"""
return static, content
def meta(file: File):
stream = ffmpeg.probe(file.file.path, select_streams="v")["streams"][0]
width = ""
height = ""
preview = file.preview.url if file.preview else ""
url = file.get_absolute_url()
description = file.description if file.description else ""
if "width" in stream:
width = stream["width"]
if "height" in stream:
height = stream["height"]
meat_f = f"""
<meta property="og:site_name" content="akarpov">
<meta property="og:url" content="{url}">
<meta property="og:title" content="{file.name}">
<meta property="og:image" content="{preview}">
<meta property="og:description" content="{description}">
<meta property="og:type" content="video">
<meta property="og:video:url" content="{file.file.url}">
<meta property="og:video:secure_url" content="{file.file.url}">
<meta property="og:video:type" content="{file.file_type}">
<meta property="og:video:width" content="1280">
<meta property="og:video:height" content="720">
<meta property="og:video:url" content="{file.file.url}">
<meta property="og:video:secure_url" content="{file.file.url}">
<meta property="og:video:type" content="{file.file_type}">
<meta property="og:video:width" content="{width}">
<meta property="og:video:height" content="{height}">
<meta name="twitter:card" content="player">
<meta name="twitter:site" content="{url}">
<meta name="twitter:url" content="{url}">
<meta name="twitter:title" content="{file.name}">
<meta name="twitter:description" content="{file.description}">
<meta name="twitter:image" content="{preview}">
<meta name="twitter:app:name:iphone" content="Akarpov">
<meta name="twitter:app:name:ipad" content="Akarpov">
<meta name="twitter:app:name:googleplay" content="Akarpov">
<meta name="twitter:player" content="{file.file.url}">
<meta name="twitter:player:width" content="{width}">
<meta name="twitter:player:height" content="{height}">
"""
return meat_f

View File

@ -3,6 +3,7 @@
import structlog
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views.generic import DetailView, ListView, RedirectView, UpdateView
@ -16,7 +17,7 @@
)
from akarpov.files.forms import FileForm
from akarpov.files.models import File, Folder
from akarpov.files.previews import extensions, previews
from akarpov.files.previews import extensions, meta, meta_extensions, previews
logger = structlog.get_logger(__name__)
@ -58,11 +59,21 @@ class FileView(DetailView):
model = File
slug_field = "slug"
def dispatch(self, request, *args, **kwargs):
# redirect if bot
file = self.get_object()
useragent = request.META["HTTP_USER_AGENT"].lower()
if "bot" in useragent:
if file.file_type and file.file_type.split("/")[0] == "image":
return HttpResponseRedirect(file.file.url)
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["has_perm"] = self.object.user == self.request.user
static = ""
content = ""
meta_s = ""
extension = self.object.file.path.split(".")[-1]
try:
if self.object.file_type:
@ -76,10 +87,23 @@ def get_context_data(self, **kwargs):
static, content = extensions[extension](self.object)
elif extension in extensions:
static, content = extensions[extension](self.object)
if self.object.file_type:
t1, t2 = self.object.file_type.split("/")
loaded = False
if t1 in meta:
if t2 in meta[t1]:
meta_s = meta[t1][t2](self.object)
loaded = True
if not loaded and extension in meta_extensions:
meta_s = meta_extensions[extension](self.object)
elif extension in meta_extensions:
meta_s = meta_extensions[extension](self.object)
except Exception as e:
logger.error(e)
context["preview_static"] = static
context["preview_content"] = content
context["meta"] = meta_s
return context

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View File

@ -1,5 +1,11 @@
{% extends 'base.html' %}
{% block title %}sanspie: akarpov{% endblock title %}
{% block meta %}
<meta itemprop="description" content="about sanspie">
<meta property="og:description" content="about sanspie">
{% endblock %}
{% block css %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/devicon.min.css">
<script src="https://kit.fontawesome.com/87f586cb23.js" crossorigin="anonymous"></script>

View File

@ -6,8 +6,16 @@
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>{% block title %}akarpov{% endblock title %}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="My collection of apps and tools">
<meta name="author" content="sanspie">
<!-- Facebook Meta Tags -->
<meta property="og:title" content="sanspie's site" />
<meta property="og:site_name" content="akarpov.ru"/>
<meta property="og:image" content="{% static 'images/logo.jpg' %}" />
<meta property="og:image:width" content="32" />
<meta property="og:image:height" content="32" />
<meta property="og:type" content="website" />
<meta property="og:image" content="">
{% block meta %}
{% endblock %}
<link rel="icon" href="{% static 'images/favicons/favicon.ico' %}">
@ -28,6 +36,7 @@
{% endblock javascript %}
<script defer src="{% static 'js/bootstrap.bundle.min.js' %}" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="{% static 'js/project.js' %}"></script>
<noscript><h1 style="text-align: center">In order to use site properly enable JavaScript!</h1></noscript>
</head>
<body>

View File

@ -1,11 +1,18 @@
{% extends 'base.html' %}
{% block meta %}
{% autoescape off %}
{{ meta }}
{% endautoescape %}
{% endblock %}
{% block javascript %}
{% autoescape off %}
{{ preview_static }}
{% endautoescape %}
{% endblock %}
{% block content %}
<div class="row m-2">
<h1 class="fs-1 text-break mb-4">{{ file.name }}

View File

@ -18,9 +18,6 @@ services:
- mailhog
volumes:
- .:/app:z
- type: bind
source: ./akarpov/media/
target: /app/akarpov/media/
env_file:
- ./.envs/.local/.django
- ./.envs/.local/.postgres
@ -54,10 +51,6 @@ services:
<<: *django
image: akarpov_local_celeryworker
container_name: akarpov_local_celeryworker
volumes:
- type: bind
source: ./akarpov/media/
target: /app/akarpov/media/
depends_on:
- redis
- postgres
@ -69,10 +62,6 @@ services:
<<: *django
image: akarpov_local_celerybeat
container_name: akarpov_local_celerybeat
volumes:
- type: bind
source: ./akarpov/media/
target: /app/akarpov/media/
depends_on:
- redis
- postgres

View File

@ -15,6 +15,10 @@ services:
depends_on:
- postgres
- redis
volumes:
- type: bind
source: /var/www/media/
target: /app/akarpov/media/
env_file:
- ./.envs/.production/.django
- ./.envs/.production/.postgres
@ -36,11 +40,19 @@ services:
celeryworker:
<<: *django
volumes:
- type: bind
source: /var/www/media/
target: /app/akarpov/media/
image: akarpov_production_celeryworker
command: /start-celeryworker
celerybeat:
<<: *django
volumes:
- type: bind
source: /var/www/media/
target: /app/akarpov/media/
image: akarpov_production_celerybeat
command: /start-celerybeat