Support IndexNox

This commit is contained in:
Konstantin 2024-04-05 18:46:44 +02:00
parent 6bd0df1f34
commit 9b4bb6c9f1
5 changed files with 53 additions and 4 deletions

9
base/indexnow.py Normal file
View file

@ -0,0 +1,9 @@
import hashlib
from base64 import urlsafe_b64encode
from django.conf import settings
from django.utils.crypto import pbkdf2
def get_key():
return settings.INDEXNOW_KEY

View file

@ -1,3 +1,14 @@
from django.shortcuts import render
from django.conf import settings
from django.http import Http404, HttpResponse
from django.utils.crypto import constant_time_compare
from django.views import View
from base.indexnow import get_key
class KeyView(View):
def get(self, request, key):
if not constant_time_compare(key, get_key()):
raise Http404()
return HttpResponse(key)
# Create your views here.

25
base/wagtail_hooks.py Normal file
View file

@ -0,0 +1,25 @@
from django.conf import settings
from wagtail import hooks
from wagtail.models import Page
from urllib.parse import urlparse
from base.indexnow import get_key
import requests
@hooks.register('after_publish_page')
def after_publish_page(request, page):
page_url = page.full_url
# avoid if the host is localhost (development)
print("preparing to notify")
if urlparse(page_url).hostname == "localhost":
print("not notifying indexnow for localhost" + get_key() + ", page url: " + page_url)
return
session = requests.Session()
session.post(
"https://api.indexnow.org/indexnow",
json={
"host": urlparse(page_url).hostname,
"urlList": [page_url],
"key": get_key(),
},
).raise_for_status()

View file

@ -212,4 +212,6 @@ TAILWIND_APP_NAME = 'theme'
INTERNAL_IPS = [
"127.0.0.1",
]
]
INDEXNOW_KEY = '6207353506374e99ae67c41edb8df2e0'

View file

@ -10,6 +10,7 @@ from wagtail.documents import urls as wagtaildocs_urls
from blog.feeds import RssBlogFeed
from search import views as search_views
from wagtail.contrib.sitemaps.views import sitemap
from base.views import KeyView
urlpatterns = [
path("django-admin/", admin.site.urls),
@ -18,7 +19,8 @@ urlpatterns = [
path("search/", search_views.search, name="search"),
path("blog/feed/", RssBlogFeed(), name="blog_feed"),
path('sitemap.xml', sitemap),
path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type="text/plain"))
path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type="text/plain")),
path('<str:key>.txt', KeyView.as_view())
]
if settings.DEBUG: